私はRubyでCLI gemを構築しており、OptionParserを使用して引数付きのフラグを追加しています。1 つのフラグが引数で呼び出されるたびに、関数が呼び出されます。私は3つの旗を持っています。私の問題は、-h
コマンドを実行して使用可能なオプション(フラグ)を確認すると、それらのうち3つしか表示されず(中央のものをスキップ)、そのフラグを使用しようとしても(ヘルプにリストされていない)最後のフラグのコードを実行します。フラグのコードは次のとおりです。
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
動かないフラグです。getを実行すると-av_max
実行されます。week_average_min(ARGV.first)
-h
ここでコマンドを実行すると、次のように表示されます。
Welcome to El Tiempo!
Usage: cli [options]
-today Get today's weather
-av_min Get this week's average minimum temperature
私の質問は、OptionParser で 3 つのフラグを持つことは可能ですか? もしそうなら、どうすればこのミドルフラットを機能させることができますか?