Really Cheap Command-Line Option Parsing in Ruby

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

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.

Leave a Comment