I'm trying to do something that looks like this:
我试着做这样的事情:
opt_parser = OptionParser.new do |opt|
opt.banner = "Test"
opt.separator ""
opt.on("-t", "--test arg1 arg2", "Test") do |arg1, arg2|
puts arg1
puts arg2
end
end
The problem is that it returns the arg1
, but arg2
returns nil
. How to make this work?
问题是它返回arg1,而arg2返回nil。如何使它工作?
2 个解决方案
#1
5
The accepted way of specifying a list of values for a given option is by repeating that option (for example the -D
option as accepted by java
and C compilers), e.g.
指定给定选项的值列表的公认方法是重复该选项(例如java和C编译器接受的-D选项),例如。
my_script.rb --test=arg1 --test=arg2
In some cases, the nature of your arguments may be such that you can afford to use a separator without introducing ambiguity (for example the -classpath
option to java
or, more clearly, the -o
option to ps
), so if arg1
and arg2
can never normally contain a comma ,
then you could also accept e.g.
在某些情况下,你的论点的本质可能是这样的,您可以使用一个分离器不引入歧义(例如java或类路径的选择,更清楚,ps - o选项),如果__arg1,最长不能通常包含一个逗号,然后你也可以接受。
my_script.rb --test=arg1,arg2
The code that supports both conventions above would be something along the lines of:
支持上述两种公约的代码应该是:
require 'optparse'
...
test_vals = []
...
opt_parser = OptionParser.new do |opt|
...
opt.on("-t", "--test=arg1[,...]", "Test") do |arg|
test_vals += arg.split(',')
end
...
end
opt_parser.parse!
puts test_vals.join("\n")
Then:
然后:
$ my_script.rb --test=arg1 --test=arg2
arg1
arg2
$ my_script.rb --test=arg1,arg2
arg1
arg2
$ my_script.rb --test=arg1 --test=arg2,arg3
arg1
arg2
arg3
#2
3
If vladr's answer is acceptable, alternate way is to pass Array
as third argument to #on
:
如果vladr的答案是可以接受的,另一种方法是将数组作为第三个参数传递给#on:
require 'optparse'
test_vals = []
opt_parser = OptionParser.new do |opt|
opt.banner = "Test"
opt.separator ""
opt.on("-t", "--test arg1[,...]", Array, "Test") do |args|
test_vals += args
end
end
opt_parser.parse!
puts test_vals.join("\n")
#1
5
The accepted way of specifying a list of values for a given option is by repeating that option (for example the -D
option as accepted by java
and C compilers), e.g.
指定给定选项的值列表的公认方法是重复该选项(例如java和C编译器接受的-D选项),例如。
my_script.rb --test=arg1 --test=arg2
In some cases, the nature of your arguments may be such that you can afford to use a separator without introducing ambiguity (for example the -classpath
option to java
or, more clearly, the -o
option to ps
), so if arg1
and arg2
can never normally contain a comma ,
then you could also accept e.g.
在某些情况下,你的论点的本质可能是这样的,您可以使用一个分离器不引入歧义(例如java或类路径的选择,更清楚,ps - o选项),如果__arg1,最长不能通常包含一个逗号,然后你也可以接受。
my_script.rb --test=arg1,arg2
The code that supports both conventions above would be something along the lines of:
支持上述两种公约的代码应该是:
require 'optparse'
...
test_vals = []
...
opt_parser = OptionParser.new do |opt|
...
opt.on("-t", "--test=arg1[,...]", "Test") do |arg|
test_vals += arg.split(',')
end
...
end
opt_parser.parse!
puts test_vals.join("\n")
Then:
然后:
$ my_script.rb --test=arg1 --test=arg2
arg1
arg2
$ my_script.rb --test=arg1,arg2
arg1
arg2
$ my_script.rb --test=arg1 --test=arg2,arg3
arg1
arg2
arg3
#2
3
If vladr's answer is acceptable, alternate way is to pass Array
as third argument to #on
:
如果vladr的答案是可以接受的,另一种方法是将数组作为第三个参数传递给#on:
require 'optparse'
test_vals = []
opt_parser = OptionParser.new do |opt|
opt.banner = "Test"
opt.separator ""
opt.on("-t", "--test arg1[,...]", Array, "Test") do |args|
test_vals += args
end
end
opt_parser.parse!
puts test_vals.join("\n")