没有分隔符的OptParse参数列表

3
我正在构建一个简单的程序,它将在标准输出上读取和写入文件。我希望以这种方式启动我的程序:ruby main.rb -f file1 file2 file3 ... 但是使用optParse时,我无法使其正常工作,我必须包含分隔符..我需要optParse,因为我处理多个选项(如verbose,help...)。所以如果我这样做:ruby main.rb -f file1,file2 ... 它可以正常工作。我该如何实现这一点?谢谢!

1
你可以在这里看到:链接 数组需要逗号分隔的值。他们确实说自定义强制转换可能会在未来的Ruby版本中出现。 - Anthony
通常,命令行界面(CLI)会将文件列表作为它们的最后一个参数传入,就像这个回答中所描述的那样:https://dev59.com/rnE95IYBdhLWcg3wHqMV。虽然不完全符合你的要求,但这种方法对你的应用程序可能有效吗? - Jordan Running
2个回答

3

如果您没有需要传递的选项参数,或者所有其他参数都是可选的,您可以通过ARGV方式以老式的方式传递文件。默认情况下,命令行选项由空格而不是逗号分隔。

如果您绝对需要支持-f选项,您可以额外添加对ARGV的支持。

require "optparse"

options = {
  :files => []
}
opt_parse = OptionParser.new do |opts|
  opts.banner = "Usage: main.rb file(s) ..."

  opts.on("-f", "--files file1,file2,...", Array, "File(s)") do |f|
    options[:files] += f
  end

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end

end
opt_parse.parse!

options[:files] += ARGV

if options[:files].length == 0
  abort(opt_parse.help)
end

puts options[:files]

使用这种方法,您甚至可以混合两种样式:

$ main.rb -f -f file1,file2 file3 -f file4 file5
file1
file2
file4
file3
file5

(注意,file5 实际上是通过 ARGV 传递的,但看起来你好像是使用空格作为分隔符。)

0

有几种方法可以使用optparse接受给定参数的多个值。一些方法允许使用空格分隔的列表,但每种方法都有其优缺点。好消息是这些方法可以互换使用,因此您始终可以实现多种方法,并让最终用户使用他们喜欢的方法。

假设有一个名为process_files.rb的脚本,如下所示:

require 'optparse'

args = { files: [] }

OptionParser.new do |opts|
  # Option 1: use an array-type optparse argument
  opts.on('--files FILE1,FILE2,...', Array, 'A list of comma-separated files.') do |files|
    args[:files].concat(files)
  end

  # Option 2: use a single-value argument; expect it to be repeated multiple times
  opts.on('-f', '--file FILE', 'A single file. Repeat for multiple files.') do |file|
    args[:files] << file
  end

  # Option 3: take a string and split it using whatever delimiter you want
  opts.on('--colon-files FILE1:FILE2:...', 'A colon-delimited file list.') do |str|
    args[:files].concat(str.split(':'))
  end

  # Option 4: as above, but quoting the file list on the command line allows space delimiting
  opts.on('--spaced-files "FILE1 FILE2 ..."', 'A space-delimited file list.') do |str|
    args[:files].concat(str.split(/\s+/))
  end
end.parse!

# Option 5: Assume all remaining, unparsed arguments are whitespace-delimited files
args[:files].concat(ARGV)

puts args[:files].inspect

以下是将文件列表传递给它的各种方法:

$ ruby process_files.rb --files foo,bar,baz
["foo", "bar", "baz"]

$ ruby process_files.rb -f foo -f bar -f baz
["foo", "bar", "baz"]

$ ruby process_files.rb --colon-files foo:bar:baz
["foo", "bar", "baz"]

$ ruby process_files.rb --spaced-files 'foo bar baz'
["foo", "bar", "baz"]

$ ruby process_files.rb foo bar baz
["foo", "bar", "baz"]

$ ruby process_files.rb --files apple,banana -f cherry --colon-files date:elderberry \
    --spaced-files 'fig grape' honeydew
["apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew"]

参考:https://rubyreferences.github.io/rubyref/stdlib/cli/optparse.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接