我是Perl新手,目前我将命令行参数传递给perl脚本并通过ARGS [0]取回。perl中带关键字的命令行参数
perl <perlscript.pl> windows IE.
我想给关键字提到上面提到的值。
perl <perlscript.pl> -os windows -browser IE -instance 2.
可能有时候实例可能存在,也可能不存在。我如何去处理这个在我的Perl脚本。
我是Perl新手,目前我将命令行参数传递给perl脚本并通过ARGS [0]取回。perl中带关键字的命令行参数
perl <perlscript.pl> windows IE.
我想给关键字提到上面提到的值。
perl <perlscript.pl> -os windows -browser IE -instance 2.
可能有时候实例可能存在,也可能不存在。我如何去处理这个在我的Perl脚本。
使用Getopt::Long和存储您的选项哈希:
use warnings;
use strict;
use Getopt::Long qw(GetOptions);
my %opt;
GetOptions(\%opt, qw(
os=s
browser=s
instance=i
)) or die;
有几个模块用于处理命令行参数:Getopt::Declare
和Getopt::Long
可能是最流行的。在我的工作中,我们主要使用Getopt :: Declare显示和示例,因为@toolic覆盖Getopt :: Long。现在
my $ARGS = Getopt::Declare->new(
join("\n",
"[strict]",
"-os <string> The operating system [required]",
"-browser <string> The web browser [required]",
"-instance <int> The instance"
)
) or die;
您可以通过$ARGS
哈希访问任何parmeters的。即$ARGS->{-os}
[strict]
解析命令行严格地并报告任何错误。
[required]
选项声明后意味着该字段必须在那里,请注意我将它从实例中删除。