That's the question. Why would I use implicit_value over default_value and vice versa?
Thanks!
That's the question. Why would I use implicit_value over default_value and vice versa?
Thanks!
 
    
    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
 
    
     
    
    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.
 
    
    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.
