版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明。
https://windtear.net/archives/2004/07/22/000462.html
http://windtear.net/archives/2004/07/22/000462.html
perl 中处理参数的模组 Getopt::Long
(本文最近于2006-3-1 9:38更新)
不会用模组的话 只能自己处理参数
既然有模组 那么就建议拿来主义
http://search.cpan.org/~jv/Getopt-Long-2.35/lib/Getopt/Long.pm
use Getopt::Long;
my $data = "file.dat";
my $length = 24;
my $verbose;
$result = GetOptions ("length|l=i" => \$length, # numeric
"file=s" => \$data, # string
"verbose|v" => \$verbose); # flaguse Getopt::Long;
或者还有 "library=s" => \@libfiles
参数不区分大小写
--L --l 都行
The argument specification can be
- !
-
The option does not take an argument and may be negated by prefixing it with "no" or "no-". E.g. "foo!" will allow --foo (a value of 1 will be assigned) as well as --nofoo and --no-foo (a value of 0 will be assigned). If the option has aliases, this applies to the aliases as well.
Using negation on a single letter option when bundling is in effect is pointless and will result in a warning.
- +
-
The 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 --more --more --more , will increment the value three times, resulting in a value of 3 (provided it was 0 or undefined at first).
The + specifier is ignored if the option destination is not a scalar.
- = type [ desttype ] [ repeat ]
-
The option requires an argument of the given type. Supported types are:
- s
-
String. An arbitrary sequence of characters. It is valid for the argument to start with - or -- .
- i
-
Integer. An optional leading plus or minus sign, followed by a sequence of digits.
- o
-
Extended integer, Perl style. This can be either an optional leading plus or minus sign, followed by a sequence of digits, or an octal string (a zero, optionally followed by '0', '1', .. '7'), or a hexadecimal string (0x followed by '0' .. '9', 'a' .. 'f', case insensitive), or a binary string (0b followed by a series of '0' and '1').
- f
-
Real number. For example 3.14 , -6.23E24 and so on.
The desttype can be @ or % to specify that the option is list or a hash valued. This is only needed when the destination for the option value is not otherwise specified. It should be omitted when not needed.
The repeat specifies the number of values this option takes per occurrence on the command line. It has the format { [ min ] [ , [ max ] ] } .
min denotes the minimal number of arguments. It defaults to 1 for options with = and to 0 for options with : , see below. Note that min overrules the = / : semantics.
max denotes the maximum number of arguments. It must be at least min. If max is omitted, but the comma is not, there is no upper bound to the number of argument values taken.
- : type [ desttype ]
-
Like = , 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 - or -- , it will be considered an option on itself.
- : number [ desttype ]
-
Like :i , but if the value is omitted, the number will be assigned.
- : + [ desttype ]
-
Like :i , but if the value is omitted, the current value for the option will be incremented.
|
|