您的位置:首页 > 运维架构

perl模块 Getopt::Long解析参数

2016-11-28 16:05 501 查看
Getopt::Long模块是用于解析命令行参数的perl模块

选项解析

其他用法
使用options调用subroutines

多命名选项

捆绑选项

- 和

GetoptLongConfigure

## option variables with default value
my $verbose = "";
my $nda = "";
my $more = 0;
my $value = "";
my @libs = ();
my %defines = ();

## parse options from @ARGV
GetOptions(
'verbose' => \$verbose,
'nda!' => \$nda,
'more+' => \$more,
'tag=s' => \$tag,
'value:s' => \$value,
'libs=i' => \@libs, ## 'libs=i@' => \$libs,
'define=s' => \%defines, ## 'define=s%' => \$defines,
) or die $!;


选项解析

verbose,只有单独的一个选项,不接受任何参数。在命令行用
--verbose
来触发,触发后
$verbose
被设置为1,次用法常作为一个开关选项使用。

nda!,该选项在
nda
后加一个
!
,表示可以接受
--nda
--nonda
两个选项,对应的将
$nda
设置为1和0,如果触发该选项,
$nda
值为
""
(即空值),可以用perl内置函数length检测
$nda


more+
该选项的
+
表示在参数列表每出现一次
--more
,对应的变量
$more
自加1。

tag=s
该选项必须接受一个
string
参数,
=
表明必须要接受一个参数,
s
表明参数为任意字符串,也可用
i
f
指明参数为整数或浮点数。

value:s
tag=s
的唯一区别是
:
,表明不是必须接受一个参数,可以
--value
形式调用,
s
默认赋值为
""
i
/
f
默认赋值为
0


libs=i
指定一个数组引用
@libs
,表明可以多次使用
"--libs <lib>"
获取多个值并存到数组中,也可以使用另一种形式
'libs=i@' => \$libs
,如果要使用
"--libs <lib1> <lib2> <lib3>"
这种参数调用形式,在GetOptions()中使用
'libs=i{3}' => \@libs
指定每次传入3个值。可以试试
'libs=i{1,}' => \@libs


define=s
指定一个hash引用
%defines
,用
--define os=linux
传入一个hash参数。其他特点同上一个段落。

其他用法

使用options调用subroutines

my $verbose = "";

GetOptions(
'set' => \&handle,
) or die $!;

sub handle {
$verbose = 100;
print "options \'--set\' set verbose $verbose\n";
}


多命名选项

GetOptions('length|height' => \$length);


捆绑选项

ex:
ls -al


## 用法1
use Getopt::Long;
Getopt::Long::Configure("bundling"); ## **配置选项**

my $long = "";
my $all = "";
my $al = "";
GetOptions(
'long|l' => \$long, # 此处必须显式的指定'l'为'long'的aliase
'all|a' => \$all, # 此处必须显式的指定'a'为'all'的aliase
'al' => \$al,
) or die $!;

## 现在命令行可使用 '-al'选项,同时设置$long/$all为1.**single dash**
## 使用'--al'选项设置$al为1.**double dash**


## 用法2

use Getopt::Long;
Getopt::Long::Configure("bundling_override"); ## **配置选项**

my $long = "";
my $all = "";
my $al = "";
GetOptions(
'long|l' => \$long, # 此处必须显式的指定'l'为'long'的aliase
'all|a' => \$all, # 此处必须显式的指定'a'为'all'的aliase
'al' => \$al,
) or die $!;

## 现在不可以使用 '-al'选项设置$long/$all,'-al'和'--al'的作用是一样;
## 不过仍然能使用bundling,因为有长选项'--al'存在,所以捆绑选项'-al'被override;
## 如果没有长选项'--al','-al'仍然与方法1中的作用一样。

## 在用法一和用法二中,如果'long|l=i' => \$long,'all|a=i' => \$all,
## 即$long/$all都需要一个整数作为参数,那么可使用'-l11a22'实现'length=11''all=22'的效果


#用法3
use Getopt::Long;
Getopt::Long::Configure("bundling_values");

...

GetOptions(
...
) or die $!;

## 只有'-l11'或者'-a22'可以使用,'-l11a22'和'-al'等方式都不能用


## 用法4
use Getopt::Long;
Getopt::Long::Configure("bundling", "ignorecase_always");

...

## 当使用Configure的'bundling'配置时,短名称如'-a/-l'都是大小写敏感,
## 而长名称对大小写不敏感,所以使用"ignorecase_alwasy"选项设置为全部大小写不敏感。


‘-’ 和 ‘<>’

通常在命令行不识别单个的single-dash‘-’,可以使用如下方法捕获single-dash为参数选项。

GetOptions(
'' => \$single,
) or die $!;


使用GetOptions解析命令行时,当遇到无法识别的字符或者本来不是命令行选项的选项,可以用
<>
捕获。

## '<>'只能指向函数引用,可以通过函数做很多有意义的事
GetOptions(
'<>' => \&handle,
) or die ;

sub handle
{
my $catch = shift;
print "catch: $catch\n";
}


Getopt::Long::Configure()

指定Getoptions的各项特性

使用方法

## 方式1
use Getopt::Long qw(:config bundling no_ignore_case); ## 参数列表


## 方式2
use Getopt::Long;
Getopt::Long::Configure("bundling", "no_ignore_case"); ## 参数列表


default

bundling(default: disable)

可使用捆绑选项

bundling_override(default: disable)

用长选项重写
bundling
中出现的短选项。

ignore_case(default: enable)

默认大小写不敏感,对
bundling
状态的
-<single-character>
选项无效;

no_ignore_case
该选项禁用,同时也会禁用
ignore_case_always


ignore_case_always(default: disable)

针对bundling状态的大小写忽略

pass_through (default: disable)

对于
GetOptions()
中无法识别或者不需要识别或者无效的选项,都不会返回错误。

可以用于多级程序调用时的参数传递。因为
GetOptions()
会将识别的选项从
@ARGV
中取出,所以剩余的内容就可以继续传递到下一级程序。

debug(default: disable)

查看
Getopt::Long
模块处理参数时的debug信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息