如何使用 Ruby 的 optparse 解析没有名称的参数

发布于 2024-08-25 04:52:39 字数 429 浏览 7 评论 0原文

我需要

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独闯女儿国 2024-09-01 04:52:39

首先使用 optparse parse!,然后扫描 ARGV,如果 ARGV 为空则引发。像这样:

op.parse!
filename = ARGV.pop
raise "Need to specify a file to process" unless filename

强制文件名不会被 OptionParser 处理,并将在 ARGV 中留给您 - 如果它不存在,只需手动提出。

First parse! with optparse, then scan the ARGV and raise if ARGV is empty. Like so:

op.parse!
filename = ARGV.pop
raise "Need to specify a file to process" unless filename

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.

擦肩而过的背影 2024-09-01 04:52:39

只是为了跟进 Julik 和 Shadowfirebird 所说的:使用 OptionParser 进行解析时,请注意 parse!parse 具有不同的功能。前者将从传递的数组中删除它理解的每个参数,后者将保留它们。这会改变您确定所需参数是否存在的条件。

Just to follow up on what Julik and Shadowfirebird said: When parsing with OptionParser be aware that parse! and parse 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.

十六岁半 2024-09-01 04:52:39

虽然它并不适用于所有情况,但能够在单个命令行上处理多个文件通常是件好事,例如:

script.rb [options] file1 file2 ...

file1 是强制性的,但 file2 及其他文件是可选的。

我知道执行此操作的最佳方法遵循以下约定:

options = {}
optparse = OptionParser.new do |opts|
  opts.banner = "Usage: script.rb [options] file1 file2 ..."

  opts.on('-a', '--an-option ARG', 'Set some option') do |arg|
    options[:a] = arg
  end

  ...
end
optparse.parse!

# Check required conditions
if ARGV.empty?
  puts optparse
  exit(-1)
end

如果未提供文件,将显示一条帮助消息,其中包含使用横幅和选项描述。如果这些文件存在,它们将是 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:

script.rb [options] file1 file2 ...

file1 is mandatory, but file2 and beyond is optional.

The best way I know to do this follows this convention:

options = {}
optparse = OptionParser.new do |opts|
  opts.banner = "Usage: script.rb [options] file1 file2 ..."

  opts.on('-a', '--an-option ARG', 'Set some option') do |arg|
    options[:a] = arg
  end

  ...
end
optparse.parse!

# Check required conditions
if ARGV.empty?
  puts optparse
  exit(-1)
end

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.

未蓝澄海的烟 2024-09-01 04:52:39

我不确定它是否是最近添加的,但是之前的答案都没有提到 optparse.parse 在删除解析的选项后将返回 ARGV 值。

如果这样做:

rest = optparse.parse!

您将获得一个包含给定文件(以及未知选项)的数组。这样您就不必关心选项是在文件之前还是之后。

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:

rest = optparse.parse!

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.

若言繁花未落 2024-09-01 04:52:39

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>...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文