In case you haven’t noticed, I love Boost 😛 so I’m going to introduce you to its Program Options library. It is a command line options parser; it can also read options from an INI file, and it works with STL containers. It supports showing a help message, setting default values for missing options, allows multiple instances of the same option, etc. For complete set of features I refer you to the documentation. This post will be a short introduction.

Let’s start with some code that defines what options our program will accept:

The first option invoked with --help will display a friendly description, like this:

Allowed options:
  –help                           produce help message
  -i [ –int ] arg (=42)           int value
  -f [ –float ] arg (=3.14100003) float value
  -s [ –string ] arg (=Vorbrodt)  string value
  -a [ –int_list ] arg            list of int values
  -b [ –string_list ] arg         list of string values

Options help message.

Next is an integer value option; the first string "int,i" means that you can specify it as either --int or -i on the command line. When specified its value will be pulled into variable v, and if not specified the default value will be 42. Next two options for float and string behave in the exact same way. The parser will throw an exception if you specify those options more than once.
Next are list options: they allow you to specify the same option multiple times and are returned through the vi and vs variables which are std::vector’s of int and string.

Here is the program invocation and the output it produces:

./bin/options -i 1 -f 3.141 -s “Martin” -a 10 -a 11 -a 12 -b “Vorbrodt’s” -b “Blog”
Int value was set to 1
Float value was set to 3.141
String value was set to “Martin”
List of ints value was set to 10
List of ints value was set to 11
List of ints value was set to 12
List of strings value was set to “Vorbrodt’s”
List of strings value was set to “Blog”

Program invocation and output.

Complete source code of the program below.

options.cpp:

Leave a Reply