perlcall.pod SAVETMPS/FREETMPS bracket
[p5sagit/p5-mst-13.2.git] / lib / Getopt / Std.pm
CommitLineData
a0d0e21e 1package Getopt::Std;
2require 5.000;
3require Exporter;
4
f06db76b 5=head1 NAME
6
7getopt - Process single-character switches with switch clustering
8
9getopts - Process single-character switches with switch clustering
10
11=head1 SYNOPSIS
12
13 use Getopt::Std;
0bc14741 14
15 getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
16 getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts
f06db76b 17 getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
18 # Sets opt_* as a side effect.
0bc14741 19 getopts('oif:', \%opts); # options as above. Values in %opts
f06db76b 20
21=head1 DESCRIPTION
22
23The getopt() functions processes single-character switches with switch
24clustering. Pass one argument which is a string containing all switches
25that take an argument. For each switch found, sets $opt_x (where x is the
26switch name) to the value of the argument, or 1 if no argument. Switches
27which take an argument don't care whether there is a space between the
28switch and the argument.
29
535b5725 30Note that, if your code is running under the recommended C<use strict
31'vars'> pragma, it may be helpful to declare these package variables
32via C<use vars> perhaps something like this:
33
34 use vars qw/ $opt_foo $opt_bar /;
35
0bc14741 36For those of you who don't like additional variables being created, getopt()
37and getopts() will also accept a hash reference as an optional second argument.
38Hash keys will be x (where x is the switch name) with key values the value of
39the argument or 1 if no argument is specified.
40
f06db76b 41=cut
42
a0d0e21e 43@ISA = qw(Exporter);
44@EXPORT = qw(getopt getopts);
45
46# $RCSfile: getopt.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:23:58 $
47
48# Process single-character switches with switch clustering. Pass one argument
49# which is a string containing all switches that take an argument. For each
50# switch found, sets $opt_x (where x is the switch name) to the value of the
51# argument, or 1 if no argument. Switches which take an argument don't care
52# whether there is a space between the switch and the argument.
53
54# Usage:
55# getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
56
0bc14741 57sub getopt ($;$) {
58 local($argumentative, $hash) = @_;
a0d0e21e 59 local($_,$first,$rest);
6ca64377 60 local @EXPORT;
a0d0e21e 61
62 while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
63 ($first,$rest) = ($1,$2);
64 if (index($argumentative,$first) >= 0) {
65 if ($rest ne '') {
66 shift(@ARGV);
67 }
68 else {
69 shift(@ARGV);
70 $rest = shift(@ARGV);
71 }
0bc14741 72 if (ref $hash) {
73 $$hash{$first} = $rest;
74 }
75 else {
29d4204f 76 ${"opt_$first"} = $rest;
0bc14741 77 push( @EXPORT, "\$opt_$first" );
78 }
a0d0e21e 79 }
80 else {
0bc14741 81 if (ref $hash) {
82 $$hash{$first} = 1;
83 }
84 else {
29d4204f 85 ${"opt_$first"} = 1;
0bc14741 86 push( @EXPORT, "\$opt_$first" );
87 }
a0d0e21e 88 if ($rest ne '') {
89 $ARGV[0] = "-$rest";
90 }
91 else {
92 shift(@ARGV);
93 }
94 }
95 }
6ca64377 96 unless (ref $hash) {
97 local $Exporter::ExportLevel = 1;
98 import Getopt::Std;
99 }
a0d0e21e 100}
101
102# Usage:
103# getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
104# # side effect.
105
0bc14741 106sub getopts ($;$) {
107 local($argumentative, $hash) = @_;
a0d0e21e 108 local(@args,$_,$first,$rest);
109 local($errs) = 0;
6ca64377 110 local @EXPORT;
a0d0e21e 111
112 @args = split( / */, $argumentative );
113 while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
114 ($first,$rest) = ($1,$2);
115 $pos = index($argumentative,$first);
116 if($pos >= 0) {
f06db76b 117 if(defined($args[$pos+1]) and ($args[$pos+1] eq ':')) {
a0d0e21e 118 shift(@ARGV);
119 if($rest eq '') {
120 ++$errs unless @ARGV;
121 $rest = shift(@ARGV);
122 }
0bc14741 123 if (ref $hash) {
124 $$hash{$first} = $rest;
125 }
126 else {
29d4204f 127 ${"opt_$first"} = $rest;
0bc14741 128 push( @EXPORT, "\$opt_$first" );
129 }
a0d0e21e 130 }
131 else {
0bc14741 132 if (ref $hash) {
133 $$hash{$first} = 1;
134 }
135 else {
29d4204f 136 ${"opt_$first"} = 1;
0bc14741 137 push( @EXPORT, "\$opt_$first" );
138 }
a0d0e21e 139 if($rest eq '') {
140 shift(@ARGV);
141 }
142 else {
143 $ARGV[0] = "-$rest";
144 }
145 }
146 }
147 else {
148 print STDERR "Unknown option: $first\n";
149 ++$errs;
150 if($rest ne '') {
151 $ARGV[0] = "-$rest";
152 }
153 else {
154 shift(@ARGV);
155 }
156 }
157 }
6ca64377 158 unless (ref $hash) {
159 local $Exporter::ExportLevel = 1;
160 import Getopt::Std;
161 }
a0d0e21e 162 $errs == 0;
163}
164
1651;
166