OptionParser
Rubyで使用しています。
C、Python などの他の言語には、同様のコマンドライン パラメーター パーサーがあり、パラメーターが指定されていない場合やパラメーターが間違っている場合にヘルプ メッセージを表示する方法を提供することがよくあります。
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: calc.rb [options]"
opts.on("-l", "--length L", Integer, "Length") { |l| options[:length] = l }
opts.on("-w", "--width W", Integer, "Width") { |w| options[:width] = w }
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
質問:
help
パラメータが渡されなかった場合にデフォルトでメッセージを表示するように設定する方法はありますか (ruby calc.rb
)?- 必要なパラメーターが指定されていないか無効な場合はどうなりますか? がREQUIRED
length
パラメータであり、ユーザーがそれを渡さないか、次のような間違ったものを渡したとし-l FOO
ます。