gpt4 book ai didi

c++ - boost::program_options 使用 -p 但不使用 --param

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:31:11 24 4
gpt4 key购买 nike

我正在使用 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;
}

但是 --config f--config=f--config="f" 不起作用并打印 No Configuration file added 虽然 -c f 有效。
如果我使用 --config 没有任何参数,它会抛出异常说 “config-type”中缺少必需的参数,它已经有一个默认参数。

最佳答案

您的问题似乎与此处描述的完全相同:boost::program_options - does it do an exact string matching for command line options?

I am using ubuntu with boost 1.42 and ubuntu repo doesn't have a higher version. but is 1.42 that buggy ?

是的。

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

更清洁的解决方案是升级 Boost。提到的 SO 答案中的 Vladimir Prus 说该错误已在 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 或 precise 分别临时替换 natty 或 11.10 或 12.04
  2. 安装更新的 Boost 包一个包含 Boost backports 的 PPA(例如 https://launchpad.net/~purplekarrot/+archive/ppa)
  3. 按照 Xeo 的建议从源代码构建 Boost。

关于c++ - boost::program_options 使用 -p 但不使用 --param,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11798646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com