Commandoptions

Parsing command line options is usually a tedious and rather boring task. Because it is an interaction with a user, it actually also requires a lot of work to get right. Often one ends up with either a rather dumb parser, or an unsafe one that perhaps assumes a little bit too much about the sanity of the user, or both.

This library is intended to avoid these problems. You supply the least possible information that is needed to construct a proper command-line interface together with the variables you want the user to be able to influence, and the library then takes care of everything else, from gathering and parsing options over error detection to defining and outputting --help and --usage messages.

Latest version is 1.1.1. Old and new source code is here. You can also have a look at the README. The library is licensed under the LGPL.

The constructed command line interface resembles that of popt (which the GNOME project uses) which in turn follows the GNU way. I actually started this project because I needed a small independent library for parsing command arguments, and popt turned out to be very C++ unfriendly (using std::strings is, for instance, almost impossible). A sample session with the test program looks like this:

  ole: ~/src/commandoptions$ ./test
  Error: not enough arguments specified, missing '<victim>'
  ole: ~/src/commandoptions$ ./test --usage
  Usage: test [-b?] [-h NUMBER] [-s SOUND] [--help-cry SOUND] [--usage] <victim>
  ole: ~/src/commandoptions$ ./test --help
  Usage: test [OPTIONS...] <victim>

  Options:
    -b, --beep            Turn on beeping
    -h, --hits NUMBER     How many times to hit victim
    -s SOUND              How hitting victim sounds
    --help-cry SOUND      Victim response

  Help options:
    -h, --help            Show this help message
    --usage               Display brief usage message
  ole: ~/src/commandoptions$ ./test --bep 'Santa Claus'
  Error: unrecognized option '--bep'
  ole: ~/src/commandoptions$ ./test --beep 'Santa Claus'
  Now hitting Santa Claus 3 times:
  *boink* *ouch*
  *boink* *ouch*
  *boink* *ouch*
  *beep*
  ole: ~/src/commandoptions$ ./test -h 2 --help-cry '*aaarrrgggghh*' 'Santa Claus'
  Now hitting Santa Claus 2 times:
  *boink* *aaarrrgggghh*
  *boink* *aaarrrgggghh*
  ole: ~/src/commandoptions$

Back to home.