Perl:使用 GetOpt 时是否可以防止选项识别在双破折号(--)后停止?
我希望在 Perl 脚本中写下接收和选项,其值列表以双破折号 (--) 结尾。 示例:
% perl_script -letters a b c -- -words he she we --
运行此命令行的结果是,将创建两个数组: 字母= [abc ]; 单词= [他她我们];
使用 GetOption 不支持这一点,因为使用 double dash 后,选项识别停止。
I wish to write down in perl scripts that receive and option with values lists that ends with double dash (--).
example:
% perl_script -letters a b c -- -words he she we --
as result of running this command line, two arrays will be created:
letters = [a b c ];
words = [he she we];
Using GetOption does not support this, b.c after using double dash , the options recognitions stops.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您有什么具体原因使用如此令人困惑的分隔符吗?
--
对于大多数脚本用户来说都有已知的含义,但事实并非如此。如果您需要读取带有列表的选项,
Getopt::Long
有处理输入数组的方法,也许这样的东西可以帮助你;查看“具有多个值的选项”。该模块位于标准发行版中,因此您甚至无需安装任何东西。我将它用于任何需要多个(可能是两个)输入的脚本,并且肯定是否有任何输入是可选的。另请参阅此处以及更多此处。
这是一个简单的例子,如果您可以灵活地更改输入语法,这将获得您所请求的功能:
给出:
虽然我永远不会鼓励编写自己的解析器,但我无法理解为什么有人会选择您所拥有的形式,所以我将假设您无法控制这种格式并且需要解决它。如果是这种情况(否则,请考虑更标准的语法并使用上面的示例),这里有一个简单的解析器可以帮助您入门。
注意,不要自己编写的原因是其他的已经经过充分测试并且已经解决了边缘案例。您还知道如何处理
--
和-title
之间的内容吗?我假设,由于新的标题将结束前一个标题,因此您可能会在两者之间有一些东西,并将所有这些按顺序集中在“默认”键中。给出:
Do you have some specific reason to use such a confusing delimiter?
--
has a known meaning to most script users and this is not it.If you need to read in options with lists,
Getopt::Long
has methods to deal with input arrays, perhaps something like this could help you; look under "Options with multiple values". This module is in the standard distribution, so you don't even have to install anything. I use it for any script that needs more than one (maybe two) inputs and definitely if any inputs are optional.See also here and even more here.
Here is a quick example, if you have the flexibility to change your input syntax, this gets you the functionality you are requesting:
Gives:
While I would never encourage writing one's own parser, I could not understand why someone would choose the form you had, so I am going to operate under the assumption that you are not in control of this format and need to work around it. If this is the case (otherwise, please consider a more standard syntax and use the example above), here is a simple parser that should get you started.
N.B. the reason NOT to write your own is others are well tested and have fringe cases ironed out. Also do you know what you would do with something between a
--
and a-title
? I assumed that since a new -title would end the previous one, that you might have something between and have lumped all these in order in a 'default' key.Gives:
怎么样
?
How about
?
如果需要,您可以多次处理您的论点。查看 pass_through 选项。这就是我在 ack 中所做的,因为某些选项会影响其他选项,所以我必须首先处理 --type 选项,并且然后处理剩下的事情。
You can handle your arguments in multiple passes if you want. Look at the pass_through option. This is what I do in ack because some options affect other options, so I have to handle the --type options first, and then handle out the rest.
禁用 dash dash 后门是不好的做法。
It's bad practice to disable the dash dash backdoor.