What is the difference between default_value and implicit_value in boost::program_options?
Asked Answered
T

3

39

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

Thanks!

Thrombosis answered 13/5, 2010 at 19:47 Comment(0)
M
44

default_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
Mobster answered 13/5, 2010 at 20:25 Comment(0)
M
2

Here is an example

    desc.add_options()
        ("help", "produce help message")
        ("optimization", po::value<int>(&opt)->default_value(10), 
              "optimization level")
        ("verbose,v", po::value<int>()->implicit_value(1),
              "enable verbosity (optionally specify level)")
        ("listen,l", po::value<int>(&portnum)->implicit_value(1001)
              ->default_value(0,"no"),
              "listen on a port.")
        ("include-path,I", po::value< vector<string> >(), 
              "include path")
        ("input-file", po::value< vector<string> >(), "input file")
    ;

If you don't input either optimization or listen,

./myApp.exe

output:

 Optimization level is 10
 Listen port is 0

If you input option --optimization without an argument, it will nofity missing argument. And if you input --listen without an argument, it will give an implicit value 1001.

Mechanic answered 15/1, 2020 at 7:10 Comment(0)
D
0

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.

Deuno answered 13/5, 2010 at 20:23 Comment(1)
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.Mobster

© 2022 - 2024 — McMap. All rights reserved.