1 #============================================================================
5 # Perl5 module to read command line argument and update the variable
6 # values in an AppConfig::State object accordingly.
8 # Written by Andy Wardley <abw@wardley.org>
10 # Copyright (C) 1997-2007 Andy Wardley. All Rights Reserved.
11 # Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
12 #============================================================================
14 package AppConfig::Args;
18 our $VERSION = '1.65';
21 #------------------------------------------------------------------------
24 # Module constructor. The first, mandatory parameter should be a
25 # reference to an AppConfig::State object to which all actions should
26 # be applied. The second parameter may be a reference to a list of
27 # command line arguments. This list reference is passed to args() for
30 # Returns a reference to a newly created AppConfig::Args object.
31 #------------------------------------------------------------------------
39 STATE => $state, # AppConfig::State ref
40 DEBUG => $state->_debug(), # store local copy of debug
41 PEDANTIC => $state->_pedantic, # and pedantic flags
46 # call parse() to parse any arg list passed
54 #------------------------------------------------------------------------
57 # Examines the argument list and updates the contents of the
58 # AppConfig::State referenced by $self->{ STATE } accordingly. If
59 # no argument list is provided then the method defaults to examining
60 # @ARGV. The method reports any warning conditions (such as undefined
61 # variables) by calling $self->{ STATE }->_error() and then continues to
62 # examine the rest of the list. If the PEDANTIC option is set in the
63 # AppConfig::State object, this behaviour is overridden and the method
64 # returns 0 immediately on any parsing error.
66 # Returns 1 on success or 0 if one or more warnings were raised.
67 #------------------------------------------------------------------------
71 my $argv = shift || \@ARGV;
73 my ($arg, $nargs, $variable, $value);
76 # take a local copy of the state to avoid much hash dereferencing
77 my ($state, $debug, $pedantic) = @$self{ qw( STATE DEBUG PEDANTIC ) };
79 # loop around arguments
80 ARG: while (@$argv && $argv->[0] =~ /^-/) {
83 # '--' indicates the end of the options
87 ($variable = $arg) =~ s/^-(-)?//;
89 # test for '--' prefix and push back any '=value' item
91 ($variable, $value) = split(/=/, $variable);
92 unshift(@$argv, $value) if defined $value;
95 # check the variable exists
96 if ($state->_exists($variable)) {
98 # see if it expects any mandatory arguments
99 $nargs = $state->_argcount($variable);
101 # check there's another arg and it's not another '-opt'
102 if(defined($argv->[0])) {
103 $value = shift(@$argv);
106 $state->_error("$arg expects an argument");
108 last ARG if $pedantic;
113 # set a value of 1 if option doesn't expect an argument
117 # set the variable with the new value
118 $state->set($variable, $value);
121 $state->_error("$arg: invalid option");
123 last ARG if $pedantic;
128 return $warnings ? 0 : 1;
139 AppConfig::Args - Perl5 module for reading command line arguments.
145 my $state = AppConfig::State->new(\%cfg);
146 my $cfgargs = AppConfig::Args->new($state);
148 $cfgargs->parse(\@args); # read args
152 AppConfig::Args is a Perl5 module which reads command line arguments and
153 uses the options therein to update variable values in an AppConfig::State
156 AppConfig::File is distributed as part of the AppConfig bundle.
160 =head2 USING THE AppConfig::Args MODULE
162 To import and use the AppConfig::Args module the following line should appear
167 AppConfig::Args is used automatically if you use the AppConfig module
168 and create an AppConfig::Args object through the parse() method.
170 AppConfig::File is implemented using object-oriented methods. A new
171 AppConfig::Args object is created and initialised using the new() method.
172 This returns a reference to a new AppConfig::File object. A reference to
173 an AppConfig::State object should be passed in as the first parameter:
175 my $state = AppConfig::State->new();
176 my $cfgargs = AppConfig::Args->new($state);
178 This will create and return a reference to a new AppConfig::Args object.
180 =head2 PARSING COMMAND LINE ARGUMENTS
182 The C<parse()> method is used to read a list of command line arguments and
183 update the STATE accordingly. A reference to the list of arguments should
186 $cfgargs->parse(\@ARGV);
188 If the method is called without a reference to an argument list then it
189 will examine and manipulate @ARGV.
191 If the PEDANTIC option is turned off in the AppConfig::State object, any
192 parsing errors (invalid variables, unvalidated values, etc) will generate
193 warnings, but not cause the method to return. Having processed all
194 arguments, the method will return 1 if processed without warning or 0 if
195 one or more warnings were raised. When the PEDANTIC option is turned on,
196 the method generates a warning and immediately returns a value of 0 as soon
197 as it encounters any parsing error.
199 The method continues parsing arguments until it detects the first one that
200 does not start with a leading dash, '-'. Arguments that constitute values
201 for other options are not examined in this way.
203 =head1 FUTURE DEVELOPMENT
205 This module was developed to provide backwards compatibility (to some
206 degree) with the preceeding App::Config module. The argument parsing
207 it provides is basic but offers a quick and efficient solution for those
208 times when simple option handling is all that is required.
210 If you require more flexibility in parsing command line arguments, then
211 you should consider using the AppConfig::Getopt module. This is loaded
212 and used automatically by calling the AppConfig getopt() method.
214 The AppConfig::Getopt module provides considerably extended functionality
215 over the AppConfig::Args module by delegating out the task of argument
216 parsing to Johan Vromans' Getopt::Long module. For advanced command-line
217 parsing, this module (either Getopt::Long by itself, or in conjunction with
218 AppConfig::Getopt) is highly recommended.
222 Andy Wardley, E<lt>abw@wardley.orgE<gt>
226 Copyright (C) 1997-2007 Andy Wardley. All Rights Reserved.
228 Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
230 This module is free software; you can redistribute it and/or modify it
231 under the same terms as Perl itself.
235 AppConfig, AppConfig::State, AppConfig::Getopt, Getopt::Long