2

boost::program_options用来解析argv. 我希望两者-c兼而有之--config

  boost::program_options::options_description description("Utility");
  description.add_options()
    ("help,h", "display this message")
    ("config,c", boost::program_options::value<std::string>(), "Path to configuration file")
    ("config-type", boost::program_options::value<std::string>()->default_value("json"), "type of configuration file (json|xml)")
    ("verbose,v", boost::program_options::value<int>()->default_value(2), "verbosity(0 to 2)")
    ("thread,t",boost::program_options::value<int>()->default_value(0), (boost::format("Max Thread Count %1% to %2%(Processor cores of this machine) if not multi threaded") % 0 % boost::thread::hardware_concurrency()).str().c_str())
    ("action,a", boost::program_options::value<std::string>()->default_value("pack"), "action to perfoem (pack|unpack)");
  boost::program_options::positional_options_description positional_description;
  positional_description.add("action", -1);

  boost::program_options::variables_map var_map;
  boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(description).positional(positional_description).style(boost::program_options::command_line_style::unix_style).run(), var_map);
  boost::program_options::notify(var_map);

  if(var_map.count("help")){
    std::cout << description;
    return 1;
  }
  if(var_map.count("config") < 1){
    std::cout << "No Configuration file added" << std::endl;
    return 1;
  }

  if(var_map.count("action") < 1){
    std::cout << "Please specify an action to perfoem (pack|unpack)" << std::endl;
    return 1;
  }

but --config for --config=for --config="f"not work 并且打印No Configuration file added虽然-c f有效。
另外,如果我--config不带任何参数使用它会抛出异常,说明required parameter is missing in 'config-type'哪个已经有默认参数。

4

1 回答 1

1

您的问题似乎与此处描述的完全相同:boost::program_options - 它是否对命令行选项进行了精确的字符串匹配?

我正在使用带有 boost 1.42 的 ubuntu,而 ubuntu repo 没有更高版本。但是 1.42 有那么大吗?

是的。

--config您可以通过交换和的规范来解决此问题--config-type

更清洁的解决方案是升级 Boost。Vladimir Prus 在提到的 SO 答案中说该错误已在 1.45 中修复。从您写的内容来看,我想您使用的是 Ubuntu 11.04 (Natty Narwhal)。您可以执行以下任一操作:

  1. 从 Ubuntu 11.10 (Boost 1.46) 或 Ubuntu 12.04 (Boost 1.48) 安装更新的 Boost 软件包 - 通过在 /etc/apt/sources.list 中分别用 oneiric 或精确替换 natty 或 11.10 或 12.04
  2. 安装较新的 Boost 包包含 Boost backports 的 PPA(例如https://launchpad.net/~purplekarrot/+archive/ppa
  3. 按照 Xeo 的建议从源代码构建 Boost。
于 2012-08-19T20:38:32.940 回答