Commit | Line | Data |
---|---|---|
a687059c | 1 | ;# getopts.pl - a better getopt.pl |
2 | ||
3 | ;# Usage: | |
4 | ;# do Getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a | |
5 | ;# # side effect. | |
6 | ||
7 | sub Getopts { | |
8 | local($argumentative) = @_; | |
55204971 | 9 | local(@args,$_,$first,$rest); |
10 | local($errs) = 0; | |
a687059c | 11 | |
12 | @args = split( / */, $argumentative ); | |
55204971 | 13 | while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) { |
a687059c | 14 | ($first,$rest) = ($1,$2); |
15 | $pos = index($argumentative,$first); | |
55497cff | 16 | if($pos >= 0) { |
17 | if($pos < $#args && $args[$pos+1] eq ':') { | |
bf38876a | 18 | shift(@ARGV); |
a687059c | 19 | if($rest eq '') { |
8adcabd8 | 20 | ++$errs unless @ARGV; |
bf38876a | 21 | $rest = shift(@ARGV); |
a687059c | 22 | } |
55497cff | 23 | ${"opt_$first"} = $rest; |
a687059c | 24 | } |
25 | else { | |
55497cff | 26 | ${"opt_$first"} = 1; |
a687059c | 27 | if($rest eq '') { |
bf38876a | 28 | shift(@ARGV); |
a687059c | 29 | } |
30 | else { | |
31 | $ARGV[0] = "-$rest"; | |
32 | } | |
33 | } | |
34 | } | |
35 | else { | |
ac58e20f | 36 | print STDERR "Unknown option: $first\n"; |
37 | ++$errs; | |
a687059c | 38 | if($rest ne '') { |
39 | $ARGV[0] = "-$rest"; | |
40 | } | |
41 | else { | |
bf38876a | 42 | shift(@ARGV); |
a687059c | 43 | } |
44 | } | |
45 | } | |
ac58e20f | 46 | $errs == 0; |
a687059c | 47 | } |
48 | ||
49 | 1; |