如何解析命令行参数? [英] How can I parse command line arguments?
本文介绍了如何解析命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在perl脚本中解析参数列表,例如,我遇到这种情况:
I want to parse a list of arguments in a perl script, for example i have this situation :
script.pl -h 127.0.0.1 -u user -p pass arg1 arg2 arg3
我该如何解析数组中不是option的参数列表以及标量值中的option参数?
How can i do for parse the list of argument that aren't option in an array, and the option parameter in scalar value ?
谢谢.
推荐答案
好吧,如果它们是命令行中唯一未作为选项提供的内容,则它们仍应位于@ARGV
中.因此,只需使用@ARGV
.
Well, if they are the only things on the command line that aren't given as options, then they should still be in @ARGV
. So just use @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> ]
请参见 Getopt::Long
这篇关于如何解析命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文