@@ -14,7 +14,7 @@ Getopt::Std - Process single-character switches with switch clustering
1414 use Getopt::Std;
1515
1616 getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
17- # Sets $opt_* global variables as a side effect
17+ # Sets $opt_* global variables as a side effect
1818 getopts('oif:', \my %opts); # Options as above, values in %opts
1919 getopt('oDI'); # -o, -D & -I take arguments
2020 # Sets $opt_* global variables as a side effect
@@ -84,7 +84,7 @@ and C<version_mess()> with the switches string as an argument.
8484
8585our @ISA = qw( Exporter) ;
8686our @EXPORT = qw( getopt getopts) ;
87- our $VERSION = ' 1.14 ' ;
87+ our $VERSION = ' 1.15 ' ;
8888# uncomment the next line to disable 1.03-backward compatibility paranoia
8989# $STANDARD_HELP_VERSION = 1;
9090
@@ -95,7 +95,7 @@ our $VERSION = '1.14';
9595# whether there is a space between the switch and the argument.
9696
9797# Usage:
98- # getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
98+ # getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
9999
100100sub getopt (;$$) {
101101 my ($argumentative , $hash ) = @_ ;
@@ -105,56 +105,56 @@ sub getopt (;$$) {
105105 local @EXPORT ;
106106
107107 while (@ARGV && ($_ = $ARGV [0]) =~ / ^-(.)(.*)/ ) {
108- ($first , $rest ) = ($1 , $2 );
109- if (/ ^--$ / ) { # early exit if --
110- shift @ARGV ;
111- last ;
112- }
113- if (index ($argumentative , $first ) >= 0) {
114- if ($rest ne ' ' ) {
115- shift (@ARGV );
116- }
117- else {
118- shift (@ARGV );
119- $rest = shift (@ARGV );
120- }
121- if (ref $hash ) {
122- $$hash {$first } = $rest ;
123- }
124- else {
125- no strict ' refs' ;
126- ${" opt_$first " } = $rest ;
127- push ( @EXPORT , " \$ opt_$first " );
128- }
129- }
130- else {
131- if (ref $hash ) {
132- $$hash {$first } = 1;
133- }
134- else {
135- no strict ' refs' ;
136- ${" opt_$first " } = 1;
137- push ( @EXPORT , " \$ opt_$first " );
138- }
139- if ($rest ne ' ' ) {
140- $ARGV [0] = " -$rest " ;
141- }
142- else {
143- shift (@ARGV );
144- }
145- }
108+ ($first , $rest ) = ($1 , $2 );
109+ if (/ ^--$ / ) { # early exit if --
110+ shift @ARGV ;
111+ last ;
112+ }
113+ if (index ($argumentative , $first ) >= 0) {
114+ if ($rest ne ' ' ) {
115+ shift (@ARGV );
116+ }
117+ else {
118+ shift (@ARGV );
119+ $rest = shift (@ARGV );
120+ }
121+ if (ref $hash ) {
122+ $$hash {$first } = $rest ;
123+ }
124+ else {
125+ no strict ' refs' ;
126+ ${" opt_$first " } = $rest ;
127+ push ( @EXPORT , " \$ opt_$first " );
128+ }
129+ }
130+ else {
131+ if (ref $hash ) {
132+ $$hash {$first } = 1;
133+ }
134+ else {
135+ no strict ' refs' ;
136+ ${" opt_$first " } = 1;
137+ push ( @EXPORT , " \$ opt_$first " );
138+ }
139+ if ($rest ne ' ' ) {
140+ $ARGV [0] = " -$rest " ;
141+ }
142+ else {
143+ shift (@ARGV );
144+ }
145+ }
146146 }
147- unless (ref $hash ) {
148- local $Exporter::ExportLevel = 1;
149- Getopt::Std-> import ;
147+ unless (ref $hash ) {
148+ local $Exporter::ExportLevel = 1;
149+ Getopt::Std-> import ;
150150 }
151151}
152152
153153our ($OUTPUT_HELP_VERSION , $STANDARD_HELP_VERSION );
154154sub output_h () {
155- return $OUTPUT_HELP_VERSION if defined $OUTPUT_HELP_VERSION ;
156- return \*STDOUT if $STANDARD_HELP_VERSION ;
157- return \*STDERR ;
155+ return $OUTPUT_HELP_VERSION if defined $OUTPUT_HELP_VERSION ;
156+ return \*STDOUT if $STANDARD_HELP_VERSION ;
157+ return \*STDERR ;
158158}
159159
160160sub try_exit () {
@@ -170,15 +170,16 @@ sub version_mess ($;$) {
170170 my $args = shift ;
171171 my $h = output_h;
172172 if (@_ and defined &main::VERSION_MESSAGE) {
173- main::VERSION_MESSAGE($h , __PACKAGE__ , $VERSION , $args );
174- } else {
175- my $v = $main::VERSION ;
176- $v = ' [unknown]' unless defined $v ;
177- my $myv = $VERSION ;
178- $myv .= ' [paranoid]' unless $STANDARD_HELP_VERSION ;
179- my $perlv = $] ;
180- $perlv = sprintf " %vd " , $^V if $] >= 5.006;
181- print $h <<EOH ;
173+ main::VERSION_MESSAGE($h , __PACKAGE__ , $VERSION , $args );
174+ }
175+ else {
176+ my $v = $main::VERSION ;
177+ $v = ' [unknown]' unless defined $v ;
178+ my $myv = $VERSION ;
179+ $myv .= ' [paranoid]' unless $STANDARD_HELP_VERSION ;
180+ my $perlv = $] ;
181+ $perlv = sprintf " %vd " , $^V if $] >= 5.006;
182+ print $h <<EOH ;
182183$0 version $v calling Getopt::Std::getopts (version $myv ),
183184running under Perl version $perlv .
184185EOH
@@ -189,47 +190,48 @@ sub help_mess ($;$) {
189190 my $args = shift ;
190191 my $h = output_h;
191192 if (@_ and defined &main::HELP_MESSAGE) {
192- main::HELP_MESSAGE($h , __PACKAGE__ , $VERSION , $args );
193- } else {
194- my (@witharg ) = ($args =~ / (\S )\s *:/g );
195- my (@rest ) = ($args =~ / ([^\s :])(?!\s *:)/g );
196- my ($help , $arg ) = (' ' , ' ' );
197- if (@witharg ) {
198- $help .= " \n\t With arguments: -" . join " -" , @witharg ;
199- $arg = " \n Space is not required between options and their arguments." ;
200- }
201- if (@rest ) {
202- $help .= " \n\t Boolean (without arguments): -" . join " -" , @rest ;
203- }
204- my ($scr ) = ($0 =~ m , ([^/\\ ]+)$ , );
205- print $h <<EOH if @_ ; # Let the script override this
193+ main::HELP_MESSAGE($h , __PACKAGE__ , $VERSION , $args );
194+ }
195+ else {
196+ my (@witharg ) = ($args =~ / (\S )\s *:/g );
197+ my (@rest ) = ($args =~ / ([^\s :])(?!\s *:)/g );
198+ my ($help , $arg ) = (' ' , ' ' );
199+ if (@witharg ) {
200+ $help .= " \n\t With arguments: -" . join " -" , @witharg ;
201+ $arg = " \n Space is not required between options and their arguments." ;
202+ }
203+ if (@rest ) {
204+ $help .= " \n\t Boolean (without arguments): -" . join " -" , @rest ;
205+ }
206+ my ($scr ) = ($0 =~ m , ([^/\\ ]+)$ , );
207+ print $h <<EOH if @_ ; # Let the script override this
206208
207209Usage: $scr [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_ARG1 ...]
208210EOH
209- print $h <<EOH ;
211+ print $h <<EOH ;
210212
211213The following single-character options are accepted:$help
212214
213215Options may be merged together. -- stops processing of options.$arg
214216EOH
215- my $has_pod ;
216- if ( defined $0 and $0 ne ' -e' and -f $0 and -r $0
217- and open my $script , ' <' , $0 ) {
218- while (<$script >) {
219- $has_pod = 1, last if / ^=(pod|head1)/ ;
220- }
221- }
222- print $h <<EOH if $has_pod ;
217+ my $has_pod ;
218+ if ( defined $0 and $0 ne ' -e' and -f $0 and -r $0
219+ and open my $script , ' <' , $0 ) {
220+ while (<$script >) {
221+ $has_pod = 1, last if / ^=(pod|head1)/ ;
222+ }
223+ }
224+ print $h <<EOH if $has_pod ;
223225
224226For more details run
225- perldoc -F $0
227+ perldoc -F $0
226228EOH
227229 }
228230}
229231
230232# Usage:
231- # getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
232- # # side effect.
233+ # getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
234+ # # side effect.
233235
234236sub getopts ($;$) {
235237 my ($argumentative , $hash ) = @_ ;
@@ -240,71 +242,72 @@ sub getopts ($;$) {
240242
241243 @args = split ( / */ , $argumentative );
242244 while (@ARGV && ($_ = $ARGV [0]) =~ / ^-(.)(.*)/s ) {
243- ($first , $rest ) = ($1 , $2 );
244- if (/ ^--$ / ) { # early exit if --
245- shift @ARGV ;
246- last ;
247- }
248- my $pos = index ($argumentative , $first );
249- if ($pos >= 0) {
250- if (defined ($args [$pos +1]) and ($args [$pos +1] eq ' :' )) {
251- shift (@ARGV );
252- if ($rest eq ' ' ) {
253- ++$errs unless @ARGV ;
254- $rest = shift (@ARGV );
255- }
256- if (ref $hash ) {
257- $$hash {$first } = $rest ;
258- }
259- else {
260- no strict ' refs' ;
261- ${" opt_$first " } = $rest ;
262- push ( @EXPORT , " \$ opt_$first " );
263- }
264- }
265- else {
266- if (ref $hash ) {
267- $$hash {$first } = 1;
268- }
269- else {
270- no strict ' refs' ;
271- ${" opt_$first " } = 1;
272- push ( @EXPORT , " \$ opt_$first " );
273- }
274- if ($rest eq ' ' ) {
275- shift (@ARGV );
276- }
277- else {
278- $ARGV [0] = " -$rest " ;
279- }
280- }
281- }
282- else {
283- if ($first eq ' -' and $rest eq ' help' ) {
284- version_mess($argumentative , ' main' );
285- help_mess($argumentative , ' main' );
286- try_exit();
287- shift (@ARGV );
288- next ;
289- } elsif ($first eq ' -' and $rest eq ' version' ) {
290- version_mess($argumentative , ' main' );
291- try_exit();
292- shift (@ARGV );
293- next ;
294- }
295- warn " Unknown option: $first \n " ;
296- ++$errs ;
297- if ($rest ne ' ' ) {
298- $ARGV [0] = " -$rest " ;
299- }
300- else {
301- shift (@ARGV );
302- }
303- }
245+ ($first , $rest ) = ($1 , $2 );
246+ if (/ ^--$ / ) { # early exit if --
247+ shift @ARGV ;
248+ last ;
249+ }
250+ my $pos = index ($argumentative , $first );
251+ if ($pos >= 0) {
252+ if (defined ($args [$pos +1]) and ($args [$pos +1] eq ' :' )) {
253+ shift (@ARGV );
254+ if ($rest eq ' ' ) {
255+ ++$errs unless @ARGV ;
256+ $rest = shift (@ARGV );
257+ }
258+ if (ref $hash ) {
259+ $$hash {$first } = $rest ;
260+ }
261+ else {
262+ no strict ' refs' ;
263+ ${" opt_$first " } = $rest ;
264+ push ( @EXPORT , " \$ opt_$first " );
265+ }
266+ }
267+ else {
268+ if (ref $hash ) {
269+ $$hash {$first } = 1;
270+ }
271+ else {
272+ no strict ' refs' ;
273+ ${" opt_$first " } = 1;
274+ push ( @EXPORT , " \$ opt_$first " );
275+ }
276+ if ($rest eq ' ' ) {
277+ shift (@ARGV );
278+ }
279+ else {
280+ $ARGV [0] = " -$rest " ;
281+ }
282+ }
283+ }
284+ else {
285+ if ($first eq ' -' and $rest eq ' help' ) {
286+ version_mess($argumentative , ' main' );
287+ help_mess($argumentative , ' main' );
288+ try_exit();
289+ shift (@ARGV );
290+ next ;
291+ }
292+ elsif ($first eq ' -' and $rest eq ' version' ) {
293+ version_mess($argumentative , ' main' );
294+ try_exit();
295+ shift (@ARGV );
296+ next ;
297+ }
298+ warn " Unknown option: $first \n " ;
299+ ++$errs ;
300+ if ($rest ne ' ' ) {
301+ $ARGV [0] = " -$rest " ;
302+ }
303+ else {
304+ shift (@ARGV );
305+ }
306+ }
304307 }
305- unless (ref $hash ) {
306- local $Exporter::ExportLevel = 1;
307- Getopt::Std-> import ;
308+ unless (ref $hash ) {
309+ local $Exporter::ExportLevel = 1;
310+ Getopt::Std-> import ;
308311 }
309312 $errs == 0;
310313}
0 commit comments