📄 getopt::long.3
字号:
.Vb 2\& use Getopt::Long qw(GetOptionsFromArray);\& $ret = GetOptionsFromArray(\e@myopts, ...);.Ve.PPWhen used like this, the global \f(CW@ARGV\fR is not touched at all..PPThe following two calls behave identically:.PP.Vb 2\& $ret = GetOptions( ... );\& $ret = GetOptionsFromArray(\e@ARGV, ... );.Ve.Sh "Parsing options from an arbitrary string".IX Subsection "Parsing options from an arbitrary string"A special entry \f(CW\*(C`GetOptionsFromString\*(C'\fR can be used to parse optionsfrom an arbitrary string..PP.Vb 2\& use Getopt::Long qw(GetOptionsFromString);\& $ret = GetOptionsFromString($string, ...);.Ve.PPThe contents of the string are split into arguments using a call to\&\f(CW\*(C`Text::ParseWords::shellwords\*(C'\fR. As with \f(CW\*(C`GetOptionsFromArray\*(C'\fR, theglobal \f(CW@ARGV\fR is not touched..PPIt is possible that, upon completion, not all arguments in the stringhave been processed. \f(CW\*(C`GetOptionsFromString\*(C'\fR will, when called in listcontext, return both the return status and an array reference to anyremaining arguments:.PP.Vb 1\& ($ret, $args) = GetOptionsFromString($string, ... );.Ve.PPIf any arguments remain, and \f(CW\*(C`GetOptionsFromString\*(C'\fR was not called inlist context, a message will be given and \f(CW\*(C`GetOptionsFromString\*(C'\fR willreturn failure..Sh "Storing options values in a hash".IX Subsection "Storing options values in a hash"Sometimes, for example when there are a lot of options, having aseparate variable for each of them can be cumbersome. \fIGetOptions()\fRsupports, as an alternative mechanism, storing options values in ahash..PPTo obtain this, a reference to a hash must be passed \fIas the firstargument\fR to \fIGetOptions()\fR. 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,\&\f(CW\*(C`exists($h{option})\*(C'\fR (or \fIdefined()\fR) can be used to test if an optionwas used. The drawback is that warnings will be issued if the programruns under \f(CW\*(C`use strict\*(C'\fR and uses \f(CW$h{option}\fR without testing with\&\fIexists()\fR or \fIdefined()\fR first..PP.Vb 2\& my %h = ();\& GetOptions (\e%h, \*(Aqlength=i\*(Aq); # will store in $h{length}.Ve.PPFor options that take list or hash values, it is necessary to indicatethis by appending an \f(CW\*(C`@\*(C'\fR or \f(CW\*(C`%\*(C'\fR sign after the type:.PP.Vb 1\& GetOptions (\e%h, \*(Aqcolours=s@\*(Aq); # will push to @{$h{colours}}.Ve.PPTo make things more complicated, the hash may contain references tothe actual destinations, for example:.PP.Vb 3\& my $len = 0;\& my %h = (\*(Aqlength\*(Aq => \e$len);\& GetOptions (\e%h, \*(Aqlength=i\*(Aq); # will store in $len.Ve.PPThis example is fully equivalent with:.PP.Vb 2\& my $len = 0;\& GetOptions (\*(Aqlength=i\*(Aq => \e$len); # will store in $len.Ve.PPAny mixture is possible. For example, the most frequently used optionscould be stored in variables while all other options get stored in thehash:.PP.Vb 6\& my $verbose = 0; # frequently referred\& my $debug = 0; # frequently referred\& my %h = (\*(Aqverbose\*(Aq => \e$verbose, \*(Aqdebug\*(Aq => \e$debug);\& GetOptions (\e%h, \*(Aqverbose\*(Aq, \*(Aqdebug\*(Aq, \*(Aqfilter\*(Aq, \*(Aqsize=i\*(Aq);\& if ( $verbose ) { ... }\& if ( exists $h{filter} ) { ... option \*(Aqfilter\*(Aq was specified ... }.Ve.Sh "Bundling".IX Subsection "Bundling"With bundling it is possible to set several single-character optionsat once. For example if \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`v\*(C'\fR and \f(CW\*(C`x\*(C'\fR are all valid options,.PP.Vb 1\& \-vax.Ve.PPwould set all three..PPGetopt::Long supports two levels of bundling. To enable bundling, acall to Getopt::Long::Configure is required..PPThe first level of bundling can be enabled with:.PP.Vb 1\& Getopt::Long::Configure ("bundling");.Ve.PPConfigured this way, single-character options can be bundled but longoptions \fBmust\fR always start with a double dash \f(CW\*(C`\-\-\*(C'\fR to avoidambiguity. For example, when \f(CW\*(C`vax\*(C'\fR, \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`v\*(C'\fR and \f(CW\*(C`x\*(C'\fR are all validoptions,.PP.Vb 1\& \-vax.Ve.PPwould set \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`v\*(C'\fR and \f(CW\*(C`x\*(C'\fR, but.PP.Vb 1\& \-\-vax.Ve.PPwould set \f(CW\*(C`vax\*(C'\fR..PPThe second level of bundling lifts this restriction. It can be enabledwith:.PP.Vb 1\& Getopt::Long::Configure ("bundling_override");.Ve.PPNow, \f(CW\*(C`\-vax\*(C'\fR would set the option \f(CW\*(C`vax\*(C'\fR..PPWhen any level of bundling is enabled, option values may be insertedin the bundle. For example:.PP.Vb 1\& \-h24w80.Ve.PPis equivalent to.PP.Vb 1\& \-h 24 \-w 80.Ve.PPWhen 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:.PP.Vb 1\& Getopt::Long::Configure ("bundling", "ignorecase_always");.Ve.PPIt goes without saying that bundling can be quite confusing..Sh "The lonesome dash".IX Subsection "The lonesome dash"Normally, a lone dash \f(CW\*(C`\-\*(C'\fR on the command line will not be consideredan option. Option processing will terminate (unless \*(L"permute\*(R" isconfigured) and the dash will be left in \f(CW@ARGV\fR..PPIt is possible to get special treatment for a lone dash. This can beachieved by adding an option specification with an empty name, forexample:.PP.Vb 1\& GetOptions (\*(Aq\*(Aq => \e$stdio);.Ve.PPA lone dash on the command line will now be a legal option, and usingit will set variable \f(CW$stdio\fR..Sh "Argument callback".IX Subsection "Argument callback"A special option 'name' \f(CW\*(C`<>\*(C'\fR can be used to designate a subroutineto handle non-option arguments. When \fIGetOptions()\fR encounters anargument that does not look like an option, it will immediately call thissubroutine and passes it one parameter: the argument name..PPFor example:.PP.Vb 3\& my $width = 80;\& sub process { ... }\& GetOptions (\*(Aqwidth=i\*(Aq => \e$width, \*(Aq<>\*(Aq => \e&process);.Ve.PPWhen applied to the following command line:.PP.Vb 1\& arg1 \-\-width=72 arg2 \-\-width=60 arg3.Ve.PPThis will call\&\f(CW\*(C`process("arg1")\*(C'\fR while \f(CW$width\fR is \f(CW80\fR,\&\f(CW\*(C`process("arg2")\*(C'\fR while \f(CW$width\fR is \f(CW72\fR, and\&\f(CW\*(C`process("arg3")\*(C'\fR while \f(CW$width\fR is \f(CW60\fR..PPThis feature requires configuration option \fBpermute\fR, see section\&\*(L"Configuring Getopt::Long\*(R"..SH "Configuring Getopt::Long".IX Header "Configuring Getopt::Long"Getopt::Long can be configured by calling subroutine\&\fIGetopt::Long::Configure()\fR. This subroutine takes a list of quotedstrings, each specifying a configuration option to be enabled, e.g.\&\f(CW\*(C`ignore_case\*(C'\fR, or disabled, e.g. \f(CW\*(C`no_ignore_case\*(C'\fR. Case does notmatter. Multiple calls to \fIConfigure()\fR are possible..PPAlternatively, as of version 2.24, the configuration options may bepassed together with the \f(CW\*(C`use\*(C'\fR statement:.PP.Vb 1\& use Getopt::Long qw(:config no_ignore_case bundling);.Ve.PPThe following options are available:.IP "default" 12.IX Item "default"This option causes all configuration options to be reset to theirdefault values..IP "posix_default" 12.IX Item "posix_default"This option causes all configuration options to be reset to theirdefault values as if the environment variable \s-1POSIXLY_CORRECT\s0 hadbeen set..IP "auto_abbrev" 12.IX Item "auto_abbrev"Allow option names to be abbreviated to uniqueness.Default is enabled unless environment variable\&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`auto_abbrev\*(C'\fR is disabled..IP "getopt_compat" 12.IX Item "getopt_compat"Allow \f(CW\*(C`+\*(C'\fR to start options.Default is enabled unless environment variable\&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`getopt_compat\*(C'\fR is disabled..IP "gnu_compat" 12.IX Item "gnu_compat"\&\f(CW\*(C`gnu_compat\*(C'\fR controls whether \f(CW\*(C`\-\-opt=\*(C'\fR is allowed, and what it shoulddo. Without \f(CW\*(C`gnu_compat\*(C'\fR, \f(CW\*(C`\-\-opt=\*(C'\fR gives an error. With \f(CW\*(C`gnu_compat\*(C'\fR,\&\f(CW\*(C`\-\-opt=\*(C'\fR will give option \f(CW\*(C`opt\*(C'\fR and empty value.This is the way \s-1GNU\s0 \fIgetopt_long()\fR does it..IP "gnu_getopt" 12.IX Item "gnu_getopt"This is a short way of setting \f(CW\*(C`gnu_compat\*(C'\fR \f(CW\*(C`bundling\*(C'\fR \f(CW\*(C`permute\*(C'\fR\&\f(CW\*(C`no_getopt_compat\*(C'\fR. With \f(CW\*(C`gnu_getopt\*(C'\fR, command line handling should befully compatible with \s-1GNU\s0 \fIgetopt_long()\fR..IP "require_order" 12.IX Item "require_order"Whether command line arguments are allowed to be mixed with options.Default is disabled unless environment variable\&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`require_order\*(C'\fR is enabled..SpSee also \f(CW\*(C`permute\*(C'\fR, which is the opposite of \f(CW\*(C`require_order\*(C'\fR..IP "permute" 12.IX Item "permute"Whether command line arguments are allowed to be mixed with options.Default is enabled unless environment variable\&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`permute\*(C'\fR is disabled.Note that \f(CW\*(C`permute\*(C'\fR is the opposite of \f(CW\*(C`require_order\*(C'\fR..SpIf \f(CW\*(C`permute\*(C'\fR is enabled, this means that.Sp.Vb 1\& \-\-foo arg1 \-\-bar arg2 arg3.Ve.Spis equivalent to.Sp.Vb 1\& \-\-foo \-\-bar arg1 arg2 arg3.Ve.SpIf an argument callback routine is specified, \f(CW@ARGV\fR will always beempty upon successful return of \fIGetOptions()\fR since all options have beenprocessed. The only exception is when \f(CW\*(C`\-\-\*(C'\fR is used:.Sp.Vb 1\& \-\-foo arg1 \-\-bar arg2 \-\- arg3.Ve.SpThis will call the callback routine for arg1 and arg2, and thenterminate \fIGetOptions()\fR leaving \f(CW"arg3"\fR in \f(CW@ARGV\fR..SpIf \f(CW\*(C`require_order\*(C'\fR is enabled, options processingterminates when the first non-option is encountered..Sp.Vb 1\& \-\-foo arg1 \-\-bar arg2 arg3.Ve.Spis equivalent to.Sp.Vb 1\& \-\-foo \-\- arg1 \-\-bar arg2 arg3.Ve.SpIf \f(CW\*(C`pass_through\*(C'\fR is also enabled, options processing will terminateat the first unrecognized option, or non-option, whichever comesfirst..IP "bundling (default: disabled)" 12.IX Item "bundling (default: disabled)"Enabling this option will allow single-character options to bebundled. To distinguish bundles from long option names, long options\&\fImust\fR be introduced with \f(CW\*(C`\-\-\*(C'\fR and bundles with \f(CW\*(C`\-\*(C'\fR..SpNote that, if you have options \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`l\*(C'\fR and \f(CW\*(C`all\*(C'\fR, andauto_abbrev enabled, possible arguments and option settings are:.Sp.Vb 6\& using argument sets option(s)\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& \-a, \-\-a a\& \-l, \-\-l l\& \-al, \-la, \-ala, \-all,... a, l\& \-\-al, \-\-all all.Ve.SpThe surprising part is that \f(CW\*(C`\-\-a\*(C'\fR sets option \f(CW\*(C`a\*(C'\fR (due to autocompletion), not \f(CW\*(C`all\*(C'\fR..SpNote: disabling \f(CW\*(C`bundling\*(C'\fR also disables \f(CW\*(C`bundling_override\*(C'\fR..IP "bundling_override (default: disabled)" 12.IX Item "bundling_override (default: disabled)"If \f(CW\*(C`bundling_override\*(C'\fR is enabled, bundling is enabled as with\&\f(CW\*(C`bundling\*(C'\fR but now long option names override option bundles..SpNote: disabling \f(CW\*(C`bundling_override\*(C'\fR also disables \f(CW\*(C`bundling\*(C'\fR.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -