The Boston Diaries

The ongoing saga of a programmer who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Monday, March 04, 2013

Ideas in parsing the command line

For any non-trivial script, even for personal consumption, it's necessary to supply usage text. The novelty of Lapp is that it starts from that point and defines a loose format for usage strings which can specify the names and types of the parameters.

An example will make this clearer:

	-- scale.lua
	  require 'lapp'
	  local args = lapp [[
	  Does some calculations
	    -o,--offset (default 0.0)  Offset to add to scaled number
	    -s,--scale  (number)  Scaling factor
	     <number> (number )  Number to be scaled

	  print(args.offset + args.scale * args.number)

lua-users wiki: Lapp Framework

The thought of parsing the usage text for parsing the command line never occured to me, and I think it's brilliant.

Now, when I want to modify the command line of a program I wrote (and this is mostly in C, by the way), there are four locations I have to edit:

  1. An enumeration specifying the “short” form of the command line option
  2. A structure describing both the short and the long forms of a command line option
  3. A switch statement that processes the command line options from getopt_long()
  4. The text printed out describing the command line options

This method though, there's only one area I would have to edit.

Now granted, this is only for Lua, but I can't see why something similar for other languages can't be done.

Obligatory Picture

[“I am NOT a number, I am … a Q-CODE!”]

Obligatory Contact Info

Obligatory Feeds

Obligatory Links

Obligatory Miscellaneous

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site:, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2024 by Sean Conner. All Rights Reserved.