vSMC
vSMC: Scalable Monte Carlo
|
Basic example
And if on the command line,
This program will produce the output
In the source, options are added using ProgramOptionMap::add
. The first argument is the option name, say option_name
. The second is a description of the option. And the third is an pointer points to the destination of the value. On the command line the option is specified with --option_name
followed by values.
The general form of the member function ProgramOptionMap::add
is
where T
is the value type of the option. If the fourth (optional) argument is present, it is taken to be the default value of the option. The second template argument, Dest
is the type of the destination pointer.
If T
is the same as Dest
, then the option is a scalar option that can store a single value. Otherwise, Dest
is taken to be a container type, e.g., std::vector<T>
, and the option can store multiple values. The behavior differs when multiple values are specified in the command line. Therefore, if Dest
is a container type and is intended to be used as such, the first template argument needs to be specified explicity. The only exception is when Dest
is std::vector<T>
, in which case no template argument needs to be specified. The value type T
needs to support input stream operator>>
. The container type needs to support push_back(T &)
, in other words, it needs to be a sequential container.
To summary, the template argument deduction is shown in the following example,
Options can be processed by calling Map.process(argc, argv)
, where argc
and argv
are the typical arguments of the main
function.
After processing, one can use ProgramOptionMap::count
to obtain the number of a certain option being specified on the command line. For example,
On the command line, each option can be specified multiple times. After each option name, zero or more values can be specified. Multiple values shall be seperated by white spaces. Optionally comma can be added after each value. For example,
Note that any values specified before the first option are ignored. Any unknown options are also ignored. For example,
If the option is a single value option, it is as if only the last one is used. If the option is a multi-value option, then each of them are processed. Continue the example,
After each option, multiple value can be specified. For each value, first trailing white spaces and comma are stripped. Then, if it is a single value option, values are joint into a single string, separated by a single white space, and processed as a single value. If it is a multi-value option, then each value are processed separately, as if they are specified multiple times. For example,
A more useful example is as the following,
If for a multi-value option, each value needs to contain multiple sub values, proper shell quoting is required. For example
If the value type of an option is bool
, then it is treated specially. If no values are specified, it is treated as true
. For example,
If values are specified, then the following are treated as true: non-zero decimal integer values, e.g., 1
, 23
. y
, Y
, yes
, Yes
, YES
, t
, T
, True
, TRUE
. The following are treated as false: zero, e.g., 0
, 00
, n
, N
, no
, No
, NO
, f
, F
, false
, False
, FALSE
. In other words, for values formed entirely of digits, it is treated as an integer and tested as in C/C++; if it is a string, then the common yes
and no
and their short forms, lower/upper case variants are treated as true
and false
, respectively. And common boolean literal in programming languages (not only true
and false
as in C++) are accepted.
--help
Each option has a description string. If on the command line, the --help
option is specified, then help informations will be printed. User does not need to add this option. However, the user can add this option manually to suppress the default printing of help informations. The default --help
is a boolean option.