perl 3.0 patch #10 patch #9, continued
[p5sagit/p5-mst-13.2.git] / lib / getopts.pl
CommitLineData
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
7sub Getopts {
8 local($argumentative) = @_;
9 local(@args,$_,$first,$rest);
10
11 @args = split( / */, $argumentative );
12 while(($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
13 ($first,$rest) = ($1,$2);
14 $pos = index($argumentative,$first);
15 if($pos >= $[) {
16 if($args[$pos+1] eq ':') {
bf38876a 17 shift(@ARGV);
a687059c 18 if($rest eq '') {
bf38876a 19 $rest = shift(@ARGV);
a687059c 20 }
21 eval "\$opt_$first = \$rest;";
22 }
23 else {
24 eval "\$opt_$first = 1";
25 if($rest eq '') {
bf38876a 26 shift(@ARGV);
a687059c 27 }
28 else {
29 $ARGV[0] = "-$rest";
30 }
31 }
32 }
33 else {
34 print stderr "Unknown option: $first\n";
35 if($rest ne '') {
36 $ARGV[0] = "-$rest";
37 }
38 else {
bf38876a 39 shift(@ARGV);
a687059c 40 }
41 }
42 }
43}
44
451;