Really Cheap Command-Line Option Parsing in Ruby

前端 未结 20 1083
野的像风
野的像风 2020-11-30 16:57

EDIT: Please, please, please read the two requirements listed at the bottom of this post before replying. People keep posting their new gems and li

20条回答
  •  粉色の甜心
    2020-11-30 17:08

    As the author of Trollop, I cannot BELIEVE the stuff that people think is reasonable in an option parser. Seriously. It boggles the mind.

    Why should I have to make a module that extends some other module to parse options? Why should I have to subclass anything? Why should I have to subscribe to some "framework" just to parse the command line?

    Here's the Trollop version of the above:

    opts = Trollop::options do
      opt :quiet, "Use minimal output", :short => 'q'
      opt :interactive, "Be interactive"
      opt :filename, "File to process", :type => String
    end
    

    And that's it. opts is now a hash with keys :quiet, :interactive, and :filename. You can do whatever you want with it. And you get a beautiful help page, formatted to fit your screen width, automatic short argument names, type checking... everything you need.

    It's one file, so you can drop it in your lib/ directory if you don't want a formal dependency. It has a minimal DSL that is easy to pick up.

    LOC per option people. It matters.

提交回复
热议问题