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() functions 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 Note that, if your code is running under the recommended C<use strict
31 'vars'> pragma, you will need to declare these package variables
34 our($opt_foo, $opt_bar);
36 For those of you who don't like additional global variables being created, getopt()
37 and getopts() will also accept a hash reference as an optional second argument.
38 Hash keys will be x (where x is the switch name) with key values the value of
39 the argument or 1 if no argument is specified.
41 To allow programs to process arguments that look like switches, but aren't,
42 both functions will stop processing switches when they see the argument
43 C<-->. The C<--> will be removed from @ARGV.
48 @EXPORT = qw(getopt getopts);
51 # Process single-character switches with switch clustering. Pass one argument
52 # which is a string containing all switches that take an argument. For each
53 # switch found, sets $opt_x (where x is the switch name) to the value of the
54 # argument, or 1 if no argument. Switches which take an argument don't care
55 # whether there is a space between the switch and the argument.
58 # getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
61 local($argumentative, $hash) = @_;
62 local($_,$first,$rest);
65 while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
66 ($first,$rest) = ($1,$2);
67 if (/^--$/) { # early exit if --
71 if (index($argumentative,$first) >= 0) {
80 $$hash{$first} = $rest;
83 ${"opt_$first"} = $rest;
84 push( @EXPORT, "\$opt_$first" );
93 push( @EXPORT, "\$opt_$first" );
104 local $Exporter::ExportLevel
= 1;
110 # getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
114 local($argumentative, $hash) = @_;
115 local(@args,$_,$first,$rest);
119 @args = split( / */, $argumentative );
120 while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
121 ($first,$rest) = ($1,$2);
122 if (/^--$/) { # early exit if --
126 $pos = index($argumentative,$first);
128 if (defined($args[$pos+1]) and ($args[$pos+1] eq ':')) {
131 ++$errs unless @ARGV;
132 $rest = shift(@ARGV);
135 $$hash{$first} = $rest;
138 ${"opt_$first"} = $rest;
139 push( @EXPORT, "\$opt_$first" );
148 push( @EXPORT, "\$opt_$first" );
159 warn "Unknown option: $first\n";
170 local $Exporter::ExportLevel
= 1;