7 getopt - Process single-character switches with switch clustering
9 getopts - Process single-character switches with switch clustering
14 getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
15 getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
16 # Sets opt_* as a side effect.
20 The getopt() functions processes single-character switches with switch
21 clustering. Pass one argument which is a string containing all switches
22 that take an argument. For each switch found, sets $opt_x (where x is the
23 switch name) to the value of the argument, or 1 if no argument. Switches
24 which take an argument don't care whether there is a space between the
25 switch and the argument.
30 @EXPORT = qw(getopt getopts);
32 # $RCSfile: getopt.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:23:58 $
34 # Process single-character switches with switch clustering. Pass one argument
35 # which is a string containing all switches that take an argument. For each
36 # switch found, sets $opt_x (where x is the switch name) to the value of the
37 # argument, or 1 if no argument. Switches which take an argument don't care
38 # whether there is a space between the switch and the argument.
41 # getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
44 local($argumentative) = @_;
45 local($_,$first,$rest);
46 local $Exporter::ExportLevel;
48 while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
49 ($first,$rest) = ($1,$2);
50 if (index($argumentative,$first) >= 0) {
58 eval "\$opt_$first = \$rest;";
59 push( @EXPORT, "\$opt_$first" );
62 eval "\$opt_$first = 1;";
63 push( @EXPORT, "\$opt_$first" );
72 $Exporter::ExportLevel++;
77 # getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
81 local($argumentative) = @_;
82 local(@args,$_,$first,$rest);
84 local $Exporter::ExportLevel;
86 @args = split( / */, $argumentative );
87 while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
88 ($first,$rest) = ($1,$2);
89 $pos = index($argumentative,$first);
91 if(defined($args[$pos+1]) and ($args[$pos+1] eq ':')) {
97 eval "\$opt_$first = \$rest;";
98 push( @EXPORT, "\$opt_$first" );
101 eval "\$opt_$first = 1";
102 push( @EXPORT, "\$opt_$first" );
112 print STDERR "Unknown option: $first\n";
122 $Exporter::ExportLevel++;