我想解析 perl 脚本中的参数列表,例如我有这种情况:
script.pl -h 127.0.0.1 -u user -p pass arg1 arg2 arg3
我该如何解析数组中不是选项的参数列表以及标量值中的选项参数?
Thanks.
好吧,如果它们是命令行上唯一没有作为选项给出的东西,那么它们仍然应该在@ARGV
。所以只需使用@ARGV
.
use Getopt::Long;
# save arguments following -h or --host in the scalar $host
# the '=s' means that an argument follows the option
# they can follow by a space or '=' ( --host=127.0.0.1 )
GetOptions( 'host=s' => \my $host
, 'user=s' => \my $user # same for --user or -u
, 'pass=s' => \my $pass # same for --pass or -p
);
# @ARGV: [ qw<arg1 arg2 arg3> ]
see Getopt::Long http://search.cpan.org/perldoc?Getopt%3a%3aLong
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)