Command-line option parsing experiences with clip
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”, $., $_)’