📄 long.pm
字号:
}
# Not an option. Save it if we $PERMUTE and don't have a <>.
elsif ( $order == $PERMUTE ) {
# Try non-options call-back.
my $cb;
if ( (defined ($cb = $linkage{'<>'})) ) {
&$cb ($tryopt);
}
else {
print STDERR ("=> saving \"$tryopt\" ",
"(not an option, may permute)\n") if $debug;
push (@ret, $tryopt);
}
next;
}
# ...otherwise, terminate.
else {
# Push this one back and exit.
unshift (@ARGV, $tryopt);
return ($error == 0);
}
}
# Finish.
if ( $order == $PERMUTE ) {
# Push back accumulated arguments
print STDERR ("=> restoring \"", join('" "', @ret), "\"\n")
if $debug && @ret > 0;
unshift (@ARGV, @ret) if @ret > 0;
}
return ($error == 0);
}
# Option lookup.
sub FindOption ($$$$$$$) {
# returns (1, $opt, $arg, $dsttype, $incr, $key) if okay,
# returns (0) otherwise.
my ($prefix, $argend, $opt, $opctl, $bopctl, $names, $aliases) = @_;
my $key; # hash key for a hash option
my $arg;
print STDERR ("=> find \"$opt\", prefix=\"$prefix\"\n") if $debug;
return (0) unless $opt =~ /^$prefix(.*)$/s;
$opt = $+;
my ($starter) = $1;
print STDERR ("=> split \"$starter\"+\"$opt\"\n") if $debug;
my $optarg = undef; # value supplied with --opt=value
my $rest = undef; # remainder from unbundling
# If it is a long option, it may include the value.
if (($starter eq "--" || ($getopt_compat && !$bundling))
&& $opt =~ /^([^=]+)=(.*)$/s ) {
$opt = $1;
$optarg = $2;
print STDERR ("=> option \"", $opt,
"\", optarg = \"$optarg\"\n") if $debug;
}
#### Look it up ###
my $tryopt = $opt; # option to try
my $optbl = $opctl; # table to look it up (long names)
my $type;
my $dsttype = '';
my $incr = 0;
if ( $bundling && $starter eq '-' ) {
# Unbundle single letter option.
$rest = substr ($tryopt, 1);
$tryopt = substr ($tryopt, 0, 1);
$tryopt = lc ($tryopt) if $ignorecase > 1;
print STDERR ("=> $starter$tryopt unbundled from ",
"$starter$tryopt$rest\n") if $debug;
$rest = undef unless $rest ne '';
$optbl = $bopctl; # look it up in the short names table
# If bundling == 2, long options can override bundles.
if ( $bundling == 2 and
defined ($type = $opctl->{$tryopt.$rest}) ) {
print STDERR ("=> $starter$tryopt rebundled to ",
"$starter$tryopt$rest\n") if $debug;
$tryopt .= $rest;
undef $rest;
}
}
# Try auto-abbreviation.
elsif ( $autoabbrev ) {
# Downcase if allowed.
$tryopt = $opt = lc ($opt) if $ignorecase;
# Turn option name into pattern.
my $pat = quotemeta ($opt);
# Look up in option names.
my @hits = grep (/^$pat/, @{$names});
print STDERR ("=> ", scalar(@hits), " hits (@hits) with \"$pat\" ",
"out of ", scalar(@{$names}), "\n") if $debug;
# Check for ambiguous results.
unless ( (@hits <= 1) || (grep ($_ eq $opt, @hits) == 1) ) {
# See if all matches are for the same option.
my %hit;
foreach ( @hits ) {
$_ = $aliases->{$_} if defined $aliases->{$_};
$hit{$_} = 1;
}
# Now see if it really is ambiguous.
unless ( keys(%hit) == 1 ) {
return (0) if $passthrough;
warn ("Option ", $opt, " is ambiguous (",
join(", ", @hits), ")\n");
$error++;
undef $opt;
return (1, $opt,$arg,$dsttype,$incr,$key);
}
@hits = keys(%hit);
}
# Complete the option name, if appropriate.
if ( @hits == 1 && $hits[0] ne $opt ) {
$tryopt = $hits[0];
$tryopt = lc ($tryopt) if $ignorecase;
print STDERR ("=> option \"$opt\" -> \"$tryopt\"\n")
if $debug;
}
}
# Map to all lowercase if ignoring case.
elsif ( $ignorecase ) {
$tryopt = lc ($opt);
}
# Check validity by fetching the info.
$type = $optbl->{$tryopt} unless defined $type;
unless ( defined $type ) {
return (0) if $passthrough;
warn ("Unknown option: ", $opt, "\n");
$error++;
return (1, $opt,$arg,$dsttype,$incr,$key);
}
# Apparently valid.
$opt = $tryopt;
print STDERR ("=> found \"$type\" for ", $opt, "\n") if $debug;
#### Determine argument status ####
# If it is an option w/o argument, we're almost finished with it.
if ( $type eq '' || $type eq '!' || $type eq '+' ) {
if ( defined $optarg ) {
return (0) if $passthrough;
warn ("Option ", $opt, " does not take an argument\n");
$error++;
undef $opt;
}
elsif ( $type eq '' || $type eq '+' ) {
$arg = 1; # supply explicit value
$incr = $type eq '+';
}
else {
substr ($opt, 0, 2) = ''; # strip NO prefix
$arg = 0; # supply explicit value
}
unshift (@ARGV, $starter.$rest) if defined $rest;
return (1, $opt,$arg,$dsttype,$incr,$key);
}
# Get mandatory status and type info.
my $mand;
($mand, $type, $dsttype, $key) = $type =~ /^(.)(.)([@%]?)$/;
# Check if there is an option argument available.
if ( defined $optarg ? ($optarg eq '')
: !(defined $rest || @ARGV > 0) ) {
# Complain if this option needs an argument.
if ( $mand eq "=" ) {
return (0) if $passthrough;
warn ("Option ", $opt, " requires an argument\n");
$error++;
undef $opt;
}
if ( $mand eq ":" ) {
$arg = $type eq "s" ? '' : 0;
}
return (1, $opt,$arg,$dsttype,$incr,$key);
}
# Get (possibly optional) argument.
$arg = (defined $rest ? $rest
: (defined $optarg ? $optarg : shift (@ARGV)));
# Get key if this is a "name=value" pair for a hash option.
$key = undef;
if ($dsttype eq '%' && defined $arg) {
($key, $arg) = ($arg =~ /^(.*)=(.*)$/s) ? ($1, $2) : ($arg, 1);
}
#### Check if the argument is valid for this option ####
if ( $type eq "s" ) { # string
# A mandatory string takes anything.
return (1, $opt,$arg,$dsttype,$incr,$key) if $mand eq "=";
# An optional string takes almost anything.
return (1, $opt,$arg,$dsttype,$incr,$key)
if defined $optarg || defined $rest;
return (1, $opt,$arg,$dsttype,$incr,$key) if $arg eq "-"; # ??
# Check for option or option list terminator.
if ($arg eq $argend ||
$arg =~ /^$prefix.+/) {
# Push back.
unshift (@ARGV, $arg);
# Supply empty value.
$arg = '';
}
}
elsif ( $type eq "n" || $type eq "i" ) { # numeric/integer
if ( $bundling && defined $rest && $rest =~ /^(-?[0-9]+)(.*)$/s ) {
$arg = $1;
$rest = $2;
unshift (@ARGV, $starter.$rest) if defined $rest && $rest ne '';
}
elsif ( $arg !~ /^-?[0-9]+$/ ) {
if ( defined $optarg || $mand eq "=" ) {
if ( $passthrough ) {
unshift (@ARGV, defined $rest ? $starter.$rest : $arg)
unless defined $optarg;
return (0);
}
warn ("Value \"", $arg, "\" invalid for option ",
$opt, " (number expected)\n");
$error++;
undef $opt;
# Push back.
unshift (@ARGV, $starter.$rest) if defined $rest;
}
else {
# Push back.
unshift (@ARGV, defined $rest ? $starter.$rest : $arg);
# Supply default value.
$arg = 0;
}
}
}
elsif ( $type eq "f" ) { # real number, int is also ok
# We require at least one digit before a point or 'e',
# and at least one digit following the point and 'e'.
# [-]NN[.NN][eNN]
if ( $bundling && defined $rest &&
$rest =~ /^(-?[0-9]+(\.[0-9]+)?([eE]-?[0-9]+)?)(.*)$/s ) {
$arg = $1;
$rest = $+;
unshift (@ARGV, $starter.$rest) if defined $rest && $rest ne '';
}
elsif ( $arg !~ /^-?[0-9.]+(\.[0-9]+)?([eE]-?[0-9]+)?$/ ) {
if ( defined $optarg || $mand eq "=" ) {
if ( $passthrough ) {
unshift (@ARGV, defined $rest ? $starter.$rest : $arg)
unless defined $optarg;
return (0);
}
warn ("Value \"", $arg, "\" invalid for option ",
$opt, " (real number expected)\n");
$error++;
undef $opt;
# Push back.
unshift (@ARGV, $starter.$rest) if defined $rest;
}
else {
# Push back.
unshift (@ARGV, defined $rest ? $starter.$rest : $arg);
# Supply default value.
$arg = 0.0;
}
}
}
else {
Croak ("GetOpt::Long internal error (Can't happen)\n");
}
return (1, $opt, $arg, $dsttype, $incr, $key);
}
# Getopt::Long Configuration.
sub Configure (@) {
my (@options) = @_;
my $opt;
foreach $opt ( @options ) {
my $try = lc ($opt);
my $action = 1;
if ( $try =~ /^no_?(.*)$/s ) {
$action = 0;
$try = $+;
}
if ( $try eq 'default' or $try eq 'defaults' ) {
ConfigDefaults () if $action;
}
elsif ( $try eq 'auto_abbrev' or $try eq 'autoabbrev' ) {
$autoabbrev = $action;
}
elsif ( $try eq 'getopt_compat' ) {
$getopt_compat = $action;
}
elsif ( $try eq 'ignorecase' or $try eq 'ignore_case' ) {
$ignorecase = $action;
}
elsif ( $try eq 'ignore_case_always' ) {
$ignorecase = $action ? 2 : 0;
}
elsif ( $try eq 'bundling' ) {
$bundling = $action;
}
elsif ( $try eq 'bundling_override' ) {
$bundling = $action ? 2 : 0;
}
elsif ( $try eq 'require_order' ) {
$order = $action ? $REQUIRE_ORDER : $PERMUTE;
}
elsif ( $try eq 'permute' ) {
$order = $action ? $PERMUTE : $REQUIRE_ORDER;
}
elsif ( $try eq 'pass_through' or $try eq 'passthrough' ) {
$passthrough = $action;
}
elsif ( $try =~ /^prefix=(.+)$/ ) {
$genprefix = $1;
# Turn into regexp. Needs to be parenthesized!
$genprefix = "(" . quotemeta($genprefix) . ")";
eval { '' =~ /$genprefix/; };
Croak ("Getopt::Long: invalid pattern \"$genprefix\"") if $@;
}
elsif ( $try =~ /^prefix_pattern=(.+)$/ ) {
$genprefix = $1;
# Parenthesize if needed.
$genprefix = "(" . $genprefix . ")"
unless $genprefix =~ /^\(.*\)$/;
eval { '' =~ /$genprefix/; };
Croak ("Getopt::Long: invalid pattern \"$genprefix\"") if $@;
}
elsif ( $try eq 'debug' ) {
$debug = $action;
}
else {
Croak ("Getopt::Long: unknown config parameter \"$opt\"")
}
}
}
# Deprecated name.
sub config (@) {
Configure (@_);
}
# To prevent Carp from being loaded unnecessarily.
sub Croak (@) {
require 'Carp.pm';
$Carp::CarpLevel = 1;
Carp::croak(@_);
};
################ Documentation ################
=head1 NAME
GetOptions - extended processing of command line options
=head1 SYNOPSIS
use Getopt::Long;
$result = GetOptions (...option-descriptions...);
=head1 DESCRIPTION
The Getopt::Long module implements an extended getopt function called
GetOptions(). This function adheres to the POSIX syntax for command
line options, with GNU extensions. In general, this means that options
have long names instead of single letters, and are introduced with a
double dash "--". Support for bundling of command line options, as was
the case with the more traditional single-letter approach, is provided
but not enabled by default. For example, the UNIX "ps" command can be
given the command line "option"
-vax
which means the combination of B<-v>, B<-a> and B<-x>. With the new
syntax B<--vax> would be a single option, probably indicating a
computer architecture.
Command line options can be used to set values. These values can be
specified in one of two ways:
--size 24
--size=24
GetOptions is called with a list of option-descriptions, each of which
consists of two elements: the option specifier and the option linkage.
The option specifier defines the name of the option and, optionally,
the value it can take. The option linkage is usually a reference to a
variable that will be set when the option is used. For example, the
following call to GetOptions:
GetOptions("size=i" => \$offset);
will accept a command line option "size" that must have an integer
value. With a command line of "--size 24" this will cause the variable
$offset to get the value 24.
Alternatively, the first argument to GetOptions may be a reference to
a HASH describing the linkage for the options, or an object whose
class is based on a HASH. The following call is equivalent to the
example above:
%optctl = ("size" => \$offset);
GetOptions(\%optctl, "size=i");
Linkage may be specified using either of the above methods, or both.
Linkage specified in the argument list takes precedence over the
linkage specified in the HASH.
The command line options are taken from array @ARGV. Upon completion
of GetOptions, @ARGV will contain the rest (i.e. the non-options) of
the command line.
Each option specifier designates the name of the option, optionally
followed by an argument specifier.
Options that do not take arguments will have no argument specifier.
The option variable will be set to 1 if the option is used.
For the other options, the values for argument specifiers are:
=over 8
=item !
Option does not take an argument and may be negated, i.e. prefixed by
"no". E.g. "foo!" will allow B<--foo> (with value 1) and B<-nofoo>
(with value 0).
The option variable will be set to 1, or 0 if negated.
=item +
Option does not take an argument and will be incremented by 1 every
time it appears on the command line. E.g. "more+", when used with
B<--more --more --more>, will set the option variable to 3 (provided
it was 0 or undefined at first).
The B<+> specifier is ignored if the option destination is not a SCALAR.
=item =s
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -