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:
- An enumeration specifying the “short” form of the command line option
- A structure describing both the short and the long forms of a command line option
- A switch statement that processes the command line options from
getopt_long()
- 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.