xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/getopt.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate;# $RCSfile: getopt.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:23:58 $
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;# Process single-character switches with switch clustering.  Pass one argument
12*0Sstevel@tonic-gate;# which is a string containing all switches that take an argument.  For each
13*0Sstevel@tonic-gate;# switch found, sets $opt_x (where x is the switch name) to the value of the
14*0Sstevel@tonic-gate;# argument, or 1 if no argument.  Switches which take an argument don't care
15*0Sstevel@tonic-gate;# whether there is a space between the switch and the argument.
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate;# Usage:
18*0Sstevel@tonic-gate;#	do Getopt('oDI');  # -o, -D & -I take arg.  Sets opt_* as a side effect.
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gatesub Getopt {
21*0Sstevel@tonic-gate    local($argumentative) = @_;
22*0Sstevel@tonic-gate    local($_,$first,$rest);
23*0Sstevel@tonic-gate    local($[) = 0;
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate    while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
26*0Sstevel@tonic-gate	($first,$rest) = ($1,$2);
27*0Sstevel@tonic-gate	if (index($argumentative,$first) >= $[) {
28*0Sstevel@tonic-gate	    if ($rest ne '') {
29*0Sstevel@tonic-gate		shift(@ARGV);
30*0Sstevel@tonic-gate	    }
31*0Sstevel@tonic-gate	    else {
32*0Sstevel@tonic-gate		shift(@ARGV);
33*0Sstevel@tonic-gate		$rest = shift(@ARGV);
34*0Sstevel@tonic-gate	    }
35*0Sstevel@tonic-gate	    ${"opt_$first"} = $rest;
36*0Sstevel@tonic-gate	}
37*0Sstevel@tonic-gate	else {
38*0Sstevel@tonic-gate	    ${"opt_$first"} = 1;
39*0Sstevel@tonic-gate	    if ($rest ne '') {
40*0Sstevel@tonic-gate		$ARGV[0] = "-$rest";
41*0Sstevel@tonic-gate	    }
42*0Sstevel@tonic-gate	    else {
43*0Sstevel@tonic-gate		shift(@ARGV);
44*0Sstevel@tonic-gate	    }
45*0Sstevel@tonic-gate	}
46*0Sstevel@tonic-gate    }
47*0Sstevel@tonic-gate}
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate1;
50