默认值

可以轻松指定默认值命令行参数:

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

它的值也被添加到变量 map:

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

默认值显示在说明中…

$ ./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

…如果没有指定则使用…

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

…但可以在命令行覆盖:

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