你能告诉我一个快速工作的代码片段来解决这个命令行选项吗?

Geo*_*Geo 1 ruby command-line

从命令行运行脚本时,我希望能够支持这样的事情:

script.rb -n 2 -t first.txt -t second.txt
Run Code Online (Sandbox Code Playgroud)

我希望能够使用一个或多个t开关,但我不知道如何实现这一目标.我不想这样做:

script.rb -n 2 -tfirst.txt,second.txt
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

pht*_*ier 7

您可能希望使用OptionParser http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html

会这样的吗?

require 'optparse'
files = []

OptionParser.new do |opts|
  opts.on("-t", "--text-file TEXTFILE","Text file to run against" ) do |text_file_name|
   files << text_file_name
  end
end.parse!

puts files.inspect
Run Code Online (Sandbox Code Playgroud)