Ruby 我在OptionParser中有三个标志,但它只允许访问其中的两个
我正在用Ruby构建一个CLI gem,并使用OptionParser添加带有参数的标志。无论何时使用参数调用一个标志,都会调用函数get。 我有三面旗帜。我的问题是,当我运行Ruby 我在OptionParser中有三个标志,但它只允许访问其中的两个,ruby,optionparser,Ruby,Optionparser,我正在用Ruby构建一个CLI gem,并使用OptionParser添加带有参数的标志。无论何时使用参数调用一个标志,都会调用函数get。 我有三面旗帜。我的问题是,当我运行-h命令查看可用的选项(标志)时,它只显示其中三个(跳过中间的一个),即使我尝试使用该标志(帮助中未列出),它也会运行最后一个标志的代码。 以下是我的标志代码: def city_weather options = {} OptionParser.new do |opts| opts
-h
命令查看可用的选项(标志)时,它只显示其中三个(跳过中间的一个),即使我尝试使用该标志(帮助中未列出),它也会运行最后一个标志的代码。
以下是我的标志代码:
def city_weather
options = {}
OptionParser.new do |opts|
opts.banner = "Welcome to El Tiempo! \n Usage: cli [options]"
opts.on('-today', 'Get today\'s weather') do |today|
options[:today] = today
weather_today(ARGV.first)
end
opts.on('-av_max', 'Get this week\'s average maximum temperature') do |av_max|
options[:av_max] = av_max
week_average_max(ARGV.first)
end
opts.on('-av_min', 'Get this week\'s average minimum temperature') do |av_min|
options[:av_min] = av_min
week_average_min(ARGV.first)
end
end.parse!
ARGV.first
end
-av_max
标志不起作用。
当我运行-av_max
时,执行周平均值_min(ARGV.first)
get
当我运行-h
命令时,它会显示以下内容:
Welcome to El Tiempo!
Usage: cli [options]
-today Get today's weather
-av_min Get this week's average minimum temperature
我的问题是,OptionParser是否可以有三个标志?如果是这样的话,我怎样才能使这个中间平铺工作呢?您需要为长参数添加另一个破折号:
opts.on(“--av_min”,“获取本周的平均最低温度”)do|av_min|
选项[:av_min]=av_min
每周平均分钟数(初始值)
结束