Ruby OptionParser empty switch “-” behavior

前端 未结 2 892
自闭症患者
自闭症患者 2021-01-07 14:41

EDITED:

I\'ve wrote code that uses OptionParser to handle command line input gracefully. I am facing two major hits.

  1. Passing an empty switches \'-\' do
相关标签:
2条回答
  • 2021-01-07 15:19

    I think Thor(https://github.com/wycats/thor) can resolve your problem more efficiently.

    0 讨论(0)
  • 2021-01-07 15:29

    If you are using OptionParser, then yes, you need to explicitly disallow the empty switch and manually check the required parameters.

    However, if you used another tool for option parsing, such as defunkt's gem choice, you could mark options as required and the invalid options (such as the empty switch) would cause the help to be printed and application to exit. I understand that in some cases it makes more sense to use OptionParser, but personally I prefer to use the more convenient tools out there.

    Even though making options required is pretty easy one way or the other, I would recommend that you think your API decision through. How many command line utilities do you know, that have required options? There's a reason why command line is usually separated into options and arguments, with the former being usually optional and the latter usually required. I would stick to that established convention.

    0 讨论(0)
提交回复
热议问题