views:

105

answers:

2

That's the question. Why would I use implicit_value over default_value and vice versa?

Thanks!

A: 

If I remember right, the difference arises with an option that allows something like -X=Y (where "Y" might be, for example, a number). The default value is what you get if the user hasn't entered a -X on the command line. An implicit value is is what you get if the user enters -X on the command line without specifying a value.

Consider, for example, gcc, which supports optimization levels from 0 to 3 (IIRC). If you don't specify -O at all, it defaults to -O0 (i.e., optimization is turned off). If you specify -O (with no number) you get the implicit value, equivalent to -O1. You can also specify -O1 explicitly, or you can specify -O2 or -O3.

Jerry Coffin
It doesn't necessarily reflect on what the user specified on the commandline. Boost's program_options uses `options_descriptor` for all its parsers, so if for example the value is specified in a configuration file or an environment-variable then the library won't fallback to the `default_value`.
wilhelmtell
@WilhelmTell:Quite true.
Jerry Coffin
A: 

deafult_value() is the value that will be put in the variables_map if the user didn't specify another value:

./a.out             # implies width=75 if that's the default_value for width
./a.out --width=80  # default_value not used

implicit_value() is the value that will be used if the user specifies the option but without an adjacent value.

./a.out --width     # implies width=75 if that's the implicit_value for width
./a.out --width=80  # implicit value not used

If you use implicit_value then in commandline options's short options the user must specify the value immediately after the option:

./a.out -w80   # implicit_value not used
./a.out -w 80  # wrong: 80 parsed as extra arg if implicit_value is defined
wilhelmtell