Command-line parsing
The hest library parses a variety of command-line options with
a small set of functions. The primary utility of
hest is for situations where the command line has to
communicate non-binary values for numerous options. This is often the
situation in a rendering or visualization program which has many
parameters or "knobs" to adjust, which may or may not have default
values, and each of which may be in fact an array of values, instead
of just a single number or string. These functions are not as handy
for situations where the majority of options are binary, as is the
case for many unix commands. So, idioms like "-xzv" are not
supported, however binary options are supported as stand-alone
command-line flags.
Useful things about hest:
- Can parse an arbitrary number of parameters for one option,
allowing things like "-i *.txt".
- Can parse both flagged and unflagged options. Flags can have
both a short form (such as "-i") and a long form (such as
"--input"). Unflagged options are interpreted according to
their order on the command line, allowing things like "mv *.txt
~/pub".
- Response files: plain text files containing options which you
don't want to have to type on the command line. Response file names
can appear anywhere on the command line. Their contents are
effectively inserted in the command line where the response file name
appears. Response files can contain comments.
- Automatic generation of helpful usage information, given as one
long line showing the various options, such as:
Usage: cmd [@file...] -res <sx sy> [-v [<level>]] [-out <file>] <input...>
- Automatic generation of a "glossary" of options, listing their
flags and their meanings, such as:
@file... = response file(s) containing command-line arguments
-res <sx sy> = image resolution (2 ints)
-v [<level>] = verbosity level (optional int); default 0
-out <file> = PPM image output (string); default output.ppm
<input...> = input image file(s) (1 or more strings)
- Simple specification of the option information, either statically
(an initialization of an array of hestOpt structs) or
dynamically (a sequence of calls to hestOptAdd).
- Can parse anything, given a set of callbacks to do so, but
this is very ugly and currently undocumented.
There other tools out there to help with command-line parsing. In
fact, the argtable library
provided the inspiration for hest. There is a good chance that
your command-line parsing needs are better served by argtable.
It doesn't support multiple parameters in the options, but its error
message generation is better, its usage is simpler, and it supports
different formats of flag (instead of just one or two hyphens as
prefix, and whitespace for delimiting). Clig (Command Line
Interpreter Generator) supports multiple parameters, but requires Tcl
to generate the C code. The getopt function is popular but
doesn't really do that much. The popt library is like
getopt on steroids, and looks very useful. It is the
command-line parsing brains of GNOME, which also has a page about
popt. It doesn't seem to do multiple parameter options, or
unflagged options.