boost Boost Program Options Default Values


A default valued command line argument can be specified easily:

// declare options
  ("name", po::value<std::string>()->required(), "Type your name to be greeted!")
  ("rank", po::value<std::string>()->default_value("Dark Knight"), "Your rank");

Its value is also added to the variable map:

  std::cout << "Hello " << vm["name"].as<std::string>() << " " << vm["rank"].as<std::string>() << std::endl;

The default value is shown in the description...

$ ./a.out
Error: the option '--name' is required but missing
Allowed Options:
  --name arg                Type your name to be greeted!
  --rank arg (=Dark Knight) Your rank

... and used if not specified...

$ ./a.out --name Batman
Hello Batman Dark Knight

... but can be overwritten at command line:

 $ ./a.out --name Batman --rank FlyingSquirrel
Hello Batman FlyingSquirrel