OptiFlag: Great Command Line Parser for Ruby

I find myself writing a few simple Ruby scripts which need the flexibility of command line arguments. Having no more experience with the command line than ARGV[0], I thought the good old interweb could help me find a better way. Although I’m sure there are many command line parsing libraries and schemes out there for the Ruby community, I did not want to have to wade through loads of documentation and dozens of example scripts just to get a couple of flags in my program. Parsing is subtle, I know, and I don’t want to have to deal with all of the details on my own.

Luckily I came across OptiFlag, an extremely lightweight and unobtrusive command line parser written by (I guess) Daniel Eklund. All one needs to do is extend his OptiFlagSet module at the top of your script using a very simply DSL and voilá, your command line arguments are available within the script as ARGV.flags.my_option. By default, all flags are required, but that’s easily changed. Validation of the parameters is also possible using regular expressions.

The author does not claim that this parser will replace all others (I like modesty) but instead is meant to meet the needs of very simple use cases. In my short experience today, it does its job perfectly.

Leave a Reply