Her*_*rms 7 ruby command-line-arguments
我正试图在我的ruby应用程序中获得简单的选项处理.看起来像OptionParser我想要的大部分,虽然我无法找到一种方法来优雅地处理意外的参数.
如果提供了任何意外的参数,我想将其视为-h传递参数(show usage and quit).我没有看到任何方法来处理这个问题.
如果OptionParser不能这样做,是否有另一个我可以用来轻松解析命令行参数的库?
Way*_*rad 10
这可能是一种光滑的方式,但我不知道.我这样做了:
opts = OptionParser.new
...
opts.on_tail("-h", "--help",
"Show this message") do
puts opts
exit
end
begin
opts.parse!(argv)
rescue OptionParser::InvalidOption => e
puts e
puts opts
exit(1)
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3205 次 |
| 最近记录: |