The Accidental Rubyist

invalid byte sequence in UTF-8

Command-line parsing: choosing a ruby library

with 7 comments

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/’

Advertisements

Written by totalrecall

September 5, 2008 at 2:08 pm

Posted in ruby

7 Responses

Subscribe to comments with RSS.

  1. Also, Trollop is another player in this area!

    Peter Cooper

    September 6, 2008 at 2:54 am

  2. 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.

    totalrecall

    September 6, 2008 at 8:55 am

  3. […] The busiest day of the year was November 30th with 85 views. The most popular post that day was Command-line parsing: choosing a ruby library. […]

  4. Hi
    I was looking for one that would allow simple addition of other switch operators. optiflag allows redefinition but has to give one up.

    eg.
    –verbose +setup 1g2blah -someconfig !this
    We have ++,–,-,! operators above.

    Also a number of these seem to need to process ARGV directly and I would prefer to pass in like OptionParser.

    Any thoughts?
    nr

    nr

    January 26, 2011 at 9:02 pm

  5. I tried many but finally felt i would like to just use the stock OptionParser. Other libs get abandoned, deprecated, or do not work for newer versions of ruby. Wherever possible i now use what comes with ruby.

    I am not clear on your requirement. You can use booleans in OptionParser. e.g. –verbose or –noverbose.

    rkumar

    January 26, 2011 at 9:08 pm

  6. I notice the blog filtered my post’s operator list.
    Hopefully, better this quotes.

    The requirement is to allow addition of switch operators in a simple way. So we are not limited to the unix/windows typical switch operators such as ‘–‘,’-‘ or ‘/’.

    With a ruby pre-processor for applications which don’t conform to the standard args above we need to be able to validate those too.

    ‘eg +this’

    I agree OptionParse would be nice but it doesn’t seem to do this job.

    Thanks
    nr

    nr

    January 26, 2011 at 9:27 pm

  7. I am reading your comments in mail, so the operators are coming through.
    Usually it is best to stick to the conventions users are familiar with.

    If you wish to use your own switch operators you could just parse the ARGV array in a loop. I often do that in unix shell scripts when i want double minuses, long and short options.

    rkumar

    January 26, 2011 at 9:33 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: