1 # NOTE: Derived from lib/Getopt/Long.pm.
2 # Changes made here will be lost when autosplit again.
6 #line 226 "lib/Getopt/Long.pm (autosplit into lib/auto/Getopt/Long/GetOptions.al)"
7 ################ AutoLoading subroutines ################
9 # RCS Status : $Id: GetoptLongAl.pl,v 2.30 2001-01-31 10:21:11+01 jv Exp $
10 # Author : Johan Vromans
11 # Created On : Fri Mar 27 11:50:30 1998
12 # Last Modified By: Johan Vromans
13 # Last Modified On: Tue Dec 26 18:01:16 2000
19 my @optionlist = @_; # local copy of the option descriptions
20 my $argend = '--'; # option list terminator
21 my %opctl = (); # table of arg.specs (long and abbrevs)
22 my %bopctl = (); # table of arg.specs (bundles)
23 my $pkg = $caller || (caller)[0]; # current context
24 # Needed if linkage is omitted.
25 my %aliases= (); # alias table
26 my @ret = (); # accum for non-options
27 my %linkage; # linkage
28 my $userlinkage; # user supplied HASH
29 my $opt; # current option
30 my $genprefix = $genprefix; # so we can call the same module many times
31 my @opctl; # the possible long option names
35 print STDERR ("GetOpt::Long $Getopt::Long::VERSION ",
36 "called from package \"$pkg\".",
38 'GetOptionsAl $Revision: 2.30 $ ',
42 "autoabbrev=$autoabbrev,".
43 "bundling=$bundling,",
44 "getopt_compat=$getopt_compat,",
45 "gnu_compat=$gnu_compat,",
48 "ignorecase=$ignorecase,",
49 "passthrough=$passthrough,",
50 "genprefix=\"$genprefix\".",
54 # Check for ref HASH as first argument.
55 # First argument may be an object. It's OK to use this as long
56 # as it is really a hash underneath.
58 if ( ref($optionlist[0]) and
59 "$optionlist[0]" =~ /^(?:.*\=)?HASH\([^\(]*\)$/ ) {
60 $userlinkage = shift (@optionlist);
61 print STDERR ("=> user linkage: $userlinkage\n") if $debug;
64 # See if the first element of the optionlist contains option
66 # Be careful not to interpret '<>' as option starters.
67 if ( $optionlist[0] =~ /^\W+$/
68 && !($optionlist[0] eq '<>'
70 && ref($optionlist[1])) ) {
71 $genprefix = shift (@optionlist);
72 # Turn into regexp. Needs to be parenthesized!
73 $genprefix =~ s/(\W)/\\$1/g;
74 $genprefix = "([" . $genprefix . "])";
77 # Verify correctness of optionlist.
80 while ( @optionlist > 0 ) {
81 my $opt = shift (@optionlist);
83 # Strip leading prefix so people can specify "--foo=i" if they like.
84 $opt = $+ if $opt =~ /^$genprefix+(.*)$/s;
87 if ( (defined $userlinkage)
88 && !(@optionlist > 0 && ref($optionlist[0]))
89 && (exists $userlinkage->{$opt})
90 && ref($userlinkage->{$opt}) ) {
91 unshift (@optionlist, $userlinkage->{$opt});
93 unless ( @optionlist > 0
94 && ref($optionlist[0]) && ref($optionlist[0]) eq 'CODE' ) {
95 $error .= "Option spec <> requires a reference to a subroutine\n";
98 $linkage{'<>'} = shift (@optionlist);
102 # Match option spec. Allow '?' as an alias only.
103 if ( $opt !~ /^((\w+[-\w]*)(\|(\?|\w[-\w]*)?)*)?([!~+]|[=:][infse][@%]?)?$/ ) {
104 $error .= "Error in option spec: \"$opt\"\n";
107 my ($o, $c, $a) = ($1, $5);
108 $c = '' unless defined $c;
110 # $linko keeps track of the primary name the user specified.
111 # This name will be used for the internal or external linkage.
112 # In other words, if the user specifies "FoO|BaR", it will
113 # match any case combinations of 'foo' and 'bar', but if a global
114 # variable needs to be set, it will be $opt_FoO in the exact case
118 if ( ! defined $o ) {
119 # empty -> '-' option
122 $bopctl{''} = $c if $bundling;
126 my @o = split (/\|/, $o);
128 # Force an alias if the option name is not locase.
129 $a = $o unless $o eq lc($o);
133 && ($bundling ? length($o) > 1 : 1));
136 if ( $bundling && length($_) == 1 ) {
137 $_ = lc ($_) if $ignorecase > 1;
140 warn ("Ignoring '!' modifier for short option $_\n");
141 $opctl{$_} = $bopctl{$_} = '';
144 $opctl{$_} = $bopctl{$_} = $c;
148 $_ = lc ($_) if $ignorecase;
168 # If no linkage is supplied in the @optionlist, copy it from
169 # the userlinkage if available.
170 if ( defined $userlinkage ) {
171 unless ( @optionlist > 0 && ref($optionlist[0]) ) {
172 if ( exists $userlinkage->{$linko} &&
173 ref($userlinkage->{$linko}) ) {
174 print STDERR ("=> found userlinkage for \"$linko\": ",
175 "$userlinkage->{$linko}\n")
177 unshift (@optionlist, $userlinkage->{$linko});
180 # Do nothing. Being undefined will be handled later.
186 # Copy the linkage. If omitted, link to global variable.
187 if ( @optionlist > 0 && ref($optionlist[0]) ) {
188 print STDERR ("=> link \"$linko\" to $optionlist[0]\n")
190 if ( ref($optionlist[0]) =~ /^(SCALAR|CODE)$/ ) {
191 $linkage{$linko} = shift (@optionlist);
193 elsif ( ref($optionlist[0]) =~ /^(ARRAY)$/ ) {
194 $linkage{$linko} = shift (@optionlist);
196 if $opctl{$o} ne '' and $opctl{$o} !~ /\@$/;
198 if $bundling and defined $bopctl{$o} and
199 $bopctl{$o} ne '' and $bopctl{$o} !~ /\@$/;
201 elsif ( ref($optionlist[0]) =~ /^(HASH)$/ ) {
202 $linkage{$linko} = shift (@optionlist);
204 if $opctl{$o} ne '' and $opctl{$o} !~ /\%$/;
206 if $bundling and defined $bopctl{$o} and
207 $bopctl{$o} ne '' and $bopctl{$o} !~ /\%$/;
210 $error .= "Invalid option linkage for \"$opt\"\n";
214 # Link to global $opt_XXX variable.
215 # Make sure a valid perl identifier results.
219 print STDERR ("=> link \"$linko\" to \@$pkg","::opt_$ov\n")
221 eval ("\$linkage{\$linko} = \\\@".$pkg."::opt_$ov;");
223 elsif ( $c =~ /%/ ) {
224 print STDERR ("=> link \"$linko\" to \%$pkg","::opt_$ov\n")
226 eval ("\$linkage{\$linko} = \\\%".$pkg."::opt_$ov;");
229 print STDERR ("=> link \"$linko\" to \$$pkg","::opt_$ov\n")
231 eval ("\$linkage{\$linko} = \\\$".$pkg."::opt_$ov;");
236 # Bail out if errors found.
237 die ($error) if $error;
240 # Sort the possible long option names.
241 @opctl = sort(keys (%opctl)) if $autoabbrev;
243 # Show the options tables if debugging.
247 while ( ($k,$v) = each(%opctl) ) {
248 print STDERR ($arrow, "\$opctl{\"$k\"} = \"$v\"\n");
252 while ( ($k,$v) = each(%bopctl) ) {
253 print STDERR ($arrow, "\$bopctl{\"$k\"} = \"$v\"\n");
258 # Process argument list
260 while ( $goon && @ARGV > 0 ) {
262 #### Get next argument ####
264 $opt = shift (@ARGV);
265 print STDERR ("=> option \"", $opt, "\"\n") if $debug;
267 #### Determine what we have ####
269 # Double dash is option list terminator.
270 if ( $opt eq $argend ) {
271 # Finish. Push back accumulated arguments and return.
272 unshift (@ARGV, @ret)
273 if $order == $PERMUTE;
274 return ($error == 0);
278 my $found; # success status
279 my $dsttype; # destination type ('@' or '%')
280 my $incr; # destination increment
281 my $key; # key (if hash type)
282 my $arg; # option argument
284 ($found, $opt, $arg, $dsttype, $incr, $key) =
285 FindOption ($genprefix, $argend, $opt,
286 \%opctl, \%bopctl, \@opctl, \%aliases);
290 # FindOption undefines $opt in case of errors.
291 next unless defined $opt;
293 if ( defined $arg ) {
294 if ( defined $aliases{$opt} ) {
295 print STDERR ("=> alias \"$opt\" -> \"$aliases{$opt}\"\n")
297 $opt = $aliases{$opt};
300 if ( defined $linkage{$opt} ) {
301 print STDERR ("=> ref(\$L{$opt}) -> ",
302 ref($linkage{$opt}), "\n") if $debug;
304 if ( ref($linkage{$opt}) eq 'SCALAR' ) {
306 print STDERR ("=> \$\$L{$opt} += \"$arg\"\n")
308 if ( defined ${$linkage{$opt}} ) {
309 ${$linkage{$opt}} += $arg;
312 ${$linkage{$opt}} = $arg;
316 print STDERR ("=> \$\$L{$opt} = \"$arg\"\n")
318 ${$linkage{$opt}} = $arg;
321 elsif ( ref($linkage{$opt}) eq 'ARRAY' ) {
322 print STDERR ("=> push(\@{\$L{$opt}, \"$arg\")\n")
324 push (@{$linkage{$opt}}, $arg);
326 elsif ( ref($linkage{$opt}) eq 'HASH' ) {
327 print STDERR ("=> \$\$L{$opt}->{$key} = \"$arg\"\n")
329 $linkage{$opt}->{$key} = $arg;
331 elsif ( ref($linkage{$opt}) eq 'CODE' ) {
332 print STDERR ("=> &L{$opt}(\"$opt\", \"$arg\")\n")
336 &{$linkage{$opt}}($opt, $arg);
338 print STDERR ("=> die($@)\n") if $debug && $@ ne '';
340 if ( $@ =~ /^!FINISH\b/ ) {
350 print STDERR ("Invalid REF type \"", ref($linkage{$opt}),
352 Croak ("Getopt::Long -- internal error!\n");
355 # No entry in linkage means entry in userlinkage.
356 elsif ( $dsttype eq '@' ) {
357 if ( defined $userlinkage->{$opt} ) {
358 print STDERR ("=> push(\@{\$L{$opt}}, \"$arg\")\n")
360 push (@{$userlinkage->{$opt}}, $arg);
363 print STDERR ("=>\$L{$opt} = [\"$arg\"]\n")
365 $userlinkage->{$opt} = [$arg];
368 elsif ( $dsttype eq '%' ) {
369 if ( defined $userlinkage->{$opt} ) {
370 print STDERR ("=> \$L{$opt}->{$key} = \"$arg\"\n")
372 $userlinkage->{$opt}->{$key} = $arg;
375 print STDERR ("=>\$L{$opt} = {$key => \"$arg\"}\n")
377 $userlinkage->{$opt} = {$key => $arg};
382 print STDERR ("=> \$L{$opt} += \"$arg\"\n")
384 if ( defined $userlinkage->{$opt} ) {
385 $userlinkage->{$opt} += $arg;
388 $userlinkage->{$opt} = $arg;
392 print STDERR ("=>\$L{$opt} = \"$arg\"\n") if $debug;
393 $userlinkage->{$opt} = $arg;
399 # Not an option. Save it if we $PERMUTE and don't have a <>.
400 elsif ( $order == $PERMUTE ) {
401 # Try non-options call-back.
403 if ( (defined ($cb = $linkage{'<>'})) ) {
408 print STDERR ("=> die($@)\n") if $debug && $@ ne '';
410 if ( $@ =~ /^!FINISH\b/ ) {
420 print STDERR ("=> saving \"$tryopt\" ",
421 "(not an option, may permute)\n") if $debug;
422 push (@ret, $tryopt);
427 # ...otherwise, terminate.
429 # Push this one back and exit.
430 unshift (@ARGV, $tryopt);
431 return ($error == 0);
437 if ( $order == $PERMUTE ) {
438 # Push back accumulated arguments
439 print STDERR ("=> restoring \"", join('" "', @ret), "\"\n")
440 if $debug && @ret > 0;
441 unshift (@ARGV, @ret) if @ret > 0;
444 return ($error == 0);
447 # end of Getopt::Long::GetOptions