Ruby 您能给我看一个快速工作的代码片段来解决这个命令行选项案例吗?

Ruby 您能给我看一个快速工作的代码片段来解决这个命令行选项案例吗?,ruby,command-line,Ruby,Command Line,在从命令行运行脚本时,我希望能够支持如下内容: script.rb -n 2 -t first.txt -t second.txt 我希望能够使用1个或多个t开关,但我不知道如何实现这一点。我不想这样做: script.rb -n 2 -tfirst.txt,second.txt 有什么想法吗?您可能想使用OptionParser 你喜欢这个工作吗 require 'optparse' files = [] OptionParser.new do |opts| opts.on("-t"

在从命令行运行脚本时,我希望能够支持如下内容:

script.rb -n 2 -t first.txt -t second.txt
我希望能够使用1个或多个
t
开关,但我不知道如何实现这一点。我不想这样做:

script.rb -n 2 -tfirst.txt,second.txt

有什么想法吗?

您可能想使用OptionParser

你喜欢这个工作吗

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
需要“optpass”
文件=[]
OptionParser.new do | opts|
opts.on(“-t”、“--text file TEXTFILE”、“要运行的文本文件”)do | text_file_name|

文件您可能需要使用OptionParser

你喜欢这个工作吗

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
需要“optpass”
文件=[]
OptionParser.new do | opts|
opts.on(“-t”、“--text file TEXTFILE”、“要运行的文本文件”)do | text_file_name|
文件夹