⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getopt::long.3

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 3
📖 第 1 页 / 共 4 页
字号:
In the option specification, the option name is followed by an equalssign \f(CW\*(C`=\*(C'\fR and the letter \f(CW\*(C`s\*(C'\fR. The equals sign indicates that thisoption requires a value. The letter \f(CW\*(C`s\*(C'\fR indicates that this value isan arbitrary string. Other possible value types are \f(CW\*(C`i\*(C'\fR for integervalues, and \f(CW\*(C`f\*(C'\fR for floating point values. Using a colon \f(CW\*(C`:\*(C'\fR insteadof the equals sign indicates that the option value is optional. Inthis case, if no suitable value is supplied, string valued options getan empty string \f(CW\*(Aq\*(Aq\fR assigned, while numeric options are set to \f(CW0\fR..Sh "Options with multiple values".IX Subsection "Options with multiple values"Options sometimes take several values. For example, a program coulduse multiple directories to search for library files:.PP.Vb 1\&    \-\-library lib/stdlib \-\-library lib/extlib.Ve.PPTo accomplish this behaviour, simply specify an array reference as thedestination for the option:.PP.Vb 1\&    GetOptions ("library=s" => \e@libfiles);.Ve.PPAlternatively, you can specify that the option can have multiplevalues by adding a \*(L"@\*(R", and pass a scalar reference as thedestination:.PP.Vb 1\&    GetOptions ("library=s@" => \e$libfiles);.Ve.PPUsed with the example above, \f(CW@libfiles\fR (or \f(CW@$libfiles\fR) wouldcontain two strings upon completion: \f(CW"lib/srdlib"\fR and\&\f(CW"lib/extlib"\fR, in that order. It is also possible to specify thatonly integer or floating point numbers are acceptable values..PPOften it is useful to allow comma-separated lists of values as well asmultiple occurrences of the options. This is easy using Perl's \fIsplit()\fRand \fIjoin()\fR operators:.PP.Vb 2\&    GetOptions ("library=s" => \e@libfiles);\&    @libfiles = split(/,/,join(\*(Aq,\*(Aq,@libfiles));.Ve.PPOf course, it is important to choose the right separator string foreach purpose..PPWarning: What follows is an experimental feature..PPOptions can take multiple values at once, for example.PP.Vb 1\&    \-\-coordinates 52.2 16.4 \-\-rgbcolor 255 255 149.Ve.PPThis can be accomplished by adding a repeat specifier to the optionspecification. Repeat specifiers are very similar to the \f(CW\*(C`{...}\*(C'\fRrepeat specifiers that can be used with regular expression patterns.For example, the above command line would be handled as follows:.PP.Vb 1\&    GetOptions(\*(Aqcoordinates=f{2}\*(Aq => \e@coor, \*(Aqrgbcolor=i{3}\*(Aq => \e@color);.Ve.PPThe destination for the option must be an array or array reference..PPIt is also possible to specify the minimal and maximal number ofarguments an option takes. \f(CW\*(C`foo=s{2,4}\*(C'\fR indicates an option thattakes at least two and at most 4 arguments. \f(CW\*(C`foo=s{,}\*(C'\fR indicates oneor more values; \f(CW\*(C`foo:s{,}\*(C'\fR indicates zero or more option values..Sh "Options with hash values".IX Subsection "Options with hash values"If the option destination is a reference to a hash, the option willtake, as value, strings of the form \fIkey\fR\f(CW\*(C`=\*(C'\fR\fIvalue\fR. The value willbe stored with the specified key in the hash..PP.Vb 1\&    GetOptions ("define=s" => \e%defines);.Ve.PPAlternatively you can use:.PP.Vb 1\&    GetOptions ("define=s%" => \e$defines);.Ve.PPWhen used with command line options:.PP.Vb 1\&    \-\-define os=linux \-\-define vendor=redhat.Ve.PPthe hash \f(CW%defines\fR (or \f(CW%$defines\fR) will contain two keys, \f(CW"os"\fRwith value \f(CW\*(C`"linux\*(C'\fR and \f(CW"vendor"\fR with value \f(CW"redhat"\fR. It isalso possible to specify that only integer or floating point numbersare acceptable values. The keys are always taken to be strings..Sh "User-defined subroutines to handle options".IX Subsection "User-defined subroutines to handle options"Ultimate control over what should be done when (actually: each time)an option is encountered on the command line can be achieved bydesignating a reference to a subroutine (or an anonymous subroutine)as the option destination. When \fIGetOptions()\fR encounters the option, itwill call the subroutine with two or three arguments. The firstargument is the name of the option. For a scalar or array destination,the second argument is the value to be stored. For a hash destination,the second arguments is the key to the hash, and the third argumentthe value to be stored. It is up to the subroutine to store the value,or do whatever it thinks is appropriate..PPA trivial application of this mechanism is to implement options thatare related to each other. For example:.PP.Vb 3\&    my $verbose = \*(Aq\*(Aq;   # option variable with default value (false)\&    GetOptions (\*(Aqverbose\*(Aq => \e$verbose,\&                \*(Aqquiet\*(Aq   => sub { $verbose = 0 });.Ve.PPHere \f(CW\*(C`\-\-verbose\*(C'\fR and \f(CW\*(C`\-\-quiet\*(C'\fR control the same variable\&\f(CW$verbose\fR, but with opposite values..PPIf the subroutine needs to signal an error, it should call \fIdie()\fR withthe desired error message as its argument. \fIGetOptions()\fR will catch the\&\fIdie()\fR, issue the error message, and record that an error result mustbe returned upon completion..PPIf the text of the error message starts with an exclamation mark \f(CW\*(C`!\*(C'\fRit is interpreted specially by \fIGetOptions()\fR. There is currently onespecial command implemented: \f(CW\*(C`die("!FINISH")\*(C'\fR will cause \fIGetOptions()\fRto stop processing options, as if it encountered a double dash \f(CW\*(C`\-\-\*(C'\fR..Sh "Options with multiple names".IX Subsection "Options with multiple names"Often it is user friendly to supply alternate mnemonic names foroptions. For example \f(CW\*(C`\-\-height\*(C'\fR could be an alternate name for\&\f(CW\*(C`\-\-length\*(C'\fR. Alternate names can be included in the optionspecification, separated by vertical bar \f(CW\*(C`|\*(C'\fR characters. To implementthe above example:.PP.Vb 1\&    GetOptions (\*(Aqlength|height=f\*(Aq => \e$length);.Ve.PPThe first name is called the \fIprimary\fR name, the other names arecalled \fIaliases\fR. When using a hash to store options, the key willalways be the primary name..PPMultiple alternate names are possible..Sh "Case and abbreviations".IX Subsection "Case and abbreviations"Without additional configuration, \fIGetOptions()\fR will ignore the case ofoption names, and allow the options to be abbreviated to uniqueness..PP.Vb 1\&    GetOptions (\*(Aqlength|height=f\*(Aq => \e$length, "head" => \e$head);.Ve.PPThis call will allow \f(CW\*(C`\-\-l\*(C'\fR and \f(CW\*(C`\-\-L\*(C'\fR for the length option, butrequires a least \f(CW\*(C`\-\-hea\*(C'\fR and \f(CW\*(C`\-\-hei\*(C'\fR for the head and height options..Sh "Summary of Option Specifications".IX Subsection "Summary of Option Specifications"Each option specifier consists of two parts: the name specificationand the argument specification..PPThe name specification contains the name of the option, optionallyfollowed by a list of alternative names separated by vertical barcharacters..PP.Vb 2\&    length            option name is "length"\&    length|size|l     name is "length", aliases are "size" and "l".Ve.PPThe argument specification is optional. If omitted, the option isconsidered boolean, a value of 1 will be assigned when the option isused on the command line..PPThe argument specification can be.IP "!" 4The option does not take an argument and may be negated by prefixingit with \*(L"no\*(R" or \*(L"no\-\*(R". E.g. \f(CW"foo!"\fR will allow \f(CW\*(C`\-\-foo\*(C'\fR (a value of1 will be assigned) as well as \f(CW\*(C`\-\-nofoo\*(C'\fR and \f(CW\*(C`\-\-no\-foo\*(C'\fR (a value of0 will be assigned). If the option has aliases, this applies to thealiases as well..SpUsing negation on a single letter option when bundling is in effect ispointless and will result in a warning..IP "+" 4The option does not take an argument and will be incremented by 1every time it appears on the command line. E.g. \f(CW"more+"\fR, when usedwith \f(CW\*(C`\-\-more \-\-more \-\-more\*(C'\fR, will increment the value three times,resulting in a value of 3 (provided it was 0 or undefined at first)..SpThe \f(CW\*(C`+\*(C'\fR specifier is ignored if the option destination is not a scalar..IP "= \fItype\fR [ \fIdesttype\fR ] [ \fIrepeat\fR ]" 4.IX Item "= type [ desttype ] [ repeat ]"The option requires an argument of the given type. Supported typesare:.RS 4.IP "s" 4.IX Item "s"String. An arbitrary sequence of characters. It is valid for theargument to start with \f(CW\*(C`\-\*(C'\fR or \f(CW\*(C`\-\-\*(C'\fR..IP "i" 4.IX Item "i"Integer. An optional leading plus or minus sign, followed by asequence of digits..IP "o" 4.IX Item "o"Extended 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 (\f(CW\*(C`0x\*(C'\fR followed by '0' .. '9', 'a' .. 'f', caseinsensitive), or a binary string (\f(CW\*(C`0b\*(C'\fR followed by a series of '0'and '1')..IP "f" 4.IX Item "f"Real number. For example \f(CW3.14\fR, \f(CW\*(C`\-6.23E24\*(C'\fR and so on..RE.RS 4.SpThe \fIdesttype\fR can be \f(CW\*(C`@\*(C'\fR or \f(CW\*(C`%\*(C'\fR 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..SpThe \fIrepeat\fR specifies the number of values this option takes peroccurrence on the command line. It has the format \f(CW\*(C`{\*(C'\fR [ \fImin\fR ] [ \f(CW\*(C`,\*(C'\fR [ \fImax\fR ] ] \f(CW\*(C`}\*(C'\fR..Sp\&\fImin\fR denotes the minimal number of arguments. It defaults to 1 foroptions with \f(CW\*(C`=\*(C'\fR and to 0 for options with \f(CW\*(C`:\*(C'\fR, see below. Note that\&\fImin\fR overrules the \f(CW\*(C`=\*(C'\fR / \f(CW\*(C`:\*(C'\fR semantics..Sp\&\fImax\fR denotes the maximum number of arguments. It must be at least\&\fImin\fR. If \fImax\fR is omitted, \fIbut the comma is not\fR, there is noupper bound to the number of argument values taken..RE.IP ": \fItype\fR [ \fIdesttype\fR ]" 4.IX Item ": type [ desttype ]"Like \f(CW\*(C`=\*(C'\fR, 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..SpNote that if a string argument starts with \f(CW\*(C`\-\*(C'\fR or \f(CW\*(C`\-\-\*(C'\fR, it will beconsidered an option on itself..IP ": \fInumber\fR [ \fIdesttype\fR ]" 4.IX Item ": number [ desttype ]"Like \f(CW\*(C`:i\*(C'\fR, but if the value is omitted, the \fInumber\fR will be assigned..IP ": + [ \fIdesttype\fR ]" 4.IX Item ": + [ desttype ]"Like \f(CW\*(C`:i\*(C'\fR, but if the value is omitted, the current value for theoption will be incremented..SH "Advanced Possibilities".IX Header "Advanced Possibilities".Sh "Object oriented interface".IX Subsection "Object oriented interface"Getopt::Long can be used in an object oriented way as well:.PP.Vb 4\&    use Getopt::Long;\&    $p = new Getopt::Long::Parser;\&    $p\->configure(...configuration options...);\&    if ($p\->getoptions(...options descriptions...)) ....Ve.PPConfiguration options can be passed to the constructor:.PP.Vb 2\&    $p = new Getopt::Long::Parser\&             config => [...configuration options...];.Ve.Sh "Thread Safety".IX Subsection "Thread Safety"Getopt::Long is thread safe when using ithreads as of Perl 5.8.  It is\&\fInot\fR thread safe when using the older (experimental and nowobsolete) threads implementation that was added to Perl 5.005..Sh "Documentation and help texts".IX Subsection "Documentation and help texts"Getopt::Long encourages the use of Pod::Usage to produce helpmessages. For example:.PP.Vb 2\&    use Getopt::Long;\&    use Pod::Usage;\&\&    my $man = 0;\&    my $help = 0;\&\&    GetOptions(\*(Aqhelp|?\*(Aq => \e$help, man => \e$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.\&\&    =cut.Ve.PPSee Pod::Usage for details..Sh "Parsing options from an arbitrary array".IX Subsection "Parsing options from an arbitrary array"By default, GetOptions parses the options that are present in theglobal array \f(CW@ARGV\fR. A special entry \f(CW\*(C`GetOptionsFromArray\*(C'\fR can beused to parse options from an arbitrary array..PP

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -