这是我的解析器:
options = { frames: 12, showcurrent: false}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-f", "--frames", "Only tickets of the last x time frames (default: 12)", Integer) { |v| options[:frames] = v }
opts.on("-c", "--show_current", "Show current (false (default) ot true)") { |v| options[:showcurrent] = v }
opts.on("-t", "--time", "Type of the report (day, week (default), month, quarter, year)", String) { |v| options[:time] = v }
opts.on("-w", "--year_week YEAR-WEEK", "wrYYWW (wr1707)", String) { |v| options[:yw] = v }
end
optparse.parse!
puts options在我使用ruby main.rb -t 'w' -w 'wr1707'运行代码之后,选项如下:
{:frames=>12, :showcurrent=>false, :time=>nil, :yw=>"wr1707"}出于某种原因,选项:时间没有设置,我不明白为什么。是不是转换有问题,不允许我使用-t作为参数?
发布于 2017-02-21 22:35:06
当需要参数时,您应该告诉OptionParser是哪个参数,以及它是可选的还是必须的,请参见documentation
"--switch=MANDATORY" or "--switch MANDATORY"
"--switch[=OPTIONAL]"
"--switch"因此,在您的示例中,对于time选项,描述应如下所示:
opts.on("-t", "--time TIME", "Type of the report (day, week (default), month, quarter, year)", String) { |v| options[:time] = v }或者像这样,如果你想做可选的:
opts.on("-t", "--time [TIME]", "Type of the report (day, week (default), month, quarter, year)", String) { |v| options[:time] = v }https://stackoverflow.com/questions/42364702
复制相似问题