使用boost :: options而不给出参数值

时间:2015-03-22 17:31:19

标签: c++ boost flags

我想使用boost :: program_option运行程序而不给出值(只有标志)。 就像那样(这里有一些其他标志):

./main.x -b -f test.txt -r 26

现在你必须写:

./main.x -b 1 -f test.txt -r 26

关键字是(-b)。我不想写,例如1。

样品:

// boost fields
boost::program_options::variables_map fMap;
boost::program_options::options_description fOptions;

// Add some options to fOptions
fOptions.add_options()
("progressBar,b", po::value<int>(), "Progress bar.");
// some other options

boost::program_options::store(po::parse_command_line(argc, argv, fOptions), fMap);

if(fMap.count("progressBar"))
{
  int l_progressBar = fMap["progressBar"].as<int>();
  if(l_progressBar == 1)
  {
    // some staff
  }
}

//I would like to have something like that:
fOptions.add_options()
("progressBar,b", po::value<void>(), "Progress bar.");

if(fMap.count("progressBar"))
{
  // check if flag (-b) was set and do some staff
}

1 个答案:

答案 0 :(得分:0)

双参数operator()应该可以解决问题:

fOptions.add_options()
("progressBar,b", "Progress bar.");