12
我在使用OptionParser
和Ruby。没有参数的OptionParse显示横幅
其他语言如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
)? - 如果没有给出所需的参数或者无效,那么怎么办?假设
length
是一个REQUIRED参数,用户不会通过它或传递错误,如-l FOO
?
补充一点:'ARGV.push( ' - H'),如果ARGV .empty?'解析之前 –
@МалъСкрылевъ,是的,谢谢! – Israel
我的回答有帮助吗? =) –