xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/getopts.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate;# getopts.pl - a better getopt.pl
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# This library is no longer being maintained, and is included for backward
4*0Sstevel@tonic-gate# compatibility with Perl 4 programs which may require it.
5*0Sstevel@tonic-gate#
6*0Sstevel@tonic-gate# In particular, this should not be used as an example of modern Perl
7*0Sstevel@tonic-gate# programming techniques.
8*0Sstevel@tonic-gate#
9*0Sstevel@tonic-gate# Suggested alternatives: Getopt::Long  or  Getopt::Std
10*0Sstevel@tonic-gate#
11*0Sstevel@tonic-gate;# Usage:
12*0Sstevel@tonic-gate;#      do Getopts('a:bc');  # -a takes arg. -b & -c not. Sets opt_* as a
13*0Sstevel@tonic-gate;#                           #  side effect.
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gatesub Getopts {
16*0Sstevel@tonic-gate    local($argumentative) = @_;
17*0Sstevel@tonic-gate    local(@args,$_,$first,$rest);
18*0Sstevel@tonic-gate    local($errs) = 0;
19*0Sstevel@tonic-gate    local($[) = 0;
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gate    @args = split( / */, $argumentative );
22*0Sstevel@tonic-gate    while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
23*0Sstevel@tonic-gate		($first,$rest) = ($1,$2);
24*0Sstevel@tonic-gate		$pos = index($argumentative,$first);
25*0Sstevel@tonic-gate		if($pos >= $[) {
26*0Sstevel@tonic-gate			if($args[$pos+1] eq ':') {
27*0Sstevel@tonic-gate				shift(@ARGV);
28*0Sstevel@tonic-gate				if($rest eq '') {
29*0Sstevel@tonic-gate					++$errs unless(@ARGV);
30*0Sstevel@tonic-gate					$rest = shift(@ARGV);
31*0Sstevel@tonic-gate				}
32*0Sstevel@tonic-gate				eval "
33*0Sstevel@tonic-gate				push(\@opt_$first, \$rest);
34*0Sstevel@tonic-gate				if(\$opt_$first eq '') {
35*0Sstevel@tonic-gate					\$opt_$first = \$rest;
36*0Sstevel@tonic-gate				}
37*0Sstevel@tonic-gate				else {
38*0Sstevel@tonic-gate					\$opt_$first .= ' ' . \$rest;
39*0Sstevel@tonic-gate				}
40*0Sstevel@tonic-gate				";
41*0Sstevel@tonic-gate			}
42*0Sstevel@tonic-gate			else {
43*0Sstevel@tonic-gate				eval "\$opt_$first = 1";
44*0Sstevel@tonic-gate				if($rest eq '') {
45*0Sstevel@tonic-gate					shift(@ARGV);
46*0Sstevel@tonic-gate				}
47*0Sstevel@tonic-gate				else {
48*0Sstevel@tonic-gate					$ARGV[0] = "-$rest";
49*0Sstevel@tonic-gate				}
50*0Sstevel@tonic-gate			}
51*0Sstevel@tonic-gate		}
52*0Sstevel@tonic-gate		else {
53*0Sstevel@tonic-gate			print STDERR "Unknown option: $first\n";
54*0Sstevel@tonic-gate			++$errs;
55*0Sstevel@tonic-gate			if($rest ne '') {
56*0Sstevel@tonic-gate				$ARGV[0] = "-$rest";
57*0Sstevel@tonic-gate			}
58*0Sstevel@tonic-gate			else {
59*0Sstevel@tonic-gate				shift(@ARGV);
60*0Sstevel@tonic-gate			}
61*0Sstevel@tonic-gate		}
62*0Sstevel@tonic-gate	}
63*0Sstevel@tonic-gate    $errs == 0;
64*0Sstevel@tonic-gate}
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate1;
67