Perl Getopt :: Long,支持参数空间 [英] Perl Getopt::Long , supporting spaces for arguments
问题描述
我有一个perl脚本,它长时间使用GetOpts.像
I have a perl script, which uses GetOpts long.A command like
automate -action build,deploy -modules chat,email,login
很容易处理.
我要实现的是,允许用户在参数之间留空格.
What I want to achieve is, allow user to give spaces between arguments.
例如
automate -action build, deploy -modules chat, email, login
问题是,GetOpt::Long
内部使用@ARGV
来根据需要设置变量,并且空格更改了@ARGV
数组,这反过来只会将'build'作为操作,而仅将'聊天"作为脚本模块,而忽略了传递的其余参数.
The issue is , that GetOpt::Long
internally uses @ARGV
, to set the variables as needed, and a space changes the @ARGV
array, which in turn will put only 'build' as an action , and only 'chat' as a module for the script, ignoring the rest of the arguments passed.
是否有一种简单的方法可以像perl那样解析命令行?
Is there a simple way to parse a command line like the one above in perl?
我希望是这样,因为否则,在将@ARGV
数组传递给GetOpts之前,我将不得不使用一种非常棘手的方法.
I hope there is, because otherwise I will have to use a very hacky way of changing the @ARGV
array before it is passed to GetOpts.
还有没有其他健壮的库可以为我做到这一点?
Are there any other robust libraries out there which will do this for me?
---------------------------由裁缝制作的脚本----------------- ---------------
---------------------------Tailor made script--------------------------------
GetOptions("action=s{1,4}"=>\@myactions,
"modules=s{,}"=>\@mymodules);
foreach(@mymodules)
{
if($_ eq $mymodules[0])
{
$mymodules= $mymodules.$_;
next;
}
if($dashboards =~ m/,$/ || $_ =~ m/^,/)
{
$mymodules= $mymodules.$_;
}
else
{
$mymodules= $mymodules.",".$_;
}
}
推荐答案
查看此具有多个值的选项部分.它看起来与您要查找的内容相似.
Check out this Options with multiple values section in the Getopt::Long perldoc. It appears similar to what you're looking for.
示例:
GetOptions ("action=s{,}" => \@valuelist);
@values = split(/[\s,]+/,join(',' , @valuelist));
# @values will contain the list of values passed to the option.
# This can handle the scenarios:
# <command> -action build,deploy
# <command> -action build, deploy
# <command> -action build deploy
这篇关于Perl Getopt :: Long,支持参数空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!