The Accidental Rubyist

invalid byte sequence in UTF-8

Command-line option parsing experiences with clip

with 2 comments

I went through the one examples provided with clip . Looked very promising and simple. Until I applied it to my timeconverter program. Quick observations:

1. in the example “host” should be “server” (typo).
2. If using ruby -w, there are too many uninitialized variable warnings
3. Can’t reuse “-h”, yet on typing “-h” on prompt gives an error
4. multiple params to an option must not have any spaces. That allows clip to know the remainder (unparsed args) which choice has a problem giving me since it allows spaces between multi options. Fine with me.
5. No fancy printing of options: common and specific etc with separators, which is fine with me.
6. Allows for optional and required options, and flags which is good.

My timeconverter was using choice , a fine commandline parser, with one failing that I cannot get the unused arguments which I needed. I converted the choice portion to clip and began running the program. My program essentially converts time from one time zone to another, taking into account daylight saving. However, it can optionally print country codes, timezones for a country etc to help in using the main function. These are like independent commands.

The clip concept of “optional” is only for configuration values which will be used in the program such as port, host etc. All optional options require a default value to be specified, so i have no way of knowing if it was triggered or not. If the user enters the optional flag without specifying a value there is no way of knowing that. In such cases, I can only ask the user to type –help, since I don’t know what went wrong.

When i first gave the prog name on the prompt with no params, I expected the help screen. But execution went into the optional paths!

Flags give eval warnings if not used. You cannot give a default for them.

Options and their parameters require a space between, a “=” cannot be used as “–port=8080”. This makes the clip program source simple and short, but took me some time to debug. It just went into the not valid block leaving me wondering!

I will summarize saying that clip is great if your program has one action, and many config values that can default (which is probably the case most of the time). If your program has multiple optional execution paths, then this may not be the correct tool for you.

# number each line of a file (left justified).
$ cat | ruby -ne ‘printf(“%-6s%s”, $., $_)’



Written by totalrecall

September 8, 2008 at 10:38 pm

Posted in ruby

2 Responses

Subscribe to comments with RSS.

  1. Thanks for such a detailed analysis of Clip. I’m using it currently but for a trivial option list. What would you recommend for something more sophisticated than what Clip can handle?

    Ashley Moran

    September 25, 2008 at 9:23 pm

  2. I’ve detailed my experiences with other libraries too in previous posts.
    It really depends on what your need is. But then do you want to keep changing the parser every time your need changes.
    Optiflag looks good. Trollop looks easy. Cmdparse is great, esp for command like programs such as rake. The optionparser that comes with ruby is also fine.

    In my opinion, i would largely like to get comfortable with one command line parser. And that would be the *optionparser* that comes with ruby. After all, its only command line options.

    I have posted bugs, or written to some of the developers of the smaller libraries and got NO response. Like Choice which i liked alot. However, i will say that the author of Cmdparse is a very active person, and responds asap, and takes in requests etc. He develops a great website generator named Webgen.


    September 25, 2008 at 10:06 pm

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: