7 getopt - Process single-character switches with switch clustering
9 getopts - Process single-character switches with switch clustering
15 getopt('oDI'); # -o, -D & -I take arg. Sets $opt_* as a side effect.
16 getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts
17 getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
18 # Sets $opt_* as a side effect.
19 getopts('oif:', \%opts); # options as above. Values in %opts
23 The getopt() function processes single-character switches with switch
24 clustering. Pass one argument which is a string containing all switches
25 that take an argument. For each switch found, sets $opt_x (where x is the
26 switch name) to the value of the argument, or 1 if no argument. Switches
27 which take an argument don't care whether there is a space between the
28 switch and the argument.
30 The getopts() function is similar, but you should pass to it the list of all
31 switches to be recognized. If unspecified switches are found on the
32 command-line, the user will be warned that an unknown option was given.
34 Note that, if your code is running under the recommended C<use strict
35 'vars'> pragma, you will need to declare these package variables
40 For those of you who don't like additional global variables being created, getopt()
41 and getopts() will also accept a hash reference as an optional second argument.
42 Hash keys will be x (where x is the switch name) with key values the value of
43 the argument or 1 if no argument is specified.
45 To allow programs to process arguments that look like switches, but aren't,
46 both functions will stop processing switches when they see the argument
47 C<-->. The C<--> will be removed from @ARGV.
52 @EXPORT = qw(getopt getopts);
55 # Process single-character switches with switch clustering. Pass one argument
56 # which is a string containing all switches that take an argument. For each
57 # switch found, sets $opt_x (where x is the switch name) to the value of the
58 # argument, or 1 if no argument. Switches which take an argument don't care
59 # whether there is a space between the switch and the argument.
62 # getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
65 my ($argumentative, $hash) = @_;
66 $argumentative = '' if !defined $argumentative;
71 while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
72 ($first,$rest) = ($1,$2);
73 if (/^--$/) { # early exit if --
77 if (index($argumentative,$first) >= 0) {
86 $$hash{$first} = $rest;
89 ${"opt_$first"} = $rest;
90 push( @EXPORT, "\$opt_$first" );
99 push( @EXPORT, "\$opt_$first" );
110 local $Exporter::ExportLevel = 1;
116 # getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
120 my ($argumentative, $hash) = @_;
121 my (@args,$first,$rest);
126 @args = split( / */, $argumentative );
127 while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
128 ($first,$rest) = ($1,$2);
129 if (/^--$/) { # early exit if --
133 $pos = index($argumentative,$first);
135 if (defined($args[$pos+1]) and ($args[$pos+1] eq ':')) {
138 ++$errs unless @ARGV;
139 $rest = shift(@ARGV);
142 $$hash{$first} = $rest;
145 ${"opt_$first"} = $rest;
146 push( @EXPORT, "\$opt_$first" );
155 push( @EXPORT, "\$opt_$first" );
166 warn "Unknown option: $first\n";
177 local $Exporter::ExportLevel = 1;