📄 long.pm
字号:
=over 4=item !The option does not take an argument and may be negated by prefixingit with "no" or "no-". E.g. C<"foo!"> will allow C<--foo> (a value of1 will be assigned) as well as C<--nofoo> and C<--no-foo> (a value of0 will be assigned). If the option has aliases, this applies to thealiases as well.Using negation on a single letter option when bundling is in effect ispointless and will result in a warning.=item +The option does not take an argument and will be incremented by 1every time it appears on the command line. E.g. C<"more+">, when usedwith C<--more --more --more>, will increment the value three times,resulting in a value of 3 (provided it was 0 or undefined at first).The C<+> specifier is ignored if the option destination is not a scalar.=item = I<type> [ I<desttype> ] [ I<repeat> ]The option requires an argument of the given type. Supported typesare:=over 4=item sString. An arbitrary sequence of characters. It is valid for theargument to start with C<-> or C<-->.=item iInteger. An optional leading plus or minus sign, followed by asequence of digits.=item oExtended integer, Perl style. This can be either an optional leadingplus or minus sign, followed by a sequence of digits, or an octalstring (a zero, optionally followed by '0', '1', .. '7'), or ahexadecimal string (C<0x> followed by '0' .. '9', 'a' .. 'f', caseinsensitive), or a binary string (C<0b> followed by a series of '0'and '1').=item fReal number. For example C<3.14>, C<-6.23E24> and so on.=backThe I<desttype> can be C<@> or C<%> to specify that the option islist or a hash valued. This is only needed when the destination forthe option value is not otherwise specified. It should be omitted whennot needed.The I<repeat> specifies the number of values this option takes peroccurrence on the command line. It has the format C<{> [ I<min> ] [ C<,> [ I<max> ] ] C<}>.I<min> denotes the minimal number of arguments. It defaults to 1 foroptions with C<=> and to 0 for options with C<:>, see below. Note thatI<min> overrules the C<=> / C<:> semantics.I<max> denotes the maximum number of arguments. It must be at leastI<min>. If I<max> is omitted, I<but the comma is not>, there is noupper bound to the number of argument values taken.=item : I<type> [ I<desttype> ]Like C<=>, but designates the argument as optional.If omitted, an empty string will be assigned to string values options,and the value zero to numeric options.Note that if a string argument starts with C<-> or C<-->, it will beconsidered an option on itself.=item : I<number> [ I<desttype> ]Like C<:i>, but if the value is omitted, the I<number> will be assigned.=item : + [ I<desttype> ]Like C<:i>, but if the value is omitted, the current value for theoption will be incremented.=back=head1 Advanced Possibilities=head2 Object oriented interfaceGetopt::Long can be used in an object oriented way as well: use Getopt::Long; $p = new Getopt::Long::Parser; $p->configure(...configuration options...); if ($p->getoptions(...options descriptions...)) ...Configuration options can be passed to the constructor: $p = new Getopt::Long::Parser config => [...configuration options...];=head2 Thread SafetyGetopt::Long is thread safe when using ithreads as of Perl 5.8. It isI<not> thread safe when using the older (experimental and nowobsolete) threads implementation that was added to Perl 5.005.=head2 Documentation and help textsGetopt::Long encourages the use of Pod::Usage to produce helpmessages. For example: use Getopt::Long; use Pod::Usage; my $man = 0; my $help = 0; GetOptions('help|?' => \$help, man => \$man) or pod2usage(2); pod2usage(1) if $help; pod2usage(-exitstatus => 0, -verbose => 2) if $man; __END__ =head1 NAME sample - Using Getopt::Long and Pod::Usage =head1 SYNOPSIS sample [options] [file ...] Options: -help brief help message -man full documentation =head1 OPTIONS =over 8 =item B<-help> Print a brief help message and exits. =item B<-man> Prints the manual page and exits. =back =head1 DESCRIPTION B<This program> will read the given input file(s) and do something useful with the contents thereof. =cutSee L<Pod::Usage> for details.=head2 Storing option values in a hashSometimes, for example when there are a lot of options, having aseparate variable for each of them can be cumbersome. GetOptions()supports, as an alternative mechanism, storing options in a hash.To obtain this, a reference to a hash must be passed I<as the firstargument> to GetOptions(). For each option that is specified on thecommand line, the option value will be stored in the hash with theoption name as key. Options that are not actually used on the commandline will not be put in the hash, on other words,C<exists($h{option})> (or defined()) can be used to test if an optionwas used. The drawback is that warnings will be issued if the programruns under C<use strict> and uses C<$h{option}> without testing withexists() or defined() first. my %h = (); GetOptions (\%h, 'length=i'); # will store in $h{length}For options that take list or hash values, it is necessary to indicatethis by appending an C<@> or C<%> sign after the type: GetOptions (\%h, 'colours=s@'); # will push to @{$h{colours}}To make things more complicated, the hash may contain references tothe actual destinations, for example: my $len = 0; my %h = ('length' => \$len); GetOptions (\%h, 'length=i'); # will store in $lenThis example is fully equivalent with: my $len = 0; GetOptions ('length=i' => \$len); # will store in $lenAny mixture is possible. For example, the most frequently used optionscould be stored in variables while all other options get stored in thehash: my $verbose = 0; # frequently referred my $debug = 0; # frequently referred my %h = ('verbose' => \$verbose, 'debug' => \$debug); GetOptions (\%h, 'verbose', 'debug', 'filter', 'size=i'); if ( $verbose ) { ... } if ( exists $h{filter} ) { ... option 'filter' was specified ... }=head2 BundlingWith bundling it is possible to set several single-character optionsat once. For example if C<a>, C<v> and C<x> are all valid options, -vaxwould set all three.Getopt::Long supports two levels of bundling. To enable bundling, acall to Getopt::Long::Configure is required.The first level of bundling can be enabled with: Getopt::Long::Configure ("bundling");Configured this way, single-character options can be bundled but longoptions B<must> always start with a double dash C<--> to avoidambiguity. For example, when C<vax>, C<a>, C<v> and C<x> are all validoptions, -vaxwould set C<a>, C<v> and C<x>, but --vaxwould set C<vax>.The second level of bundling lifts this restriction. It can be enabledwith: Getopt::Long::Configure ("bundling_override");Now, C<-vax> would set the option C<vax>.When any level of bundling is enabled, option values may be insertedin the bundle. For example: -h24w80is equivalent to -h 24 -w 80When configured for bundling, single-character options are matchedcase sensitive while long options are matched case insensitive. Tohave the single-character options matched case insensitive as well,use: Getopt::Long::Configure ("bundling", "ignorecase_always");It goes without saying that bundling can be quite confusing.=head2 The lonesome dashNormally, a lone dash C<-> on the command line will not be consideredan option. Option processing will terminate (unless "permute" isconfigured) and the dash will be left in C<@ARGV>.It is possible to get special treatment for a lone dash. This can beachieved by adding an option specification with an empty name, forexample: GetOptions ('' => \$stdio);A lone dash on the command line will now be a legal option, and usingit will set variable C<$stdio>.=head2 Argument callbackA special option 'name' C<< <> >> can be used to designate a subroutineto handle non-option arguments. When GetOptions() encounters anargument that does not look like an option, it will immediately call thissubroutine and passes it one parameter: the argument name.For example: my $width = 80; sub process { ... } GetOptions ('width=i' => \$width, '<>' => \&process);When applied to the following command line: arg1 --width=72 arg2 --width=60 arg3This will callC<process("arg1")> while C<$width> is C<80>,C<process("arg2")> while C<$width> is C<72>, andC<process("arg3")> while C<$width> is C<60>.This feature requires configuration option B<permute>, see sectionL<Configuring Getopt::Long>.=head1 Configuring Getopt::LongGetopt::Long can be configured by calling subroutineGetopt::Long::Configure(). This subroutine takes a list of quotedstrings, each specifying a configuration option to be enabled, e.g.C<ignore_case>, or disabled, e.g. C<no_ignore_case>. Case does notmatter. Multiple calls to Configure() are possible.Alternatively, as of version 2.24, the configuration options may bepassed together with the C<use> statement: use Getopt::Long qw(:config no_ignore_case bundling);The following options are available:=over 12=item defaultThis option causes all configuration options to be reset to theirdefault values.=item posix_defaultThis option causes all configuration options to be reset to theirdefault values as if the environment variable POSIXLY_CORRECT hadbeen set.=item auto_abbrevAllow option names to be abbreviated to uniqueness.Default is enabled unless environment variablePOSIXLY_CORRECT has been set, in which case C<auto_abbrev> is disabled.=item getopt_compatAllow C<+> to start options.Default is enabled unless environment variablePOSIXLY_CORRECT has been set, in which case C<getopt_compat> is disabled.=item gnu_compatC<gnu_compat> controls whether C<--opt=> is allowed, and what it shoulddo. Without C<gnu_compat>, C<--opt=> gives an error. With C<gnu_compat>,C<--opt=> will give option C<opt> and empty value.This is the way GNU getopt_long() does it.=item gnu_getoptThis is a short way of setting C<gnu_compat> C<bundling> C<permute>C<no_getopt_compat>. With C<gnu_getopt>, command line handling should befully compatible with GNU getopt_long().=item require_orderWhether command line arguments are allowed to be mixed with options.Default is disabled unless environment variablePOSIXLY_CORRECT has been set, in which case C<require_order> is enabled.See also C<permute>, which is the opposite of C<require_order>.=item permuteWhether command line arguments are allowed to be mixed with options.Default is enabled unless environment variablePOSIXLY_CORRECT has been set, in which case C<permute> is disabled.Note that C<permute> is the opposite of C<require_order>.If C<permute> is enabled, this means that --foo arg1 --bar arg2 arg3is equivalent to --foo --bar arg1 arg2 arg3If an argument callback routine is specified, C<@ARGV> will always beempty upon successful return of GetOptions() since all options have beenprocessed. The only exception is when C<--> is used: --foo arg1 --bar arg2 -- arg3This will call the callback routine for arg1 and arg2, and thenterminate GetOptions() leaving C<"arg2"> in C<@ARGV>.If C<require_order> is enabled, options processingterminates when the first non-option is encountered. --foo arg1 --bar arg2 arg3is equivalent to --foo -- arg1 --bar arg2 arg3If C<pass_through> is also enabled, options processing will terminateat the first unrecognized option, or non-option, whichever comesfirst.=item bundling (default: disabled)Enabling this option will allow single-character options to bebundled. To distinguish bundles from long option names, long optionsI<must> be introduced with C<--> and bundles with C<->.Note that, if you have options C<a>, C<l> and C<all>, andauto_abbrev enabled, possible arguments and option settings are: using argument sets option(s) ------------------------------------------ -a, --a a -l, --l l -al, -la, -ala, -all,... a, l --al, --all allThe surprising part is that C<--a> sets option C<a> (due to autocompletion), not C<all>.Note: disabling C<bundling> also disables C<bundling_override>.=item bundling_override (default: disabled)If C<bundling_override> is enabled, bundling is enabled as withC<bundling> but now long option names override option bundles.Note: disabling C<bundling_override> also disables C<bundling>.B<Note:> Using option bundling can easily lead to unexpected results,especially when mixing long options and bundles. Caveat emptor.=item ignore_case (default: enabled)If enabled, case is ignor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -