Command-line parsing: choosing a ruby library

I started with getoptlong, but lately decided to try something that would make life simpler. I need a parser that gives me options for:

  • mandatory flags

  • optional flags

  • switches

I used OptionParser for the ruby csplit program. The code of OptionParser is probably longer than the program code itself. It also does not let me define a mandatory flag. So I need to put in some code myself. Error messages for missing option parameters come with line numbers like program bugs. OptionParser is good, it will cast to a int, float, etc, but I am looking for something easier.
Looked at trollop — very little documentation, probably does not support the above.
Looked at Optiflag: this looks good. Besides allowing for optional and mandatory flags, it even allows for defining validations and lists for parameters.

Their page lists other alternatives, and another I really liked is Choice. Choice has very easy to use syntax. One does not need to remember what position some configuration needs to go. It may not look rubyish, but it is very easy to work on and remember. However, that page specifies that it may not be stable. It also gave me a link to a command-line library for taking input and validating it named Highline.

The ruby forum page (article dated 2005) also talked of CommandLine, looks good, but a needs looking into in greater detail to see what all is offered with ease, without needed to get into the low-level section.

cmdparse seems to be the most advanced – certainly not for me. Or maybe I’ll just come back to getoptlong. With getoptlong, i need to maintain my own help/usage string. Options are defined in one array. Actions on options are defined in a separate case switch. Three places to update for each change.


# grep for foo AND bar AND baz (in order)
$ cat | ruby -pe ‘next unless $_ =~ /foo.*bar.*baz/’

Explore posts in the same categories: ruby

2 Comments on “Command-line parsing: choosing a ruby library”

  1. Peter Cooper Says:

    Also, Trollop is another player in this area!

  2. totalrecall Says:

    Thanks Peter, I did look at trollop. The examples did not look like they gave me what i was looking for. However, it is simple, and much like getoptlong = which is familiar to all, and yet less repetition. Will give it a try.


Comment: