1 # NOTE: Derived from lib/Getopt/Long.pm.
2 # Changes made here will be lost when autosplit again.
6 #line 657 "lib/Getopt/Long.pm (autosplit into lib/auto/Getopt/Long/FindOption.al)"
8 sub FindOption ($$$$$$$) {
10 # returns (1, $opt, $arg, $dsttype, $incr, $key) if okay,
11 # returns (0) otherwise.
13 my ($prefix, $argend, $opt, $opctl, $bopctl, $names, $aliases) = @_;
14 my $key; # hash key for a hash option
17 print STDERR ("=> find \"$opt\", prefix=\"$prefix\"\n") if $debug;
19 return 0 unless $opt =~ /^$prefix(.*)$/s;
20 return 0 if $opt eq "-" && !defined $opctl->{""};
25 print STDERR ("=> split \"$starter\"+\"$opt\"\n") if $debug;
27 my $optarg = undef; # value supplied with --opt=value
28 my $rest = undef; # remainder from unbundling
30 # If it is a long option, it may include the value.
31 if (($starter eq "--" || ($getopt_compat && !$bundling))
32 && $opt =~ /^([^=]+)=(.*)$/s ) {
35 print STDERR ("=> option \"", $opt,
36 "\", optarg = \"$optarg\"\n") if $debug;
41 my $tryopt = $opt; # option to try
42 my $optbl = $opctl; # table to look it up (long names)
47 if ( $bundling && $starter eq '-' ) {
48 # Unbundle single letter option.
49 $rest = length ($tryopt) > 0 ? substr ($tryopt, 1) : "";
50 $tryopt = substr ($tryopt, 0, 1);
51 $tryopt = lc ($tryopt) if $ignorecase > 1;
52 print STDERR ("=> $starter$tryopt unbundled from ",
53 "$starter$tryopt$rest\n") if $debug;
54 $rest = undef unless $rest ne '';
55 $optbl = $bopctl; # look it up in the short names table
57 # If bundling == 2, long options can override bundles.
58 if ( $bundling == 2 and
60 defined ($type = $opctl->{$tryopt.$rest}) ) {
61 print STDERR ("=> $starter$tryopt rebundled to ",
62 "$starter$tryopt$rest\n") if $debug;
68 # Try auto-abbreviation.
69 elsif ( $autoabbrev ) {
70 # Downcase if allowed.
71 $tryopt = $opt = lc ($opt) if $ignorecase;
72 # Turn option name into pattern.
73 my $pat = quotemeta ($opt);
74 # Look up in option names.
75 my @hits = grep (/^$pat/, @{$names});
76 print STDERR ("=> ", scalar(@hits), " hits (@hits) with \"$pat\" ",
77 "out of ", scalar(@{$names}), "\n") if $debug;
79 # Check for ambiguous results.
80 unless ( (@hits <= 1) || (grep ($_ eq $opt, @hits) == 1) ) {
81 # See if all matches are for the same option.
84 $_ = $aliases->{$_} if defined $aliases->{$_};
87 # Now see if it really is ambiguous.
88 unless ( keys(%hit) == 1 ) {
89 return (0) if $passthrough;
90 warn ("Option ", $opt, " is ambiguous (",
91 join(", ", @hits), ")\n");
94 return (1, $opt,$arg,$dsttype,$incr,$key);
99 # Complete the option name, if appropriate.
100 if ( @hits == 1 && $hits[0] ne $opt ) {
102 $tryopt = lc ($tryopt) if $ignorecase;
103 print STDERR ("=> option \"$opt\" -> \"$tryopt\"\n")
108 # Map to all lowercase if ignoring case.
109 elsif ( $ignorecase ) {
113 # Check validity by fetching the info.
114 $type = $optbl->{$tryopt} unless defined $type;
115 unless ( defined $type ) {
116 return (0) if $passthrough;
117 warn ("Unknown option: ", $opt, "\n");
119 return (1, $opt,$arg,$dsttype,$incr,$key);
123 print STDERR ("=> found \"$type\" for \"", $opt, "\"\n") if $debug;
125 #### Determine argument status ####
127 # If it is an option w/o argument, we're almost finished with it.
128 if ( $type eq '' || $type eq '!' || $type eq '+' ) {
129 if ( defined $optarg ) {
130 return (0) if $passthrough;
131 warn ("Option ", $opt, " does not take an argument\n");
135 elsif ( $type eq '' || $type eq '+' ) {
136 $arg = 1; # supply explicit value
137 $incr = $type eq '+';
140 substr ($opt, 0, 2) = ''; # strip NO prefix
141 $arg = 0; # supply explicit value
143 unshift (@ARGV, $starter.$rest) if defined $rest;
144 return (1, $opt,$arg,$dsttype,$incr,$key);
147 # Get mandatory status and type info.
149 ($mand, $type, $dsttype, $key) = $type =~ /^(.)(.)([@%]?)$/;
151 # Check if there is an option argument available.
153 return (1, $opt, $optarg, $dsttype, $incr, $key)
155 return (1, $opt, $type eq "s" ? '' : 0, $dsttype, $incr, $key)
159 # Check if there is an option argument available.
162 : !(defined $rest || @ARGV > 0) ) {
163 # Complain if this option needs an argument.
164 if ( $mand eq "=" ) {
165 return (0) if $passthrough;
166 warn ("Option ", $opt, " requires an argument\n");
170 return (1, $opt, $type eq "s" ? '' : 0, $dsttype, $incr, $key);
173 # Get (possibly optional) argument.
174 $arg = (defined $rest ? $rest
175 : (defined $optarg ? $optarg : shift (@ARGV)));
177 # Get key if this is a "name=value" pair for a hash option.
179 if ($dsttype eq '%' && defined $arg) {
180 ($key, $arg) = ($arg =~ /^([^=]*)=(.*)$/s) ? ($1, $2) : ($arg, 1);
183 #### Check if the argument is valid for this option ####
185 if ( $type eq "s" ) { # string
186 # A mandatory string takes anything.
187 return (1, $opt,$arg,$dsttype,$incr,$key) if $mand eq "=";
189 # An optional string takes almost anything.
190 return (1, $opt,$arg,$dsttype,$incr,$key)
191 if defined $optarg || defined $rest;
192 return (1, $opt,$arg,$dsttype,$incr,$key) if $arg eq "-"; # ??
194 # Check for option or option list terminator.
195 if ($arg eq $argend ||
196 $arg =~ /^$prefix.+/) {
198 unshift (@ARGV, $arg);
199 # Supply empty value.
204 elsif ( $type eq "n" || $type eq "i" ) { # numeric/integer
205 if ( $bundling && defined $rest && $rest =~ /^([-+]?[0-9]+)(.*)$/s ) {
208 unshift (@ARGV, $starter.$rest) if defined $rest && $rest ne '';
210 elsif ( $arg !~ /^[-+]?[0-9]+$/ ) {
211 if ( defined $optarg || $mand eq "=" ) {
212 if ( $passthrough ) {
213 unshift (@ARGV, defined $rest ? $starter.$rest : $arg)
214 unless defined $optarg;
217 warn ("Value \"", $arg, "\" invalid for option ",
218 $opt, " (number expected)\n");
222 unshift (@ARGV, $starter.$rest) if defined $rest;
226 unshift (@ARGV, defined $rest ? $starter.$rest : $arg);
227 # Supply default value.
233 elsif ( $type eq "f" ) { # real number, int is also ok
234 # We require at least one digit before a point or 'e',
235 # and at least one digit following the point and 'e'.
237 if ( $bundling && defined $rest &&
238 $rest =~ /^([-+]?[0-9]+(\.[0-9]+)?([eE][-+]?[0-9]+)?)(.*)$/s ) {
241 unshift (@ARGV, $starter.$rest) if defined $rest && $rest ne '';
243 elsif ( $arg !~ /^[-+]?[0-9.]+(\.[0-9]+)?([eE][-+]?[0-9]+)?$/ ) {
244 if ( defined $optarg || $mand eq "=" ) {
245 if ( $passthrough ) {
246 unshift (@ARGV, defined $rest ? $starter.$rest : $arg)
247 unless defined $optarg;
250 warn ("Value \"", $arg, "\" invalid for option ",
251 $opt, " (real number expected)\n");
255 unshift (@ARGV, $starter.$rest) if defined $rest;
259 unshift (@ARGV, defined $rest ? $starter.$rest : $arg);
260 # Supply default value.
266 Croak ("GetOpt::Long internal error (Can't happen)\n");
268 return (1, $opt, $arg, $dsttype, $incr, $key);
271 # end of Getopt::Long::FindOption