Trollop 1.8.1 is out. This is a minor bugfix release, but 1.8, released a few weeks ago but not really advertised, adds new functionality, so I’m describing that here.
The new functionality is subcommand support, as seen in things like git and
svn. This feature is actually trivial to use / implement: you give Trollop a
list of stopwords. When it sees one, it stops parsing. The end. That’s all you
need.
Here’s how you use it:
- Call
Trollop::optionswith your global option specs. Pass it the list of subcommands as the stopwords. It will parseARGVand stop on the subcommand. - Parse the next word in ARGV as the subcommand, however you wish.
ARGV.shiftis the traditional choice. - Call
Trollop::optionsagain with whatever command-specific options you want.
And that’s it. Simple eh?
It continually amazes me how hard other people make option parsing. I think it’s a holdover from their days of using C or Java. Take a look at synopsis for optparse — it’s a ridiculous amount of work for something simple. Or better yet, look at the synopsis for CmdParse. Having to make a class for each command is a clunky Java-ism. I’m sorry, but it’s true. Subclassing is the one option for specializing code in Java; in Ruby we can be far more sophisticated. Take a look at Ditz’s operator.rb for an example of a subcommand DSL.
… and 1.8.2 a day later. Whoops!
[Excessively-large quote elided. Bad commenter, no cookie! — ed.]
amen, tell it brother!