The Accidental Rubyist

invalid byte sequence in UTF-8

Ruby command-line parsing (update)

with 5 comments

The issues with optiflag and choice: regarding determining unparsed arguments seems to have been posted in their respective forums a year or 2 back. But no response from authors. Are these 2 projects abandoned ?

I dipped into the source code of choice (parser.rb) to see if i could figure out how to extract the args, but after trying out variations on the command line, and print statements, it does not look like an easy task – not for me at least (a newbie). The author is a big-shot and busy (and very cool looking), so I guess he won’t be reading my mail for some time ( !).

The comments on this page led me to some excellent options such as:
Thor: by whycat’s (yehuda katz). It’s a great tool, maps commands on the CL to ruby methods. It is executed like rake: thor prog options. Something definitely to use in the near future.
clip: This one does state in its help that it allows the remainder args to be accessed. It seems to simplest of all.
– I once again looked at trollop. It does not do any validations, and I am not sure whether the remainder args are accessible or not. What i can see is that I would have to have a separate switch case for all the options.
– I once again looked at Commandline. It begins to look more appealing after the issues I have faced with the others.
– I gave a much closer look at cmdparse : looks great for commands and subcommands as in rake and webgen (its written by the webgen author who is very active and responds immediately). Could not figure out whether it will give me the remainder or not.

My next step will probably to give clip and trollop a try. Along the way, I discovered interesting things such as SproutCore (a ruby javascript framework), gambit, elif (a backward file reader), SimpleConsole, python’s cly, read up more on JSON …

As can be seen from my sig on git, I looked at the tutorials of several revision control systems such as bazaar, mercurial, git and subversion and finally decided to try out git after all. Have been using rcs until now.

I’m an egotistical bastard, and I name all my projects after myself. First Linux, now git.
— Linus Torvalds

# print file except for blank lines
$ cat | ruby -pe ‘next if $_ =~ /^\s*$/’



Written by totalrecall

September 8, 2008 at 12:29 pm

Posted in Uncategorized

5 Responses

Subscribe to comments with RSS.

  1. Is there a reason you haven’t tried optparse? I’m pretty sure it satisfies your criteria and it will return any remaining arguments.


    September 8, 2008 at 10:39 pm

  2. cmdparse will throw an error if it encounters a command line option that was not specified (cmdparse uses optparse from Ruby’s stdlib for option parsing). However, it will give you all non-option arguments specified on the command line, so:

    my_program -i -j test test1

    – will throw an error if, for example, the -j option is not defined (you can catch the error and handle it)
    – if all options are defined, you get test and test1 as arguments

    Thomas Leitner

    September 8, 2008 at 10:41 pm

  3. Thanks Thomas.
    I will definitely install it and check it out.


    September 8, 2008 at 10:49 pm

  4. @dxoigmn:
    >Is there a reason you haven’t tried optparse? I’m pretty sure it satisfies your criteria and it
    > will return any remaining arguments.

    Thanks @dxoigmn. Actually that is what I tried after getoptlong. It did whatever I wanted. It did not have mandatory options. However, looking back, after being attracted by the promises of other parsers that appear easier, I am thinking optparse is very good after all.

    For mandatory options I just had to add a check anyway. At that point i was thinking that the option code was about 50% of my program, but looking back I see that productivity is more important that just LOC (most of it was simple cut-paste).

    I am trying to remember: iirc, when optparse encounters an error in options, it gives out line number and stack trace rather than well-formatted error.


    September 8, 2008 at 11:14 pm

  5. For another, dare I say ‘option’, take a look at It is quite similar to clip in overall design, since it seems to want as did I, the smallest and fastest thing I could find without necessarily going to the extent of creating a DSL—which to me seems like overkill. Unlike clip Switches is based on OptionParser, which I am considering dropping and doing my own management of ARGV. Note that while I have code which extends Switches similarly to Thor insofar as making those attributes accessible to the calling class, this is unreleased at present. I’m also considering removing OpenStruct and instead using Hash with #methodize_keys so as both are available, giving output the same as Trollop (ie. hashes), but I’m not sure about that yet. I will also be incorporating the subcommand style too shortly. Lastly, I’ll be putting up some tests for Switches in the next day or so!


    February 18, 2011 at 10:05 am

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: