This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 4.0 patch 17: patch #11, continued
[perl5.git] / lib / getopts.pl
CommitLineData
a687059c
LW
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) = @_;
ac58e20f
LW
9 local(@args,$_,$first,$rest,$errs);
10 local($[) = 0;
a687059c
LW
11
12 @args = split( / */, $argumentative );
13 while(($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
14 ($first,$rest) = ($1,$2);
15 $pos = index($argumentative,$first);
16 if($pos >= $[) {
17 if($args[$pos+1] eq ':') {
bf38876a 18 shift(@ARGV);
a687059c 19 if($rest eq '') {
bf38876a 20 $rest = shift(@ARGV);
a687059c
LW
21 }
22 eval "\$opt_$first = \$rest;";
23 }
24 else {
25 eval "\$opt_$first = 1";
26 if($rest eq '') {
bf38876a 27 shift(@ARGV);
a687059c
LW
28 }
29 else {
30 $ARGV[0] = "-$rest";
31 }
32 }
33 }
34 else {
ac58e20f
LW
35 print STDERR "Unknown option: $first\n";
36 ++$errs;
a687059c
LW
37 if($rest ne '') {
38 $ARGV[0] = "-$rest";
39 }
40 else {
bf38876a 41 shift(@ARGV);
a687059c
LW
42 }
43 }
44 }
ac58e20f 45 $errs == 0;
a687059c
LW
46}
47
481;