如何使用 Ruby 的 optparse 解析没有名称的参数
我需要
script.rb <mandatory filename> [options]
像 optparse 一样解析命令行。
当然,我可以编写一些自定义代码来处理文件名,然后将 ARGV
传递给 optparse,但也许有更简单的方法来做到这一点?
编辑:还有另一种破解此类命令行的方法,即将 ['--mandatory-filename'] + ARGV
传递给 optparse,然后处理 --mandatory-filename< /代码> 选项。
I need to parse a command line like
script.rb <mandatory filename> [options]
with optparse.
Sure I can write some custom code to handle the filename, then pass ARGV
to optparse, but maybe there's a simpler way to do it?
EDIT: there's another hacky way to parse such a command line, and that is pass ['--mandatory-filename'] + ARGV
to optparse, then handle the --mandatory-filename
option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先使用 optparse
parse!
,然后扫描 ARGV,如果 ARGV 为空则引发。像这样:强制文件名不会被 OptionParser 处理,并将在 ARGV 中留给您 - 如果它不存在,只需手动提出。
First
parse!
with optparse, then scan the ARGV and raise if ARGV is empty. Like so:The mandatory filename will not be processed by the
OptionParser
and will be left for you in ARGV - if it's not there, just raise manually.只是为了跟进 Julik 和 Shadowfirebird 所说的:使用
OptionParser
进行解析时,请注意parse!
和parse
具有不同的功能。前者将从传递的数组中删除它理解的每个参数,后者将保留它们。这会改变您确定所需参数是否存在的条件。Just to follow up on what Julik and Shadowfirebird said: When parsing with
OptionParser
be aware thatparse!
andparse
have different functionality. The former will remove every argument it understands out of the passed array where the latter will leave them be. This changes your conditions for determining if the required argument is present.虽然它并不适用于所有情况,但能够在单个命令行上处理多个文件通常是件好事,例如:
file1 是强制性的,但 file2 及其他文件是可选的。
我知道执行此操作的最佳方法遵循以下约定:
如果未提供文件,将显示一条帮助消息,其中包含使用横幅和选项描述。如果这些文件存在,它们将是 ARGV 中唯一剩下的东西。
Although it doesn't apply to every situation, it is often nice to be able to process multiple files on a single command line, such as:
file1 is mandatory, but file2 and beyond is optional.
The best way I know to do this follows this convention:
If files are not provided, a help message will be displayed with the usage banner and a description of options. If the files are present, they will be the only thing left in ARGV.
我不确定它是否是最近添加的,但是之前的答案都没有提到
optparse.parse
在删除解析的选项后将返回 ARGV 值。如果这样做:
您将获得一个包含给定文件(以及未知选项)的数组。这样您就不必关心选项是在文件之前还是之后。
I am not sure if it was added recently, but none of the previous answers mention that
optparse.parse
will return the ARGV value after removing the parsed options.If you do this:
You will get an array with the given file/s (along unknown options). This way you do not have to care if the options come before or after the file.
Optparse 只处理带有参数的参数,据我所知。处理文件名的“正确”方法是在 optparse 之外处理它。我为此发布了一些示例代码来回答 this< /a> 问题。
顺便说一句,这是一个相当不寻常的命令行。如果它只适合你,那很好,但其他人可能会觉得它相当违反直觉。更正常的是:
script.rb [options]
...Optparse only does arguments with parameters, AFAIK. The "correct" way to handle your filename is to deal with it outside of optparse. I posted some example code for this in answer to this question.
BTW, that's a rather unusual commandline. If it's just for you, fine, but others are likely to find it rather counter-intuitive. It would be more normal to have:
script.rb [options] <filename>
...