Commit | Line | Data |
a11f5414 |
1 | # GetOpt::Long.pm -- Universal options parsing |
404cbe93 |
2 | |
a11f5414 |
3 | package Getopt::Long; |
4 | |
10e5c9cc |
5 | # RCS Status : $Id: GetoptLong.pl,v 2.24 2000-03-14 21:28:52+01 jv Exp jv $ |
404cbe93 |
6 | # Author : Johan Vromans |
7 | # Created On : Tue Sep 11 15:00:12 1990 |
8 | # Last Modified By: Johan Vromans |
10e5c9cc |
9 | # Last Modified On: Mon Jul 31 21:21:13 2000 |
10 | # Update Count : 739 |
404cbe93 |
11 | # Status : Released |
12 | |
bb40d378 |
13 | ################ Copyright ################ |
f06db76b |
14 | |
0b7031a2 |
15 | # This program is Copyright 1990,2000 by Johan Vromans. |
bb40d378 |
16 | # This program is free software; you can redistribute it and/or |
1a505819 |
17 | # modify it under the terms of the Perl Artistic License or the |
18 | # GNU General Public License as published by the Free Software |
19 | # Foundation; either version 2 of the License, or (at your option) any |
20 | # later version. |
21 | # |
bb40d378 |
22 | # This program is distributed in the hope that it will be useful, |
23 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 | # GNU General Public License for more details. |
0b7031a2 |
26 | # |
bb40d378 |
27 | # If you do not have a copy of the GNU General Public License write to |
0b7031a2 |
28 | # the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, |
f9a400e4 |
29 | # MA 02139, USA. |
f06db76b |
30 | |
bb40d378 |
31 | ################ Module Preamble ################ |
404cbe93 |
32 | |
bb40d378 |
33 | use strict; |
404cbe93 |
34 | |
bb40d378 |
35 | BEGIN { |
3a0431da |
36 | require 5.004; |
bb40d378 |
37 | use Exporter (); |
e6d5c530 |
38 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
c3d5f609 |
39 | $VERSION = 2.23_05; |
e6d5c530 |
40 | |
41 | @ISA = qw(Exporter); |
42 | @EXPORT = qw(&GetOptions $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER); |
43 | %EXPORT_TAGS = qw(); |
44 | @EXPORT_OK = qw(); |
45 | use AutoLoader qw(AUTOLOAD); |
bb40d378 |
46 | } |
404cbe93 |
47 | |
bb40d378 |
48 | # User visible variables. |
e6d5c530 |
49 | use vars @EXPORT, @EXPORT_OK; |
bb40d378 |
50 | use vars qw($error $debug $major_version $minor_version); |
51 | # Deprecated visible variables. |
52 | use vars qw($autoabbrev $getopt_compat $ignorecase $bundling $order |
53 | $passthrough); |
e6d5c530 |
54 | # Official invisible variables. |
10e5c9cc |
55 | use vars qw($genprefix $caller $gnu_compat); |
e6d5c530 |
56 | |
0b7031a2 |
57 | # Public subroutines. |
e6d5c530 |
58 | sub Configure (@); |
59 | sub config (@); # deprecated name |
60 | sub GetOptions; |
61 | |
0b7031a2 |
62 | # Private subroutines. |
e6d5c530 |
63 | sub ConfigDefaults (); |
64 | sub FindOption ($$$$$$$); |
65 | sub Croak (@); # demand loading the real Croak |
404cbe93 |
66 | |
bb40d378 |
67 | ################ Local Variables ################ |
404cbe93 |
68 | |
e6d5c530 |
69 | ################ Resident subroutines ################ |
70 | |
71 | sub ConfigDefaults () { |
72 | # Handle POSIX compliancy. |
73 | if ( defined $ENV{"POSIXLY_CORRECT"} ) { |
74 | $genprefix = "(--|-)"; |
75 | $autoabbrev = 0; # no automatic abbrev of options |
76 | $bundling = 0; # no bundling of single letter switches |
77 | $getopt_compat = 0; # disallow '+' to start options |
78 | $order = $REQUIRE_ORDER; |
79 | } |
80 | else { |
81 | $genprefix = "(--|-|\\+)"; |
82 | $autoabbrev = 1; # automatic abbrev of options |
83 | $bundling = 0; # bundling off by default |
84 | $getopt_compat = 1; # allow '+' to start options |
85 | $order = $PERMUTE; |
86 | } |
87 | # Other configurable settings. |
88 | $debug = 0; # for debugging |
89 | $error = 0; # error tally |
90 | $ignorecase = 1; # ignore case when matching options |
91 | $passthrough = 0; # leave unrecognized options alone |
10e5c9cc |
92 | $gnu_compat = 0; # require --opt=val if value is optional |
93 | } |
94 | |
95 | # Override import. |
96 | sub import { |
97 | my $pkg = shift; # package |
98 | my @syms = (); # symbols to import |
99 | my @config = (); # configuration |
100 | my $dest = \@syms; # symbols first |
101 | for ( @_ ) { |
102 | if ( $_ eq ':config' ) { |
103 | $dest = \@config; # config next |
104 | next; |
105 | } |
106 | push (@$dest, $_); # push |
107 | } |
108 | # Hide one level and call super. |
109 | local $Exporter::ExportLevel = 1; |
110 | $pkg->SUPER::import(@syms); |
111 | # And configure. |
112 | Configure (@config) if @config; |
e6d5c530 |
113 | } |
114 | |
115 | ################ Initialization ################ |
116 | |
117 | # Values for $order. See GNU getopt.c for details. |
118 | ($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER) = (0..2); |
119 | # Version major/minor numbers. |
120 | ($major_version, $minor_version) = $VERSION =~ /^(\d+)\.(\d+)/; |
121 | |
0b7031a2 |
122 | ConfigDefaults(); |
123 | |
10e5c9cc |
124 | ################ OO Interface ################ |
125 | |
126 | package Getopt::Long::Parser; |
127 | |
128 | # NOTE: The object oriented routines use $error for thread locking. |
129 | my $_lock = sub { |
130 | lock ($Getopt::Long::error) if $] >= 5.005 |
131 | }; |
132 | |
133 | # Store a copy of the default configuration. Since ConfigDefaults has |
134 | # just been called, what we get from Configure is the default. |
135 | my $default_config = do { |
136 | &$_lock; |
137 | Getopt::Long::Configure () |
138 | }; |
139 | |
140 | sub new { |
141 | my $that = shift; |
142 | my $class = ref($that) || $that; |
143 | my %atts = @_; |
144 | |
145 | # Register the callers package. |
146 | my $self = { caller => (caller)[0] }; |
147 | |
148 | bless ($self, $class); |
149 | |
150 | # Process config attributes. |
151 | if ( defined $atts{config} ) { |
152 | &$_lock; |
153 | my $save = Getopt::Long::Configure ($default_config, @{$atts{config}}); |
154 | $self->{settings} = Getopt::Long::Configure ($save); |
155 | delete ($atts{config}); |
156 | } |
157 | # Else use default config. |
158 | else { |
159 | $self->{settings} = $default_config; |
160 | } |
161 | |
162 | if ( %atts ) { # Oops |
163 | Getopt::Long::Croak(__PACKAGE__.": unhandled attributes: ". |
164 | join(" ", sort(keys(%atts)))); |
165 | } |
166 | |
167 | $self; |
168 | } |
169 | |
170 | sub configure { |
171 | my ($self) = shift; |
172 | |
173 | &$_lock; |
174 | |
175 | # Restore settings, merge new settings in. |
176 | my $save = Getopt::Long::Configure ($self->{settings}, @_); |
177 | |
178 | # Restore orig config and save the new config. |
179 | $self->{settings} = Configure ($save); |
180 | } |
181 | |
182 | sub getoptions { |
183 | my ($self) = shift; |
184 | |
185 | &$_lock; |
186 | |
187 | # Restore config settings. |
188 | my $save = Getopt::Long::Configure ($self->{settings}); |
189 | |
190 | # Call main routine. |
191 | my $ret = 0; |
192 | $Getopt::Long::caller = $self->{caller}; |
193 | eval { $ret = Getopt::Long::GetOptions (@_); }; |
194 | |
195 | # Restore saved settings. |
196 | Getopt::Long::Configure ($save); |
197 | |
198 | # Handle errors and return value. |
199 | die ($@) if $@; |
200 | return $ret; |
201 | } |
202 | |
203 | package Getopt::Long; |
204 | |
e6d5c530 |
205 | ################ Package return ################ |
206 | |
207 | 1; |
208 | |
209 | __END__ |
210 | |
211 | ################ AutoLoading subroutines ################ |
212 | |
10e5c9cc |
213 | # RCS Status : $Id: GetoptLongAl.pl,v 2.28 2000-05-12 11:26:41+02 jv Exp jv $ |
e6d5c530 |
214 | # Author : Johan Vromans |
215 | # Created On : Fri Mar 27 11:50:30 1998 |
216 | # Last Modified By: Johan Vromans |
10e5c9cc |
217 | # Last Modified On: Fri Jul 28 19:12:29 2000 |
218 | # Update Count : 97 |
e6d5c530 |
219 | # Status : Released |
404cbe93 |
220 | |
bb40d378 |
221 | sub GetOptions { |
404cbe93 |
222 | |
bb40d378 |
223 | my @optionlist = @_; # local copy of the option descriptions |
e6d5c530 |
224 | my $argend = '--'; # option list terminator |
225 | my %opctl = (); # table of arg.specs (long and abbrevs) |
226 | my %bopctl = (); # table of arg.specs (bundles) |
0b7031a2 |
227 | my $pkg = $caller || (caller)[0]; # current context |
bb40d378 |
228 | # Needed if linkage is omitted. |
e6d5c530 |
229 | my %aliases= (); # alias table |
bb40d378 |
230 | my @ret = (); # accum for non-options |
231 | my %linkage; # linkage |
232 | my $userlinkage; # user supplied HASH |
e6d5c530 |
233 | my $opt; # current option |
234 | my $genprefix = $genprefix; # so we can call the same module many times |
235 | my @opctl; # the possible long option names |
236 | |
bb40d378 |
237 | $error = ''; |
404cbe93 |
238 | |
e6d5c530 |
239 | print STDERR ("GetOpt::Long $Getopt::Long::VERSION ", |
240 | "called from package \"$pkg\".", |
241 | "\n ", |
10e5c9cc |
242 | 'GetOptionsAl $Revision: 2.28 $ ', |
e6d5c530 |
243 | "\n ", |
244 | "ARGV: (@ARGV)", |
245 | "\n ", |
246 | "autoabbrev=$autoabbrev,". |
247 | "bundling=$bundling,", |
248 | "getopt_compat=$getopt_compat,", |
10e5c9cc |
249 | "gnu_compat=$gnu_compat,", |
e6d5c530 |
250 | "order=$order,", |
251 | "\n ", |
252 | "ignorecase=$ignorecase,", |
253 | "passthrough=$passthrough,", |
254 | "genprefix=\"$genprefix\".", |
255 | "\n") |
bb40d378 |
256 | if $debug; |
404cbe93 |
257 | |
0b7031a2 |
258 | # Check for ref HASH as first argument. |
bb40d378 |
259 | # First argument may be an object. It's OK to use this as long |
0b7031a2 |
260 | # as it is really a hash underneath. |
bb40d378 |
261 | $userlinkage = undef; |
262 | if ( ref($optionlist[0]) and |
263 | "$optionlist[0]" =~ /^(?:.*\=)?HASH\([^\(]*\)$/ ) { |
264 | $userlinkage = shift (@optionlist); |
265 | print STDERR ("=> user linkage: $userlinkage\n") if $debug; |
266 | } |
404cbe93 |
267 | |
bb40d378 |
268 | # See if the first element of the optionlist contains option |
269 | # starter characters. |
1a505819 |
270 | # Be careful not to interpret '<>' as option starters. |
271 | if ( $optionlist[0] =~ /^\W+$/ |
272 | && !($optionlist[0] eq '<>' |
273 | && @optionlist > 0 |
274 | && ref($optionlist[1])) ) { |
bb40d378 |
275 | $genprefix = shift (@optionlist); |
276 | # Turn into regexp. Needs to be parenthesized! |
277 | $genprefix =~ s/(\W)/\\$1/g; |
278 | $genprefix = "([" . $genprefix . "])"; |
279 | } |
404cbe93 |
280 | |
bb40d378 |
281 | # Verify correctness of optionlist. |
282 | %opctl = (); |
283 | %bopctl = (); |
284 | while ( @optionlist > 0 ) { |
285 | my $opt = shift (@optionlist); |
404cbe93 |
286 | |
bb40d378 |
287 | # Strip leading prefix so people can specify "--foo=i" if they like. |
3a0431da |
288 | $opt = $+ if $opt =~ /^$genprefix+(.*)$/s; |
404cbe93 |
289 | |
bb40d378 |
290 | if ( $opt eq '<>' ) { |
291 | if ( (defined $userlinkage) |
292 | && !(@optionlist > 0 && ref($optionlist[0])) |
293 | && (exists $userlinkage->{$opt}) |
294 | && ref($userlinkage->{$opt}) ) { |
295 | unshift (@optionlist, $userlinkage->{$opt}); |
296 | } |
0b7031a2 |
297 | unless ( @optionlist > 0 |
bb40d378 |
298 | && ref($optionlist[0]) && ref($optionlist[0]) eq 'CODE' ) { |
299 | $error .= "Option spec <> requires a reference to a subroutine\n"; |
300 | next; |
301 | } |
302 | $linkage{'<>'} = shift (@optionlist); |
303 | next; |
304 | } |
404cbe93 |
305 | |
10e5c9cc |
306 | # Match option spec. Allow '?' as an alias only. |
e6d5c530 |
307 | if ( $opt !~ /^((\w+[-\w]*)(\|(\?|\w[-\w]*)?)*)?([!~+]|[=:][infse][@%]?)?$/ ) { |
bb40d378 |
308 | $error .= "Error in option spec: \"$opt\"\n"; |
309 | next; |
310 | } |
311 | my ($o, $c, $a) = ($1, $5); |
312 | $c = '' unless defined $c; |
404cbe93 |
313 | |
10e5c9cc |
314 | # $linko keeps track of the primary name the user specified. |
315 | # This name will be used for the internal or external linkage. |
316 | # In other words, if the user specifies "FoO|BaR", it will |
317 | # match any case combinations of 'foo' and 'bar', but if a global |
318 | # variable needs to be set, it will be $opt_FoO in the exact case |
319 | # as specified. |
320 | my $linko; |
321 | |
bb40d378 |
322 | if ( ! defined $o ) { |
323 | # empty -> '-' option |
10e5c9cc |
324 | $opctl{$linko = $o = ''} = $c; |
bb40d378 |
325 | } |
326 | else { |
327 | # Handle alias names |
328 | my @o = split (/\|/, $o); |
10e5c9cc |
329 | $linko = $o = $o[0]; |
bb40d378 |
330 | # Force an alias if the option name is not locase. |
331 | $a = $o unless $o eq lc($o); |
332 | $o = lc ($o) |
0b7031a2 |
333 | if $ignorecase > 1 |
bb40d378 |
334 | || ($ignorecase |
335 | && ($bundling ? length($o) > 1 : 1)); |
404cbe93 |
336 | |
bb40d378 |
337 | foreach ( @o ) { |
338 | if ( $bundling && length($_) == 1 ) { |
339 | $_ = lc ($_) if $ignorecase > 1; |
340 | if ( $c eq '!' ) { |
341 | $opctl{"no$_"} = $c; |
342 | warn ("Ignoring '!' modifier for short option $_\n"); |
265c41c2 |
343 | $opctl{$_} = $bopctl{$_} = ''; |
344 | } |
345 | else { |
346 | $opctl{$_} = $bopctl{$_} = $c; |
bb40d378 |
347 | } |
bb40d378 |
348 | } |
349 | else { |
350 | $_ = lc ($_) if $ignorecase; |
351 | if ( $c eq '!' ) { |
352 | $opctl{"no$_"} = $c; |
265c41c2 |
353 | $opctl{$_} = '' |
354 | } |
355 | else { |
356 | $opctl{$_} = $c; |
bb40d378 |
357 | } |
bb40d378 |
358 | } |
359 | if ( defined $a ) { |
360 | # Note alias. |
361 | $aliases{$_} = $a; |
362 | } |
363 | else { |
364 | # Set primary name. |
365 | $a = $_; |
366 | } |
367 | } |
bb40d378 |
368 | } |
404cbe93 |
369 | |
bb40d378 |
370 | # If no linkage is supplied in the @optionlist, copy it from |
371 | # the userlinkage if available. |
372 | if ( defined $userlinkage ) { |
373 | unless ( @optionlist > 0 && ref($optionlist[0]) ) { |
10e5c9cc |
374 | if ( exists $userlinkage->{$linko} && |
375 | ref($userlinkage->{$linko}) ) { |
376 | print STDERR ("=> found userlinkage for \"$linko\": ", |
377 | "$userlinkage->{$linko}\n") |
bb40d378 |
378 | if $debug; |
10e5c9cc |
379 | unshift (@optionlist, $userlinkage->{$linko}); |
bb40d378 |
380 | } |
381 | else { |
382 | # Do nothing. Being undefined will be handled later. |
383 | next; |
384 | } |
385 | } |
386 | } |
404cbe93 |
387 | |
bb40d378 |
388 | # Copy the linkage. If omitted, link to global variable. |
389 | if ( @optionlist > 0 && ref($optionlist[0]) ) { |
10e5c9cc |
390 | print STDERR ("=> link \"$linko\" to $optionlist[0]\n") |
bb40d378 |
391 | if $debug; |
392 | if ( ref($optionlist[0]) =~ /^(SCALAR|CODE)$/ ) { |
10e5c9cc |
393 | $linkage{$linko} = shift (@optionlist); |
bb40d378 |
394 | } |
395 | elsif ( ref($optionlist[0]) =~ /^(ARRAY)$/ ) { |
10e5c9cc |
396 | $linkage{$linko} = shift (@optionlist); |
bb40d378 |
397 | $opctl{$o} .= '@' |
398 | if $opctl{$o} ne '' and $opctl{$o} !~ /\@$/; |
399 | $bopctl{$o} .= '@' |
0b7031a2 |
400 | if $bundling and defined $bopctl{$o} and |
bb40d378 |
401 | $bopctl{$o} ne '' and $bopctl{$o} !~ /\@$/; |
402 | } |
403 | elsif ( ref($optionlist[0]) =~ /^(HASH)$/ ) { |
10e5c9cc |
404 | $linkage{$linko} = shift (@optionlist); |
bb40d378 |
405 | $opctl{$o} .= '%' |
406 | if $opctl{$o} ne '' and $opctl{$o} !~ /\%$/; |
407 | $bopctl{$o} .= '%' |
0b7031a2 |
408 | if $bundling and defined $bopctl{$o} and |
bb40d378 |
409 | $bopctl{$o} ne '' and $bopctl{$o} !~ /\%$/; |
410 | } |
411 | else { |
412 | $error .= "Invalid option linkage for \"$opt\"\n"; |
413 | } |
414 | } |
415 | else { |
416 | # Link to global $opt_XXX variable. |
417 | # Make sure a valid perl identifier results. |
10e5c9cc |
418 | my $ov = $linko; |
bb40d378 |
419 | $ov =~ s/\W/_/g; |
420 | if ( $c =~ /@/ ) { |
10e5c9cc |
421 | print STDERR ("=> link \"$linko\" to \@$pkg","::opt_$ov\n") |
bb40d378 |
422 | if $debug; |
10e5c9cc |
423 | eval ("\$linkage{\$linko} = \\\@".$pkg."::opt_$ov;"); |
bb40d378 |
424 | } |
425 | elsif ( $c =~ /%/ ) { |
10e5c9cc |
426 | print STDERR ("=> link \"$linko\" to \%$pkg","::opt_$ov\n") |
bb40d378 |
427 | if $debug; |
10e5c9cc |
428 | eval ("\$linkage{\$linko} = \\\%".$pkg."::opt_$ov;"); |
bb40d378 |
429 | } |
430 | else { |
10e5c9cc |
431 | print STDERR ("=> link \"$linko\" to \$$pkg","::opt_$ov\n") |
bb40d378 |
432 | if $debug; |
10e5c9cc |
433 | eval ("\$linkage{\$linko} = \\\$".$pkg."::opt_$ov;"); |
bb40d378 |
434 | } |
435 | } |
436 | } |
437 | |
438 | # Bail out if errors found. |
439 | die ($error) if $error; |
440 | $error = 0; |
441 | |
442 | # Sort the possible long option names. |
443 | @opctl = sort(keys (%opctl)) if $autoabbrev; |
444 | |
445 | # Show the options tables if debugging. |
446 | if ( $debug ) { |
447 | my ($arrow, $k, $v); |
448 | $arrow = "=> "; |
449 | while ( ($k,$v) = each(%opctl) ) { |
450 | print STDERR ($arrow, "\$opctl{\"$k\"} = \"$v\"\n"); |
451 | $arrow = " "; |
452 | } |
453 | $arrow = "=> "; |
454 | while ( ($k,$v) = each(%bopctl) ) { |
455 | print STDERR ($arrow, "\$bopctl{\"$k\"} = \"$v\"\n"); |
456 | $arrow = " "; |
457 | } |
458 | } |
459 | |
460 | # Process argument list |
0b7031a2 |
461 | my $goon = 1; |
462 | while ( $goon && @ARGV > 0 ) { |
bb40d378 |
463 | |
464 | #### Get next argument #### |
465 | |
466 | $opt = shift (@ARGV); |
bb40d378 |
467 | print STDERR ("=> option \"", $opt, "\"\n") if $debug; |
468 | |
469 | #### Determine what we have #### |
470 | |
471 | # Double dash is option list terminator. |
472 | if ( $opt eq $argend ) { |
473 | # Finish. Push back accumulated arguments and return. |
0b7031a2 |
474 | unshift (@ARGV, @ret) |
bb40d378 |
475 | if $order == $PERMUTE; |
476 | return ($error == 0); |
477 | } |
478 | |
479 | my $tryopt = $opt; |
e6d5c530 |
480 | my $found; # success status |
481 | my $dsttype; # destination type ('@' or '%') |
0b7031a2 |
482 | my $incr; # destination increment |
e6d5c530 |
483 | my $key; # key (if hash type) |
484 | my $arg; # option argument |
485 | |
0b7031a2 |
486 | ($found, $opt, $arg, $dsttype, $incr, $key) = |
487 | FindOption ($genprefix, $argend, $opt, |
e6d5c530 |
488 | \%opctl, \%bopctl, \@opctl, \%aliases); |
bb40d378 |
489 | |
e6d5c530 |
490 | if ( $found ) { |
0b7031a2 |
491 | |
e6d5c530 |
492 | # FindOption undefines $opt in case of errors. |
bb40d378 |
493 | next unless defined $opt; |
494 | |
495 | if ( defined $arg ) { |
10e5c9cc |
496 | if ( defined $aliases{$opt} ) { |
497 | print STDERR ("=> alias \"$opt\" -> \"$aliases{$opt}\"\n") |
498 | if $debug; |
499 | $opt = $aliases{$opt}; |
500 | } |
bb40d378 |
501 | |
502 | if ( defined $linkage{$opt} ) { |
503 | print STDERR ("=> ref(\$L{$opt}) -> ", |
504 | ref($linkage{$opt}), "\n") if $debug; |
505 | |
506 | if ( ref($linkage{$opt}) eq 'SCALAR' ) { |
e6d5c530 |
507 | if ( $incr ) { |
508 | print STDERR ("=> \$\$L{$opt} += \"$arg\"\n") |
509 | if $debug; |
510 | if ( defined ${$linkage{$opt}} ) { |
511 | ${$linkage{$opt}} += $arg; |
512 | } |
513 | else { |
514 | ${$linkage{$opt}} = $arg; |
515 | } |
516 | } |
517 | else { |
518 | print STDERR ("=> \$\$L{$opt} = \"$arg\"\n") |
519 | if $debug; |
520 | ${$linkage{$opt}} = $arg; |
521 | } |
bb40d378 |
522 | } |
523 | elsif ( ref($linkage{$opt}) eq 'ARRAY' ) { |
524 | print STDERR ("=> push(\@{\$L{$opt}, \"$arg\")\n") |
525 | if $debug; |
526 | push (@{$linkage{$opt}}, $arg); |
527 | } |
528 | elsif ( ref($linkage{$opt}) eq 'HASH' ) { |
529 | print STDERR ("=> \$\$L{$opt}->{$key} = \"$arg\"\n") |
530 | if $debug; |
531 | $linkage{$opt}->{$key} = $arg; |
532 | } |
533 | elsif ( ref($linkage{$opt}) eq 'CODE' ) { |
534 | print STDERR ("=> &L{$opt}(\"$opt\", \"$arg\")\n") |
535 | if $debug; |
0b7031a2 |
536 | local ($@); |
537 | eval { |
538 | &{$linkage{$opt}}($opt, $arg); |
539 | }; |
540 | print STDERR ("=> die($@)\n") if $debug && $@ ne ''; |
bee0ef1e |
541 | if ( $@ =~ /^!/ ) { |
542 | if ( $@ =~ /^!FINISH\b/ ) { |
543 | $goon = 0; |
544 | } |
0b7031a2 |
545 | } |
546 | elsif ( $@ ne '' ) { |
547 | warn ($@); |
548 | $error++; |
549 | } |
bb40d378 |
550 | } |
551 | else { |
552 | print STDERR ("Invalid REF type \"", ref($linkage{$opt}), |
553 | "\" in linkage\n"); |
e6d5c530 |
554 | Croak ("Getopt::Long -- internal error!\n"); |
bb40d378 |
555 | } |
556 | } |
557 | # No entry in linkage means entry in userlinkage. |
e6d5c530 |
558 | elsif ( $dsttype eq '@' ) { |
bb40d378 |
559 | if ( defined $userlinkage->{$opt} ) { |
560 | print STDERR ("=> push(\@{\$L{$opt}}, \"$arg\")\n") |
561 | if $debug; |
562 | push (@{$userlinkage->{$opt}}, $arg); |
563 | } |
564 | else { |
565 | print STDERR ("=>\$L{$opt} = [\"$arg\"]\n") |
566 | if $debug; |
567 | $userlinkage->{$opt} = [$arg]; |
568 | } |
569 | } |
e6d5c530 |
570 | elsif ( $dsttype eq '%' ) { |
bb40d378 |
571 | if ( defined $userlinkage->{$opt} ) { |
572 | print STDERR ("=> \$L{$opt}->{$key} = \"$arg\"\n") |
573 | if $debug; |
574 | $userlinkage->{$opt}->{$key} = $arg; |
575 | } |
576 | else { |
577 | print STDERR ("=>\$L{$opt} = {$key => \"$arg\"}\n") |
578 | if $debug; |
579 | $userlinkage->{$opt} = {$key => $arg}; |
580 | } |
581 | } |
582 | else { |
e6d5c530 |
583 | if ( $incr ) { |
584 | print STDERR ("=> \$L{$opt} += \"$arg\"\n") |
585 | if $debug; |
586 | if ( defined $userlinkage->{$opt} ) { |
587 | $userlinkage->{$opt} += $arg; |
588 | } |
589 | else { |
590 | $userlinkage->{$opt} = $arg; |
591 | } |
592 | } |
593 | else { |
594 | print STDERR ("=>\$L{$opt} = \"$arg\"\n") if $debug; |
595 | $userlinkage->{$opt} = $arg; |
596 | } |
bb40d378 |
597 | } |
598 | } |
599 | } |
600 | |
601 | # Not an option. Save it if we $PERMUTE and don't have a <>. |
602 | elsif ( $order == $PERMUTE ) { |
603 | # Try non-options call-back. |
604 | my $cb; |
605 | if ( (defined ($cb = $linkage{'<>'})) ) { |
0b7031a2 |
606 | local ($@); |
607 | eval { |
608 | &$cb ($tryopt); |
609 | }; |
610 | print STDERR ("=> die($@)\n") if $debug && $@ ne ''; |
bee0ef1e |
611 | if ( $@ =~ /^!/ ) { |
612 | if ( $@ =~ /^!FINISH\b/ ) { |
613 | $goon = 0; |
614 | } |
0b7031a2 |
615 | } |
616 | elsif ( $@ ne '' ) { |
617 | warn ($@); |
618 | $error++; |
619 | } |
bb40d378 |
620 | } |
621 | else { |
622 | print STDERR ("=> saving \"$tryopt\" ", |
623 | "(not an option, may permute)\n") if $debug; |
624 | push (@ret, $tryopt); |
625 | } |
626 | next; |
627 | } |
628 | |
629 | # ...otherwise, terminate. |
630 | else { |
631 | # Push this one back and exit. |
632 | unshift (@ARGV, $tryopt); |
633 | return ($error == 0); |
634 | } |
635 | |
636 | } |
637 | |
638 | # Finish. |
639 | if ( $order == $PERMUTE ) { |
640 | # Push back accumulated arguments |
641 | print STDERR ("=> restoring \"", join('" "', @ret), "\"\n") |
642 | if $debug && @ret > 0; |
643 | unshift (@ARGV, @ret) if @ret > 0; |
644 | } |
645 | |
646 | return ($error == 0); |
647 | } |
648 | |
e6d5c530 |
649 | # Option lookup. |
650 | sub FindOption ($$$$$$$) { |
bb40d378 |
651 | |
e6d5c530 |
652 | # returns (1, $opt, $arg, $dsttype, $incr, $key) if okay, |
653 | # returns (0) otherwise. |
bb40d378 |
654 | |
e6d5c530 |
655 | my ($prefix, $argend, $opt, $opctl, $bopctl, $names, $aliases) = @_; |
656 | my $key; # hash key for a hash option |
657 | my $arg; |
bb40d378 |
658 | |
e6d5c530 |
659 | print STDERR ("=> find \"$opt\", prefix=\"$prefix\"\n") if $debug; |
bb40d378 |
660 | |
e6d5c530 |
661 | return (0) unless $opt =~ /^$prefix(.*)$/s; |
bb40d378 |
662 | |
3a0431da |
663 | $opt = $+; |
bb40d378 |
664 | my ($starter) = $1; |
665 | |
666 | print STDERR ("=> split \"$starter\"+\"$opt\"\n") if $debug; |
667 | |
668 | my $optarg = undef; # value supplied with --opt=value |
669 | my $rest = undef; # remainder from unbundling |
670 | |
671 | # If it is a long option, it may include the value. |
672 | if (($starter eq "--" || ($getopt_compat && !$bundling)) |
3a0431da |
673 | && $opt =~ /^([^=]+)=(.*)$/s ) { |
bb40d378 |
674 | $opt = $1; |
675 | $optarg = $2; |
0b7031a2 |
676 | print STDERR ("=> option \"", $opt, |
bb40d378 |
677 | "\", optarg = \"$optarg\"\n") if $debug; |
678 | } |
679 | |
680 | #### Look it up ### |
681 | |
682 | my $tryopt = $opt; # option to try |
e6d5c530 |
683 | my $optbl = $opctl; # table to look it up (long names) |
bb40d378 |
684 | my $type; |
e6d5c530 |
685 | my $dsttype = ''; |
686 | my $incr = 0; |
bb40d378 |
687 | |
688 | if ( $bundling && $starter eq '-' ) { |
689 | # Unbundle single letter option. |
690 | $rest = substr ($tryopt, 1); |
691 | $tryopt = substr ($tryopt, 0, 1); |
692 | $tryopt = lc ($tryopt) if $ignorecase > 1; |
693 | print STDERR ("=> $starter$tryopt unbundled from ", |
694 | "$starter$tryopt$rest\n") if $debug; |
695 | $rest = undef unless $rest ne ''; |
e6d5c530 |
696 | $optbl = $bopctl; # look it up in the short names table |
bb40d378 |
697 | |
698 | # If bundling == 2, long options can override bundles. |
699 | if ( $bundling == 2 and |
f9a400e4 |
700 | defined ($rest) and |
e6d5c530 |
701 | defined ($type = $opctl->{$tryopt.$rest}) ) { |
bb40d378 |
702 | print STDERR ("=> $starter$tryopt rebundled to ", |
703 | "$starter$tryopt$rest\n") if $debug; |
704 | $tryopt .= $rest; |
705 | undef $rest; |
706 | } |
0b7031a2 |
707 | } |
bb40d378 |
708 | |
709 | # Try auto-abbreviation. |
710 | elsif ( $autoabbrev ) { |
711 | # Downcase if allowed. |
712 | $tryopt = $opt = lc ($opt) if $ignorecase; |
713 | # Turn option name into pattern. |
714 | my $pat = quotemeta ($opt); |
715 | # Look up in option names. |
e6d5c530 |
716 | my @hits = grep (/^$pat/, @{$names}); |
bb40d378 |
717 | print STDERR ("=> ", scalar(@hits), " hits (@hits) with \"$pat\" ", |
e6d5c530 |
718 | "out of ", scalar(@{$names}), "\n") if $debug; |
bb40d378 |
719 | |
720 | # Check for ambiguous results. |
721 | unless ( (@hits <= 1) || (grep ($_ eq $opt, @hits) == 1) ) { |
722 | # See if all matches are for the same option. |
723 | my %hit; |
724 | foreach ( @hits ) { |
e6d5c530 |
725 | $_ = $aliases->{$_} if defined $aliases->{$_}; |
bb40d378 |
726 | $hit{$_} = 1; |
727 | } |
728 | # Now see if it really is ambiguous. |
729 | unless ( keys(%hit) == 1 ) { |
e6d5c530 |
730 | return (0) if $passthrough; |
bb40d378 |
731 | warn ("Option ", $opt, " is ambiguous (", |
732 | join(", ", @hits), ")\n"); |
733 | $error++; |
734 | undef $opt; |
e6d5c530 |
735 | return (1, $opt,$arg,$dsttype,$incr,$key); |
bb40d378 |
736 | } |
737 | @hits = keys(%hit); |
738 | } |
739 | |
740 | # Complete the option name, if appropriate. |
741 | if ( @hits == 1 && $hits[0] ne $opt ) { |
742 | $tryopt = $hits[0]; |
743 | $tryopt = lc ($tryopt) if $ignorecase; |
744 | print STDERR ("=> option \"$opt\" -> \"$tryopt\"\n") |
745 | if $debug; |
746 | } |
747 | } |
748 | |
749 | # Map to all lowercase if ignoring case. |
750 | elsif ( $ignorecase ) { |
751 | $tryopt = lc ($opt); |
752 | } |
753 | |
754 | # Check validity by fetching the info. |
755 | $type = $optbl->{$tryopt} unless defined $type; |
756 | unless ( defined $type ) { |
e6d5c530 |
757 | return (0) if $passthrough; |
bb40d378 |
758 | warn ("Unknown option: ", $opt, "\n"); |
759 | $error++; |
e6d5c530 |
760 | return (1, $opt,$arg,$dsttype,$incr,$key); |
bb40d378 |
761 | } |
762 | # Apparently valid. |
763 | $opt = $tryopt; |
10e5c9cc |
764 | print STDERR ("=> found \"$type\" for \"", $opt, "\"\n") if $debug; |
bb40d378 |
765 | |
766 | #### Determine argument status #### |
767 | |
768 | # If it is an option w/o argument, we're almost finished with it. |
e6d5c530 |
769 | if ( $type eq '' || $type eq '!' || $type eq '+' ) { |
bb40d378 |
770 | if ( defined $optarg ) { |
e6d5c530 |
771 | return (0) if $passthrough; |
bb40d378 |
772 | warn ("Option ", $opt, " does not take an argument\n"); |
773 | $error++; |
774 | undef $opt; |
775 | } |
e6d5c530 |
776 | elsif ( $type eq '' || $type eq '+' ) { |
bb40d378 |
777 | $arg = 1; # supply explicit value |
e6d5c530 |
778 | $incr = $type eq '+'; |
bb40d378 |
779 | } |
780 | else { |
781 | substr ($opt, 0, 2) = ''; # strip NO prefix |
782 | $arg = 0; # supply explicit value |
783 | } |
784 | unshift (@ARGV, $starter.$rest) if defined $rest; |
e6d5c530 |
785 | return (1, $opt,$arg,$dsttype,$incr,$key); |
bb40d378 |
786 | } |
787 | |
788 | # Get mandatory status and type info. |
789 | my $mand; |
e6d5c530 |
790 | ($mand, $type, $dsttype, $key) = $type =~ /^(.)(.)([@%]?)$/; |
bb40d378 |
791 | |
792 | # Check if there is an option argument available. |
10e5c9cc |
793 | if ( $gnu_compat ) { |
794 | return (1, $opt, $optarg, $dsttype, $incr, $key) |
795 | if defined $optarg; |
796 | return (1, $opt, $type eq "s" ? '' : 0, $dsttype, $incr, $key) |
797 | if $mand eq ':'; |
798 | } |
799 | |
800 | # Check if there is an option argument available. |
801 | if ( defined $optarg |
802 | ? ($optarg eq '') |
bb40d378 |
803 | : !(defined $rest || @ARGV > 0) ) { |
804 | # Complain if this option needs an argument. |
805 | if ( $mand eq "=" ) { |
e6d5c530 |
806 | return (0) if $passthrough; |
bb40d378 |
807 | warn ("Option ", $opt, " requires an argument\n"); |
808 | $error++; |
809 | undef $opt; |
810 | } |
10e5c9cc |
811 | return (1, $opt, $type eq "s" ? '' : 0, $dsttype, $incr, $key); |
bb40d378 |
812 | } |
813 | |
814 | # Get (possibly optional) argument. |
815 | $arg = (defined $rest ? $rest |
816 | : (defined $optarg ? $optarg : shift (@ARGV))); |
817 | |
818 | # Get key if this is a "name=value" pair for a hash option. |
819 | $key = undef; |
e6d5c530 |
820 | if ($dsttype eq '%' && defined $arg) { |
0b7031a2 |
821 | ($key, $arg) = ($arg =~ /^([^=]*)=(.*)$/s) ? ($1, $2) : ($arg, 1); |
bb40d378 |
822 | } |
823 | |
824 | #### Check if the argument is valid for this option #### |
825 | |
826 | if ( $type eq "s" ) { # string |
0b7031a2 |
827 | # A mandatory string takes anything. |
e6d5c530 |
828 | return (1, $opt,$arg,$dsttype,$incr,$key) if $mand eq "="; |
bb40d378 |
829 | |
0b7031a2 |
830 | # An optional string takes almost anything. |
831 | return (1, $opt,$arg,$dsttype,$incr,$key) |
e6d5c530 |
832 | if defined $optarg || defined $rest; |
833 | return (1, $opt,$arg,$dsttype,$incr,$key) if $arg eq "-"; # ?? |
bb40d378 |
834 | |
835 | # Check for option or option list terminator. |
836 | if ($arg eq $argend || |
e6d5c530 |
837 | $arg =~ /^$prefix.+/) { |
bb40d378 |
838 | # Push back. |
839 | unshift (@ARGV, $arg); |
840 | # Supply empty value. |
841 | $arg = ''; |
842 | } |
843 | } |
844 | |
845 | elsif ( $type eq "n" || $type eq "i" ) { # numeric/integer |
0b7031a2 |
846 | if ( $bundling && defined $rest && $rest =~ /^([-+]?[0-9]+)(.*)$/s ) { |
bb40d378 |
847 | $arg = $1; |
848 | $rest = $2; |
849 | unshift (@ARGV, $starter.$rest) if defined $rest && $rest ne ''; |
850 | } |
0b7031a2 |
851 | elsif ( $arg !~ /^[-+]?[0-9]+$/ ) { |
bb40d378 |
852 | if ( defined $optarg || $mand eq "=" ) { |
853 | if ( $passthrough ) { |
854 | unshift (@ARGV, defined $rest ? $starter.$rest : $arg) |
855 | unless defined $optarg; |
e6d5c530 |
856 | return (0); |
bb40d378 |
857 | } |
858 | warn ("Value \"", $arg, "\" invalid for option ", |
859 | $opt, " (number expected)\n"); |
860 | $error++; |
861 | undef $opt; |
862 | # Push back. |
863 | unshift (@ARGV, $starter.$rest) if defined $rest; |
864 | } |
865 | else { |
866 | # Push back. |
867 | unshift (@ARGV, defined $rest ? $starter.$rest : $arg); |
868 | # Supply default value. |
869 | $arg = 0; |
870 | } |
871 | } |
872 | } |
873 | |
874 | elsif ( $type eq "f" ) { # real number, int is also ok |
875 | # We require at least one digit before a point or 'e', |
876 | # and at least one digit following the point and 'e'. |
877 | # [-]NN[.NN][eNN] |
878 | if ( $bundling && defined $rest && |
0b7031a2 |
879 | $rest =~ /^([-+]?[0-9]+(\.[0-9]+)?([eE][-+]?[0-9]+)?)(.*)$/s ) { |
bb40d378 |
880 | $arg = $1; |
3a0431da |
881 | $rest = $+; |
bb40d378 |
882 | unshift (@ARGV, $starter.$rest) if defined $rest && $rest ne ''; |
883 | } |
0b7031a2 |
884 | elsif ( $arg !~ /^[-+]?[0-9.]+(\.[0-9]+)?([eE][-+]?[0-9]+)?$/ ) { |
bb40d378 |
885 | if ( defined $optarg || $mand eq "=" ) { |
886 | if ( $passthrough ) { |
887 | unshift (@ARGV, defined $rest ? $starter.$rest : $arg) |
888 | unless defined $optarg; |
e6d5c530 |
889 | return (0); |
bb40d378 |
890 | } |
891 | warn ("Value \"", $arg, "\" invalid for option ", |
892 | $opt, " (real number expected)\n"); |
893 | $error++; |
894 | undef $opt; |
895 | # Push back. |
896 | unshift (@ARGV, $starter.$rest) if defined $rest; |
897 | } |
898 | else { |
899 | # Push back. |
900 | unshift (@ARGV, defined $rest ? $starter.$rest : $arg); |
901 | # Supply default value. |
902 | $arg = 0.0; |
903 | } |
904 | } |
905 | } |
906 | else { |
e6d5c530 |
907 | Croak ("GetOpt::Long internal error (Can't happen)\n"); |
bb40d378 |
908 | } |
e6d5c530 |
909 | return (1, $opt, $arg, $dsttype, $incr, $key); |
910 | } |
bb40d378 |
911 | |
e6d5c530 |
912 | # Getopt::Long Configuration. |
913 | sub Configure (@) { |
914 | my (@options) = @_; |
0b7031a2 |
915 | |
916 | my $prevconfig = |
917 | [ $error, $debug, $major_version, $minor_version, |
918 | $autoabbrev, $getopt_compat, $ignorecase, $bundling, $order, |
10e5c9cc |
919 | $gnu_compat, $passthrough, $genprefix ]; |
0b7031a2 |
920 | |
921 | if ( ref($options[0]) eq 'ARRAY' ) { |
922 | ( $error, $debug, $major_version, $minor_version, |
923 | $autoabbrev, $getopt_compat, $ignorecase, $bundling, $order, |
10e5c9cc |
924 | $gnu_compat, $passthrough, $genprefix ) = @{shift(@options)}; |
0b7031a2 |
925 | } |
926 | |
e6d5c530 |
927 | my $opt; |
928 | foreach $opt ( @options ) { |
929 | my $try = lc ($opt); |
930 | my $action = 1; |
931 | if ( $try =~ /^no_?(.*)$/s ) { |
932 | $action = 0; |
933 | $try = $+; |
934 | } |
10e5c9cc |
935 | if ( ($try eq 'default' or $try eq 'defaults') && $action ) { |
936 | ConfigDefaults (); |
937 | } |
938 | elsif ( ($try eq 'posix_default' or $try eq 'posix_defaults') ) { |
939 | local $ENV{POSIXLY_CORRECT}; |
940 | $ENV{POSIXLY_CORRECT} = 1 if $action; |
941 | ConfigDefaults (); |
e6d5c530 |
942 | } |
943 | elsif ( $try eq 'auto_abbrev' or $try eq 'autoabbrev' ) { |
944 | $autoabbrev = $action; |
945 | } |
946 | elsif ( $try eq 'getopt_compat' ) { |
947 | $getopt_compat = $action; |
948 | } |
10e5c9cc |
949 | elsif ( $try eq 'gnu_getopt' ) { |
950 | if ( $action ) { |
951 | $gnu_compat = 1; |
952 | $bundling = 1; |
953 | $getopt_compat = 0; |
954 | $permute = 1; |
955 | } |
956 | } |
957 | elsif ( $try eq 'gnu_compat' ) { |
958 | $gnu_compat = $action; |
959 | } |
e6d5c530 |
960 | elsif ( $try eq 'ignorecase' or $try eq 'ignore_case' ) { |
961 | $ignorecase = $action; |
962 | } |
963 | elsif ( $try eq 'ignore_case_always' ) { |
964 | $ignorecase = $action ? 2 : 0; |
965 | } |
966 | elsif ( $try eq 'bundling' ) { |
967 | $bundling = $action; |
968 | } |
969 | elsif ( $try eq 'bundling_override' ) { |
970 | $bundling = $action ? 2 : 0; |
971 | } |
972 | elsif ( $try eq 'require_order' ) { |
973 | $order = $action ? $REQUIRE_ORDER : $PERMUTE; |
974 | } |
975 | elsif ( $try eq 'permute' ) { |
976 | $order = $action ? $PERMUTE : $REQUIRE_ORDER; |
977 | } |
978 | elsif ( $try eq 'pass_through' or $try eq 'passthrough' ) { |
979 | $passthrough = $action; |
980 | } |
10e5c9cc |
981 | elsif ( $try =~ /^prefix=(.+)$/ && $action ) { |
e6d5c530 |
982 | $genprefix = $1; |
983 | # Turn into regexp. Needs to be parenthesized! |
984 | $genprefix = "(" . quotemeta($genprefix) . ")"; |
985 | eval { '' =~ /$genprefix/; }; |
986 | Croak ("Getopt::Long: invalid pattern \"$genprefix\"") if $@; |
987 | } |
10e5c9cc |
988 | elsif ( $try =~ /^prefix_pattern=(.+)$/ && $action ) { |
e6d5c530 |
989 | $genprefix = $1; |
990 | # Parenthesize if needed. |
0b7031a2 |
991 | $genprefix = "(" . $genprefix . ")" |
e6d5c530 |
992 | unless $genprefix =~ /^\(.*\)$/; |
993 | eval { '' =~ /$genprefix/; }; |
994 | Croak ("Getopt::Long: invalid pattern \"$genprefix\"") if $@; |
995 | } |
996 | elsif ( $try eq 'debug' ) { |
997 | $debug = $action; |
998 | } |
999 | else { |
1000 | Croak ("Getopt::Long: unknown config parameter \"$opt\"") |
1001 | } |
bb40d378 |
1002 | } |
0b7031a2 |
1003 | $prevconfig; |
e6d5c530 |
1004 | } |
bb40d378 |
1005 | |
e6d5c530 |
1006 | # Deprecated name. |
1007 | sub config (@) { |
1008 | Configure (@_); |
1009 | } |
bb40d378 |
1010 | |
e6d5c530 |
1011 | # To prevent Carp from being loaded unnecessarily. |
1012 | sub Croak (@) { |
1013 | require 'Carp.pm'; |
1014 | $Carp::CarpLevel = 1; |
1015 | Carp::croak(@_); |
1016 | }; |
bb40d378 |
1017 | |
e6d5c530 |
1018 | ################ Documentation ################ |
bb40d378 |
1019 | |
1020 | =head1 NAME |
1021 | |
0b7031a2 |
1022 | Getopt::Long - Extended processing of command line options |
bb40d378 |
1023 | |
1024 | =head1 SYNOPSIS |
1025 | |
1026 | use Getopt::Long; |
1027 | $result = GetOptions (...option-descriptions...); |
1028 | |
1029 | =head1 DESCRIPTION |
1030 | |
1031 | The Getopt::Long module implements an extended getopt function called |
1032 | GetOptions(). This function adheres to the POSIX syntax for command |
1033 | line options, with GNU extensions. In general, this means that options |
1034 | have long names instead of single letters, and are introduced with a |
1035 | double dash "--". Support for bundling of command line options, as was |
1036 | the case with the more traditional single-letter approach, is provided |
0b7031a2 |
1037 | but not enabled by default. |
1038 | |
1039 | =head1 Command Line Options, an Introduction |
1040 | |
1041 | Command line operated programs traditionally take their arguments from |
1042 | the command line, for example filenames or other information that the |
1043 | program needs to know. Besides arguments, these programs often take |
1044 | command line I<options> as well. Options are not necessary for the |
1045 | program to work, hence the name 'option', but are used to modify its |
1046 | default behaviour. For example, a program could do its job quietly, |
1047 | but with a suitable option it could provide verbose information about |
1048 | what it did. |
1049 | |
1050 | Command line options come in several flavours. Historically, they are |
1051 | preceded by a single dash C<->, and consist of a single letter. |
1052 | |
1053 | -l -a -c |
1054 | |
1055 | Usually, these single-character options can be bundled: |
1056 | |
1057 | -lac |
1058 | |
1059 | Options can have values, the value is placed after the option |
1060 | character. Sometimes with whitespace in between, sometimes not: |
1061 | |
1062 | -s 24 -s24 |
1063 | |
1064 | Due to the very cryptic nature of these options, another style was |
1065 | developed that used long names. So instead of a cryptic C<-l> one |
1066 | could use the more descriptive C<--long>. To distinguish between a |
1067 | bundle of single-character options and a long one, two dashes are used |
1068 | to precede the option name. Early implementations of long options used |
1069 | a plus C<+> instead. Also, option values could be specified either |
10e5c9cc |
1070 | like |
0b7031a2 |
1071 | |
1072 | --size=24 |
1073 | |
1074 | or |
1075 | |
1076 | --size 24 |
1077 | |
1078 | The C<+> form is now obsolete and strongly deprecated. |
1079 | |
1080 | =head1 Getting Started with Getopt::Long |
1081 | |
1082 | Getopt::Long is the Perl5 successor of C<newgetopt.pl>. This was |
10e5c9cc |
1083 | the first Perl module that provided support for handling the new style |
0b7031a2 |
1084 | of command line options, hence the name Getopt::Long. This module |
1085 | also supports single-character options and bundling. In this case, the |
1086 | options are restricted to alphabetic characters only, and the |
1087 | characters C<?> and C<->. |
1088 | |
1089 | To use Getopt::Long from a Perl program, you must include the |
1090 | following line in your Perl program: |
1091 | |
1092 | use Getopt::Long; |
1093 | |
1094 | This will load the core of the Getopt::Long module and prepare your |
1095 | program for using it. Most of the actual Getopt::Long code is not |
1096 | loaded until you really call one of its functions. |
1097 | |
1098 | In the default configuration, options names may be abbreviated to |
1099 | uniqueness, case does not matter, and a single dash is sufficient, |
1100 | even for long option names. Also, options may be placed between |
1101 | non-option arguments. See L<Configuring Getopt::Long> for more |
1102 | details on how to configure Getopt::Long. |
1103 | |
1104 | =head2 Simple options |
1105 | |
1106 | The most simple options are the ones that take no values. Their mere |
1107 | presence on the command line enables the option. Popular examples are: |
1108 | |
1109 | --all --verbose --quiet --debug |
1110 | |
1111 | Handling simple options is straightforward: |
1112 | |
1113 | my $verbose = ''; # option variable with default value (false) |
1114 | my $all = ''; # option variable with default value (false) |
1115 | GetOptions ('verbose' => \$verbose, 'all' => \$all); |
1116 | |
1117 | The call to GetOptions() parses the command line arguments that are |
1118 | present in C<@ARGV> and sets the option variable to the value C<1> if |
1119 | the option did occur on the command line. Otherwise, the option |
1120 | variable is not touched. Setting the option value to true is often |
1121 | called I<enabling> the option. |
1122 | |
1123 | The option name as specified to the GetOptions() function is called |
1124 | the option I<specification>. Later we'll see that this specification |
1125 | can contain more than just the option name. The reference to the |
1126 | variable is called the option I<destination>. |
1127 | |
1128 | GetOptions() will return a true value if the command line could be |
1129 | processed successfully. Otherwise, it will write error messages to |
1130 | STDERR, and return a false result. |
1131 | |
1132 | =head2 A little bit less simple options |
1133 | |
1134 | Getopt::Long supports two useful variants of simple options: |
1135 | I<negatable> options and I<incremental> options. |
1136 | |
1137 | A negatable option is specified with a exclamation mark C<!> after the |
1138 | option name: |
1139 | |
1140 | my $verbose = ''; # option variable with default value (false) |
1141 | GetOptions ('verbose!' => \$verbose); |
1142 | |
1143 | Now, using C<--verbose> on the command line will enable C<$verbose>, |
1144 | as expected. But it is also allowed to use C<--noverbose>, which will |
1145 | disable C<$verbose> by setting its value to C<0>. Using a suitable |
1146 | default value, the program can find out whether C<$verbose> is false |
1147 | by default, or disabled by using C<--noverbose>. |
1148 | |
1149 | An incremental option is specified with a plus C<+> after the |
1150 | option name: |
1151 | |
1152 | my $verbose = ''; # option variable with default value (false) |
1153 | GetOptions ('verbose+' => \$verbose); |
1154 | |
1155 | Using C<--verbose> on the command line will increment the value of |
1156 | C<$verbose>. This way the program can keep track of how many times the |
1157 | option occurred on the command line. For example, each occurrence of |
1158 | C<--verbose> could increase the verbosity level of the program. |
1159 | |
1160 | =head2 Mixing command line option with other arguments |
1161 | |
1162 | Usually programs take command line options as well as other arguments, |
1163 | for example, file names. It is good practice to always specify the |
1164 | options first, and the other arguments last. Getopt::Long will, |
1165 | however, allow the options and arguments to be mixed and 'filter out' |
1166 | all the options before passing the rest of the arguments to the |
1167 | program. To stop Getopt::Long from processing further arguments, |
1168 | insert a double dash C<--> on the command line: |
1169 | |
1170 | --size 24 -- --all |
1171 | |
1172 | In this example, C<--all> will I<not> be treated as an option, but |
1173 | passed to the program unharmed, in C<@ARGV>. |
1174 | |
1175 | =head2 Options with values |
1176 | |
1177 | For options that take values it must be specified whether the option |
1178 | value is required or not, and what kind of value the option expects. |
1179 | |
1180 | Three kinds of values are supported: integer numbers, floating point |
1181 | numbers, and strings. |
1182 | |
1183 | If the option value is required, Getopt::Long will take the |
1184 | command line argument that follows the option and assign this to the |
1185 | option variable. If, however, the option value is specified as |
1186 | optional, this will only be done if that value does not look like a |
1187 | valid command line option itself. |
bb40d378 |
1188 | |
0b7031a2 |
1189 | my $tag = ''; # option variable with default value |
1190 | GetOptions ('tag=s' => \$tag); |
bb40d378 |
1191 | |
0b7031a2 |
1192 | In the option specification, the option name is followed by an equals |
1193 | sign C<=> and the letter C<s>. The equals sign indicates that this |
1194 | option requires a value. The letter C<s> indicates that this value is |
1195 | an arbitrary string. Other possible value types are C<i> for integer |
1196 | values, and C<f> for floating point values. Using a colon C<:> instead |
1197 | of the equals sign indicates that the option value is optional. In |
1198 | this case, if no suitable value is supplied, string valued options get |
1199 | an empty string C<''> assigned, while numeric options are set to C<0>. |
bb40d378 |
1200 | |
0b7031a2 |
1201 | =head2 Options with multiple values |
bb40d378 |
1202 | |
0b7031a2 |
1203 | Options sometimes take several values. For example, a program could |
1204 | use multiple directories to search for library files: |
bb40d378 |
1205 | |
0b7031a2 |
1206 | --library lib/stdlib --library lib/extlib |
bb40d378 |
1207 | |
0b7031a2 |
1208 | To accomplish this behaviour, simply specify an array reference as the |
1209 | destination for the option: |
bb40d378 |
1210 | |
0b7031a2 |
1211 | my @libfiles = (); |
1212 | GetOptions ("library=s" => \@libfiles); |
bb40d378 |
1213 | |
0b7031a2 |
1214 | Used with the example above, C<@libfiles> would contain two strings |
1215 | upon completion: C<"lib/srdlib"> and C<"lib/extlib">, in that order. |
1216 | It is also possible to specify that only integer or floating point |
1217 | numbers are acceptible values. |
bb40d378 |
1218 | |
0b7031a2 |
1219 | Often it is useful to allow comma-separated lists of values as well as |
1220 | multiple occurrences of the options. This is easy using Perl's split() |
1221 | and join() operators: |
bb40d378 |
1222 | |
0b7031a2 |
1223 | my @libfiles = (); |
1224 | GetOptions ("library=s" => \@libfiles); |
1225 | @libfiles = split(/,/,join(',',@libfiles)); |
bb40d378 |
1226 | |
0b7031a2 |
1227 | Of course, it is important to choose the right separator string for |
1228 | each purpose. |
3cb6de81 |
1229 | |
0b7031a2 |
1230 | =head2 Options with hash values |
bb40d378 |
1231 | |
0b7031a2 |
1232 | If the option destination is a reference to a hash, the option will |
1233 | take, as value, strings of the form I<key>C<=>I<value>. The value will |
1234 | be stored with the specified key in the hash. |
bb40d378 |
1235 | |
0b7031a2 |
1236 | my %defines = (); |
1237 | GetOptions ("define=s" => \%defines); |
bb40d378 |
1238 | |
0b7031a2 |
1239 | When used with command line options: |
1240 | |
1241 | --define os=linux --define vendor=redhat |
1242 | |
1243 | the hash C<%defines> will contain two keys, C<"os"> with value |
1244 | C<"linux> and C<"vendor"> with value C<"redhat">. |
1245 | It is also possible to specify that only integer or floating point |
1246 | numbers are acceptible values. The keys are always taken to be strings. |
1247 | |
1248 | =head2 User-defined subroutines to handle options |
1249 | |
1250 | Ultimate control over what should be done when (actually: each time) |
1251 | an option is encountered on the command line can be achieved by |
1252 | designating a reference to a subroutine (or an anonymous subroutine) |
1253 | as the option destination. When GetOptions() encounters the option, it |
1254 | will call the subroutine with two arguments: the name of the option, |
1255 | and the value to be assigned. It is up to the subroutine to store the |
1256 | value, or do whatever it thinks is appropriate. |
1257 | |
1258 | A trivial application of this mechanism is to implement options that |
1259 | are related to each other. For example: |
1260 | |
1261 | my $verbose = ''; # option variable with default value (false) |
1262 | GetOptions ('verbose' => \$verbose, |
1263 | 'quiet' => sub { $verbose = 0 }); |
1264 | |
1265 | Here C<--verbose> and C<--quiet> control the same variable |
1266 | C<$verbose>, but with opposite values. |
1267 | |
1268 | If the subroutine needs to signal an error, it should call die() with |
1269 | the desired error message as its argument. GetOptions() will catch the |
1270 | die(), issue the error message, and record that an error result must |
1271 | be returned upon completion. |
1272 | |
bee0ef1e |
1273 | If the text of the error message starts with an exclamantion mark C<!> |
1274 | it is interpreted specially by GetOptions(). There is currently one |
1275 | special command implemented: C<die("!FINISH")> will cause GetOptions() |
1276 | to stop processing options, as if it encountered a double dash C<-->. |
0b7031a2 |
1277 | |
1278 | =head2 Options with multiple names |
1279 | |
1280 | Often it is user friendly to supply alternate mnemonic names for |
1281 | options. For example C<--height> could be an alternate name for |
1282 | C<--length>. Alternate names can be included in the option |
1283 | specification, separated by vertical bar C<|> characters. To implement |
1284 | the above example: |
1285 | |
1286 | GetOptions ('length|height=f' => \$length); |
1287 | |
1288 | The first name is called the I<primary> name, the other names are |
1289 | called I<aliases>. |
1290 | |
1291 | Multiple alternate names are possible. |
1292 | |
1293 | =head2 Case and abbreviations |
1294 | |
1295 | Without additional configuration, GetOptions() will ignore the case of |
1296 | option names, and allow the options to be abbreviated to uniqueness. |
1297 | |
1298 | GetOptions ('length|height=f' => \$length, "head" => \$head); |
1299 | |
1300 | This call will allow C<--l> and C<--L> for the length option, but |
1301 | requires a least C<--hea> and C<--hei> for the head and height options. |
1302 | |
1303 | =head2 Summary of Option Specifications |
1304 | |
1305 | Each option specifier consists of two parts: the name specification |
10e5c9cc |
1306 | and the argument specification. |
0b7031a2 |
1307 | |
1308 | The name specification contains the name of the option, optionally |
1309 | followed by a list of alternative names separated by vertical bar |
10e5c9cc |
1310 | characters. |
0b7031a2 |
1311 | |
1312 | length option name is "length" |
1313 | length|size|l name is "length", aliases are "size" and "l" |
1314 | |
1315 | The argument specification is optional. If omitted, the option is |
1316 | considered boolean, a value of 1 will be assigned when the option is |
1317 | used on the command line. |
1318 | |
1319 | The argument specification can be |
1320 | |
1321 | =over |
bb40d378 |
1322 | |
1323 | =item ! |
1324 | |
0b7031a2 |
1325 | The option does not take an argument and may be negated, i.e. prefixed |
1326 | by "no". E.g. C<"foo!"> will allow C<--foo> (a value of 1 will be |
265c41c2 |
1327 | assigned) and C<--nofoo> (a value of 0 will be assigned). If the |
1328 | option has aliases, this applies to the aliases as well. |
1329 | |
1330 | Using negation on a single letter option when bundling is in effect is |
1331 | pointless and will result in a warning. |
bb40d378 |
1332 | |
e6d5c530 |
1333 | =item + |
1334 | |
0b7031a2 |
1335 | The option does not take an argument and will be incremented by 1 |
1336 | every time it appears on the command line. E.g. C<"more+">, when used |
1337 | with C<--more --more --more>, will increment the value three times, |
1338 | resulting in a value of 3 (provided it was 0 or undefined at first). |
e6d5c530 |
1339 | |
0b7031a2 |
1340 | The C<+> specifier is ignored if the option destination is not a scalar. |
e6d5c530 |
1341 | |
0b7031a2 |
1342 | =item = I<type> [ I<desttype> ] |
bb40d378 |
1343 | |
0b7031a2 |
1344 | The option requires an argument of the given type. Supported types |
1345 | are: |
bb40d378 |
1346 | |
0b7031a2 |
1347 | =over |
bb40d378 |
1348 | |
0b7031a2 |
1349 | =item s |
bb40d378 |
1350 | |
0b7031a2 |
1351 | String. An arbitrary sequence of characters. It is valid for the |
1352 | argument to start with C<-> or C<-->. |
bb40d378 |
1353 | |
0b7031a2 |
1354 | =item i |
bb40d378 |
1355 | |
0b7031a2 |
1356 | Integer. An optional leading plus or minus sign, followed by a |
1357 | sequence of digits. |
bb40d378 |
1358 | |
0b7031a2 |
1359 | =item f |
bb40d378 |
1360 | |
0b7031a2 |
1361 | Real number. For example C<3.14>, C<-6.23E24> and so on. |
bb40d378 |
1362 | |
0b7031a2 |
1363 | =back |
1364 | |
1365 | The I<desttype> can be C<@> or C<%> to specify that the option is |
1366 | list or a hash valued. This is only needed when the destination for |
1367 | the option value is not otherwise specified. It should be omitted when |
1368 | not needed. |
1369 | |
1370 | =item : I<type> [ I<desttype> ] |
404cbe93 |
1371 | |
0b7031a2 |
1372 | Like C<=>, but designates the argument as optional. |
1373 | If omitted, an empty string will be assigned to string values options, |
1374 | and the value zero to numeric options. |
404cbe93 |
1375 | |
0b7031a2 |
1376 | Note that if a string argument starts with C<-> or C<-->, it will be |
1377 | considered an option on itself. |
404cbe93 |
1378 | |
1379 | =back |
1380 | |
0b7031a2 |
1381 | =head1 Advanced Possibilities |
404cbe93 |
1382 | |
10e5c9cc |
1383 | =head2 Object oriented interface |
1384 | |
1385 | Getopt::Long can be used in an object oriented way as well: |
1386 | |
1387 | use Getopt::Long; |
1388 | $p = new Getopt::Long::Parser; |
1389 | $p->configure(...configuration options...); |
1390 | if ($p->getoptions(...options descriptions...)) ... |
1391 | |
1392 | Configuration options can be passed to the constructor: |
1393 | |
1394 | $p = new Getopt::Long::Parser |
1395 | config => [...configuration options...]; |
1396 | |
1397 | For thread safety, each method call will acquire an exclusive lock to |
1398 | the Getopt::Long module. So don't call these methods from a callback |
1399 | routine! |
1400 | |
0b7031a2 |
1401 | =head2 Documentation and help texts |
404cbe93 |
1402 | |
0b7031a2 |
1403 | Getopt::Long encourages the use of Pod::Usage to produce help |
1404 | messages. For example: |
404cbe93 |
1405 | |
0b7031a2 |
1406 | use Getopt::Long; |
1407 | use Pod::Usage; |
404cbe93 |
1408 | |
0b7031a2 |
1409 | my $man = 0; |
1410 | my $help = 0; |
404cbe93 |
1411 | |
0b7031a2 |
1412 | GetOptions('help|?' => \$help, man => \$man) or pod2usage(2); |
1413 | pod2usage(1) if $help; |
1414 | pod2usage(-exitstatus => 0, -verbose => 2) if $man; |
404cbe93 |
1415 | |
0b7031a2 |
1416 | __END__ |
404cbe93 |
1417 | |
0b7031a2 |
1418 | =head1 NAME |
404cbe93 |
1419 | |
0b7031a2 |
1420 | sample - Using GetOpt::Long and Pod::Usage |
404cbe93 |
1421 | |
0b7031a2 |
1422 | =head1 SYNOPSIS |
404cbe93 |
1423 | |
0b7031a2 |
1424 | sample [options] [file ...] |
404cbe93 |
1425 | |
0b7031a2 |
1426 | Options: |
1427 | -help brief help message |
1428 | -man full documentation |
381319f7 |
1429 | |
0b7031a2 |
1430 | =head1 OPTIONS |
381319f7 |
1431 | |
0b7031a2 |
1432 | =over 8 |
381319f7 |
1433 | |
0b7031a2 |
1434 | =item B<-help> |
381319f7 |
1435 | |
0b7031a2 |
1436 | Print a brief help message and exits. |
404cbe93 |
1437 | |
0b7031a2 |
1438 | =item B<-man> |
404cbe93 |
1439 | |
0b7031a2 |
1440 | Prints the manual page and exits. |
404cbe93 |
1441 | |
0b7031a2 |
1442 | =back |
404cbe93 |
1443 | |
0b7031a2 |
1444 | =head1 DESCRIPTION |
404cbe93 |
1445 | |
0b7031a2 |
1446 | B<This program> will read the given input file(s) and do someting |
1447 | useful with the contents thereof. |
404cbe93 |
1448 | |
0b7031a2 |
1449 | =cut |
535b5725 |
1450 | |
0b7031a2 |
1451 | See L<Pod::Usage> for details. |
535b5725 |
1452 | |
0b7031a2 |
1453 | =head2 Storing options in a hash |
404cbe93 |
1454 | |
0b7031a2 |
1455 | Sometimes, for example when there are a lot of options, having a |
1456 | separate variable for each of them can be cumbersome. GetOptions() |
1457 | supports, as an alternative mechanism, storing options in a hash. |
404cbe93 |
1458 | |
0b7031a2 |
1459 | To obtain this, a reference to a hash must be passed I<as the first |
1460 | argument> to GetOptions(). For each option that is specified on the |
1461 | command line, the option value will be stored in the hash with the |
1462 | option name as key. Options that are not actually used on the command |
1463 | line will not be put in the hash, on other words, |
1464 | C<exists($h{option})> (or defined()) can be used to test if an option |
1465 | was used. The drawback is that warnings will be issued if the program |
1466 | runs under C<use strict> and uses C<$h{option}> without testing with |
1467 | exists() or defined() first. |
381319f7 |
1468 | |
0b7031a2 |
1469 | my %h = (); |
1470 | GetOptions (\%h, 'length=i'); # will store in $h{length} |
f06db76b |
1471 | |
0b7031a2 |
1472 | For options that take list or hash values, it is necessary to indicate |
1473 | this by appending an C<@> or C<%> sign after the type: |
f06db76b |
1474 | |
0b7031a2 |
1475 | GetOptions (\%h, 'colours=s@'); # will push to @{$h{colours}} |
f06db76b |
1476 | |
0b7031a2 |
1477 | To make things more complicated, the hash may contain references to |
1478 | the actual destinations, for example: |
f06db76b |
1479 | |
0b7031a2 |
1480 | my $len = 0; |
1481 | my %h = ('length' => \$len); |
1482 | GetOptions (\%h, 'length=i'); # will store in $len |
f06db76b |
1483 | |
0b7031a2 |
1484 | This example is fully equivalent with: |
a11f5414 |
1485 | |
0b7031a2 |
1486 | my $len = 0; |
1487 | GetOptions ('length=i' => \$len); # will store in $len |
f06db76b |
1488 | |
0b7031a2 |
1489 | Any mixture is possible. For example, the most frequently used options |
1490 | could be stored in variables while all other options get stored in the |
1491 | hash: |
f06db76b |
1492 | |
0b7031a2 |
1493 | my $verbose = 0; # frequently referred |
1494 | my $debug = 0; # frequently referred |
1495 | my %h = ('verbose' => \$verbose, 'debug' => \$debug); |
1496 | GetOptions (\%h, 'verbose', 'debug', 'filter', 'size=i'); |
1497 | if ( $verbose ) { ... } |
1498 | if ( exists $h{filter} ) { ... option 'filter' was specified ... } |
f06db76b |
1499 | |
0b7031a2 |
1500 | =head2 Bundling |
f06db76b |
1501 | |
0b7031a2 |
1502 | With bundling it is possible to set several single-character options |
1503 | at once. For example if C<a>, C<v> and C<x> are all valid options, |
bb40d378 |
1504 | |
0b7031a2 |
1505 | -vax |
bb40d378 |
1506 | |
0b7031a2 |
1507 | would set all three. |
f06db76b |
1508 | |
0b7031a2 |
1509 | Getopt::Long supports two levels of bundling. To enable bundling, a |
1510 | call to Getopt::Long::Configure is required. |
bb40d378 |
1511 | |
0b7031a2 |
1512 | The first level of bundling can be enabled with: |
f06db76b |
1513 | |
0b7031a2 |
1514 | Getopt::Long::Configure ("bundling"); |
404cbe93 |
1515 | |
0b7031a2 |
1516 | Configured this way, single-character options can be bundled but long |
1517 | options B<must> always start with a double dash C<--> to avoid |
1518 | abiguity. For example, when C<vax>, C<a>, C<v> and C<x> are all valid |
1519 | options, |
404cbe93 |
1520 | |
0b7031a2 |
1521 | -vax |
381319f7 |
1522 | |
10e5c9cc |
1523 | would set C<a>, C<v> and C<x>, but |
404cbe93 |
1524 | |
0b7031a2 |
1525 | --vax |
404cbe93 |
1526 | |
0b7031a2 |
1527 | would set C<vax>. |
a11f5414 |
1528 | |
0b7031a2 |
1529 | The second level of bundling lifts this restriction. It can be enabled |
1530 | with: |
a11f5414 |
1531 | |
0b7031a2 |
1532 | Getopt::Long::Configure ("bundling_override"); |
a11f5414 |
1533 | |
0b7031a2 |
1534 | Now, C<-vax> would set the option C<vax>. |
a11f5414 |
1535 | |
0b7031a2 |
1536 | When any level of bundling is enabled, option values may be inserted |
1537 | in the bundle. For example: |
381319f7 |
1538 | |
0b7031a2 |
1539 | -h24w80 |
f06db76b |
1540 | |
0b7031a2 |
1541 | is equivalent to |
f06db76b |
1542 | |
0b7031a2 |
1543 | -h 24 -w 80 |
f06db76b |
1544 | |
0b7031a2 |
1545 | When configured for bundling, single-character options are matched |
1546 | case sensitive while long options are matched case insensitive. To |
1547 | have the single-character options matched case insensitive as well, |
1548 | use: |
a0d0e21e |
1549 | |
0b7031a2 |
1550 | Getopt::Long::Configure ("bundling", "ignorecase_always"); |
a0d0e21e |
1551 | |
0b7031a2 |
1552 | It goes without saying that bundling can be quite confusing. |
404cbe93 |
1553 | |
0b7031a2 |
1554 | =head2 The lonesome dash |
404cbe93 |
1555 | |
0b7031a2 |
1556 | Some applications require the option C<-> (that's a lone dash). This |
1557 | can be achieved by adding an option specification with an empty name: |
a0d0e21e |
1558 | |
0b7031a2 |
1559 | GetOptions ('' => \$stdio); |
a11f5414 |
1560 | |
0b7031a2 |
1561 | A lone dash on the command line will now be legal, and set options |
1562 | variable C<$stdio>. |
a0d0e21e |
1563 | |
0b7031a2 |
1564 | =head2 Argument call-back |
a0d0e21e |
1565 | |
0b7031a2 |
1566 | A special option 'name' C<<>> can be used to designate a subroutine |
1567 | to handle non-option arguments. When GetOptions() encounters an |
1568 | argument that does not look like an option, it will immediately call this |
1569 | subroutine and passes it the argument as a parameter. |
a0d0e21e |
1570 | |
0b7031a2 |
1571 | For example: |
a0d0e21e |
1572 | |
0b7031a2 |
1573 | my $width = 80; |
1574 | sub process { ... } |
1575 | GetOptions ('width=i' => \$width, '<>' => \&process); |
a0d0e21e |
1576 | |
0b7031a2 |
1577 | When applied to the following command line: |
a11f5414 |
1578 | |
0b7031a2 |
1579 | arg1 --width=72 arg2 --width=60 arg3 |
404cbe93 |
1580 | |
10e5c9cc |
1581 | This will call |
1582 | C<process("arg1")> while C<$width> is C<80>, |
0b7031a2 |
1583 | C<process("arg2")> while C<$width> is C<72>, and |
1584 | C<process("arg3")> while C<$width> is C<60>. |
381319f7 |
1585 | |
0b7031a2 |
1586 | This feature requires configuration option B<permute>, see section |
1587 | L<Configuring Getopt::Long>. |
a0d0e21e |
1588 | |
a0d0e21e |
1589 | |
0b7031a2 |
1590 | =head1 Configuring Getopt::Long |
1591 | |
1592 | Getopt::Long can be configured by calling subroutine |
1593 | Getopt::Long::Configure(). This subroutine takes a list of quoted |
10e5c9cc |
1594 | strings, each specifying a configuration option to be enabled, e.g. |
1595 | C<ignore_case>, or disabled, e.g. C<no_ignore_case>. Case does not |
0b7031a2 |
1596 | matter. Multiple calls to Configure() are possible. |
404cbe93 |
1597 | |
10e5c9cc |
1598 | Alternatively, as of version 2.24, the configuration options may be |
1599 | passed together with the C<use> statement: |
1600 | |
1601 | use Getopt::Long qw(:config no_ignore_case bundling); |
1602 | |
bb40d378 |
1603 | The following options are available: |
404cbe93 |
1604 | |
bb40d378 |
1605 | =over 12 |
a0d0e21e |
1606 | |
bb40d378 |
1607 | =item default |
a0d0e21e |
1608 | |
bb40d378 |
1609 | This option causes all configuration options to be reset to their |
1610 | default values. |
404cbe93 |
1611 | |
10e5c9cc |
1612 | =item posix_default |
1613 | |
1614 | This option causes all configuration options to be reset to their |
1615 | default values as if the environment variable POSIXLY_CORRECT had |
1616 | been set. |
1617 | |
bb40d378 |
1618 | =item auto_abbrev |
404cbe93 |
1619 | |
bb40d378 |
1620 | Allow option names to be abbreviated to uniqueness. |
10e5c9cc |
1621 | Default is enabled unless environment variable |
1622 | POSIXLY_CORRECT has been set, in which case C<auto_abbrev> is disabled. |
404cbe93 |
1623 | |
0b7031a2 |
1624 | =item getopt_compat |
a0d0e21e |
1625 | |
0b7031a2 |
1626 | Allow C<+> to start options. |
10e5c9cc |
1627 | Default is enabled unless environment variable |
1628 | POSIXLY_CORRECT has been set, in which case C<getopt_compat> is disabled. |
88e49c4e |
1629 | |
bb40d378 |
1630 | =item require_order |
404cbe93 |
1631 | |
0b7031a2 |
1632 | Whether command line arguments are allowed to be mixed with options. |
10e5c9cc |
1633 | Default is disabled unless environment variable |
1634 | POSIXLY_CORRECT has been set, in which case C<require_order> is enabled. |
404cbe93 |
1635 | |
0b7031a2 |
1636 | See also C<permute>, which is the opposite of C<require_order>. |
a0d0e21e |
1637 | |
bb40d378 |
1638 | =item permute |
404cbe93 |
1639 | |
0b7031a2 |
1640 | Whether command line arguments are allowed to be mixed with options. |
10e5c9cc |
1641 | Default is enabled unless environment variable |
1642 | POSIXLY_CORRECT has been set, in which case C<permute> is disabled. |
0b7031a2 |
1643 | Note that C<permute> is the opposite of C<require_order>. |
a0d0e21e |
1644 | |
10e5c9cc |
1645 | If C<permute> is enabled, this means that |
a0d0e21e |
1646 | |
0b7031a2 |
1647 | --foo arg1 --bar arg2 arg3 |
a0d0e21e |
1648 | |
bb40d378 |
1649 | is equivalent to |
a0d0e21e |
1650 | |
0b7031a2 |
1651 | --foo --bar arg1 arg2 arg3 |
a0d0e21e |
1652 | |
0b7031a2 |
1653 | If an argument call-back routine is specified, C<@ARGV> will always be |
1654 | empty upon succesful return of GetOptions() since all options have been |
1655 | processed. The only exception is when C<--> is used: |
a0d0e21e |
1656 | |
0b7031a2 |
1657 | --foo arg1 --bar arg2 -- arg3 |
404cbe93 |
1658 | |
bb40d378 |
1659 | will call the call-back routine for arg1 and arg2, and terminate |
0b7031a2 |
1660 | GetOptions() leaving C<"arg2"> in C<@ARGV>. |
381319f7 |
1661 | |
10e5c9cc |
1662 | If C<require_order> is enabled, options processing |
bb40d378 |
1663 | terminates when the first non-option is encountered. |
a0d0e21e |
1664 | |
0b7031a2 |
1665 | --foo arg1 --bar arg2 arg3 |
381319f7 |
1666 | |
bb40d378 |
1667 | is equivalent to |
381319f7 |
1668 | |
0b7031a2 |
1669 | --foo -- arg1 --bar arg2 arg3 |
404cbe93 |
1670 | |
10e5c9cc |
1671 | =item bundling (default: disabled) |
404cbe93 |
1672 | |
10e5c9cc |
1673 | Enabling this option will allow single-character options to be bundled. |
0b7031a2 |
1674 | To distinguish bundles from long option names, long options I<must> be |
1675 | introduced with C<--> and single-character options (and bundles) with |
1676 | C<->. |
bb40d378 |
1677 | |
10e5c9cc |
1678 | Note: disabling C<bundling> also disables C<bundling_override>. |
a11f5414 |
1679 | |
10e5c9cc |
1680 | =item bundling_override (default: disabled) |
381319f7 |
1681 | |
10e5c9cc |
1682 | If C<bundling_override> is enabled, bundling is enabled as with |
1683 | C<bundling> but now long option names override option bundles. |
381319f7 |
1684 | |
10e5c9cc |
1685 | Note: disabling C<bundling_override> also disables C<bundling>. |
381319f7 |
1686 | |
bb40d378 |
1687 | B<Note:> Using option bundling can easily lead to unexpected results, |
1688 | especially when mixing long options and bundles. Caveat emptor. |
381319f7 |
1689 | |
10e5c9cc |
1690 | =item ignore_case (default: enabled) |
381319f7 |
1691 | |
10e5c9cc |
1692 | If enabled, case is ignored when matching long option names. Single |
0b7031a2 |
1693 | character options will be treated case-sensitive. |
381319f7 |
1694 | |
10e5c9cc |
1695 | Note: disabling C<ignore_case> also disables C<ignore_case_always>. |
381319f7 |
1696 | |
10e5c9cc |
1697 | =item ignore_case_always (default: disabled) |
a11f5414 |
1698 | |
bb40d378 |
1699 | When bundling is in effect, case is ignored on single-character |
10e5c9cc |
1700 | options also. |
381319f7 |
1701 | |
10e5c9cc |
1702 | Note: disabling C<ignore_case_always> also disables C<ignore_case>. |
381319f7 |
1703 | |
10e5c9cc |
1704 | =item pass_through (default: disabled) |
a0d0e21e |
1705 | |
0b7031a2 |
1706 | Options that are unknown, ambiguous or supplied with an invalid option |
1707 | value are passed through in C<@ARGV> instead of being flagged as |
1708 | errors. This makes it possible to write wrapper scripts that process |
1709 | only part of the user supplied command line arguments, and pass the |
bb40d378 |
1710 | remaining options to some other program. |
a0d0e21e |
1711 | |
10e5c9cc |
1712 | This can be very confusing, especially when C<permute> is also enabled. |
16c18a90 |
1713 | |
3a0431da |
1714 | =item prefix |
1715 | |
0b7031a2 |
1716 | The string that starts options. If a constant string is not |
1717 | sufficient, see C<prefix_pattern>. |
3a0431da |
1718 | |
1719 | =item prefix_pattern |
1720 | |
1721 | A Perl pattern that identifies the strings that introduce options. |
1722 | Default is C<(--|-|\+)> unless environment variable |
1723 | POSIXLY_CORRECT has been set, in which case it is C<(--|-)>. |
1724 | |
10e5c9cc |
1725 | =item debug (default: disabled) |
a0d0e21e |
1726 | |
10e5c9cc |
1727 | Enable debugging output. |
a0d0e21e |
1728 | |
bb40d378 |
1729 | =back |
a0d0e21e |
1730 | |
0b7031a2 |
1731 | =head1 Return values and Errors |
381319f7 |
1732 | |
0b7031a2 |
1733 | Configuration errors and errors in the option definitions are |
1734 | signalled using die() and will terminate the calling program unless |
1735 | the call to Getopt::Long::GetOptions() was embedded in C<eval { ... |
1736 | }>, or die() was trapped using C<$SIG{__DIE__}>. |
a0d0e21e |
1737 | |
10e5c9cc |
1738 | GetOptions returns true to indicate success. |
1739 | It returns false when the function detected one or more errors during |
1740 | option parsing. These errors are signalled using warn() and can be |
1741 | trapped with C<$SIG{__WARN__}>. |
a0d0e21e |
1742 | |
0b7031a2 |
1743 | Errors that can't happen are signalled using Carp::croak(). |
a0d0e21e |
1744 | |
0b7031a2 |
1745 | =head1 Legacy |
a0d0e21e |
1746 | |
0b7031a2 |
1747 | The earliest development of C<newgetopt.pl> started in 1990, with Perl |
1748 | version 4. As a result, its development, and the development of |
1749 | Getopt::Long, has gone through several stages. Since backward |
1750 | compatibility has always been extremely important, the current version |
1751 | of Getopt::Long still supports a lot of constructs that nowadays are |
1752 | no longer necessary or otherwise unwanted. This section describes |
1753 | briefly some of these 'features'. |
a0d0e21e |
1754 | |
0b7031a2 |
1755 | =head2 Default destinations |
a0d0e21e |
1756 | |
0b7031a2 |
1757 | When no destination is specified for an option, GetOptions will store |
1758 | the resultant value in a global variable named C<opt_>I<XXX>, where |
1759 | I<XXX> is the primary name of this option. When a progam executes |
1760 | under C<use strict> (recommended), these variables must be |
1761 | pre-declared with our() or C<use vars>. |
1762 | |
1763 | our $opt_length = 0; |
1764 | GetOptions ('length=i'); # will store in $opt_length |
1765 | |
1766 | To yield a usable Perl variable, characters that are not part of the |
1767 | syntax for variables are translated to underscores. For example, |
1768 | C<--fpp-struct-return> will set the variable |
1769 | C<$opt_fpp_struct_return>. Note that this variable resides in the |
1770 | namespace of the calling program, not necessarily C<main>. For |
1771 | example: |
1772 | |
1773 | GetOptions ("size=i", "sizes=i@"); |
1774 | |
1775 | with command line "-size 10 -sizes 24 -sizes 48" will perform the |
1776 | equivalent of the assignments |
1777 | |
1778 | $opt_size = 10; |
1779 | @opt_sizes = (24, 48); |
1780 | |
1781 | =head2 Alternative option starters |
1782 | |
1783 | A string of alternative option starter characters may be passed as the |
1784 | first argument (or the first argument after a leading hash reference |
1785 | argument). |
1786 | |
1787 | my $len = 0; |
1788 | GetOptions ('/', 'length=i' => $len); |
1789 | |
1790 | Now the command line may look like: |
1791 | |
1792 | /length 24 -- arg |
1793 | |
1794 | Note that to terminate options processing still requires a double dash |
1795 | C<-->. |
1796 | |
10e5c9cc |
1797 | GetOptions() will not interpret a leading C<< "<>" >> as option starters |
1798 | if the next argument is a reference. To force C<< "<" >> and C<< ">" >> as |
1799 | option starters, use C<< "><" >>. Confusing? Well, B<using a starter |
0b7031a2 |
1800 | argument is strongly deprecated> anyway. |
1801 | |
1802 | =head2 Configuration variables |
1803 | |
1804 | Previous versions of Getopt::Long used variables for the purpose of |
10e5c9cc |
1805 | configuring. Although manipulating these variables still work, it is |
1806 | strongly encouraged to use the C<Configure> routine that was introduced |
1807 | in version 2.17. Besides, it is much easier. |
1808 | |
1809 | =head1 Trouble Shooting |
1810 | |
1811 | =head2 Warning: Ignoring '!' modifier for short option |
1812 | |
1813 | This warning is issued when the '!' modifier is applied to a short |
1814 | (one-character) option and bundling is in effect. E.g., |
1815 | |
1816 | Getopt::Long::Configure("bundling"); |
1817 | GetOptions("foo|f!" => \$foo); |
1818 | |
1819 | Note that older Getopt::Long versions did not issue a warning, because |
1820 | the '!' modifier was applied to the first name only. This bug was |
1821 | fixed in 2.22. |
1822 | |
1823 | Solution: separate the long and short names and apply the '!' to the |
1824 | long names only, e.g., |
1825 | |
1826 | GetOptions("foo!" => \$foo, "f" => \$foo); |
1827 | |
1828 | =head2 GetOptions does not return a false result when an option is not supplied |
1829 | |
1830 | That's why they're called 'options'. |
a0d0e21e |
1831 | |
bb40d378 |
1832 | =head1 AUTHOR |
a11f5414 |
1833 | |
10e5c9cc |
1834 | Johan Vromans <jvromans@squirrel.nl> |
a11f5414 |
1835 | |
bb40d378 |
1836 | =head1 COPYRIGHT AND DISCLAIMER |
a11f5414 |
1837 | |
0b7031a2 |
1838 | This program is Copyright 2000,1990 by Johan Vromans. |
bb40d378 |
1839 | This program is free software; you can redistribute it and/or |
1a505819 |
1840 | modify it under the terms of the Perl Artistic License or the |
1841 | GNU General Public License as published by the Free Software |
1842 | Foundation; either version 2 of the License, or (at your option) any |
1843 | later version. |
a11f5414 |
1844 | |
bb40d378 |
1845 | This program is distributed in the hope that it will be useful, |
1846 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
1847 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1848 | GNU General Public License for more details. |
a0d0e21e |
1849 | |
bb40d378 |
1850 | If you do not have a copy of the GNU General Public License write to |
10e5c9cc |
1851 | the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, |
f9a400e4 |
1852 | MA 02139, USA. |
a0d0e21e |
1853 | |
bb40d378 |
1854 | =cut |
0b7031a2 |
1855 | |
1856 | # Local Variables: |
1857 | # mode: perl |
1858 | # eval: (load-file "pod.el") |
1859 | # End: |