2012-08-03 85 views
2

我正在使用boost::program_options来解析argv。我希望双方-c--configboost :: program_options使用-p但不使用--param

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没有任何参数它会抛出异常说required parameter is missing in 'config-type'已经有一个默认参数。

+1

长选项使用' - 选项=值'格式,IIRC。 – Xeo 2012-08-03 15:27:24

+0

我也试过这个。这也没有工作 – 2012-08-03 15:30:14

+0

您的代码在VC10上的预期效果与'--config = f'和'-c f' – Praetorian 2012-08-03 15:44:57

回答

1

你的问题似乎是完全一样的描述如下:boost::program_options - does it do an exact string matching for command line options?

我使用Ubuntu与提升1.42和Ubuntu回购不具有 更高的版本。但是是1.42那辆越野车?

是的。

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

更清洁的解决方案是升级Boost。在所提到的答案中,弗拉基米尔·普鲁斯说这个错误在1.45中被修复。从你写的东西我想你使用的是Ubuntu 11.04(Natty Narwhal)。你可以做以下任一操作:

  1. 从Ubuntu的11.10(升压1.46)或Ubuntu 12.04(升压1.48)安装新的升压包 - 通过暂时在/ etc分别替换整洁的与precise中或精确为11.10和12.04/apt/sources.list
  2. 安装较新的Boost包含一个包含Boost后端口的PPA(例如,如https://launchpad.net/~purplekarrot/+archive/ppa
  3. 从Xeo建议的源代码构建Boost。
+0

是的,当我将'config-type'重命名为'format'时,它工作正常我正在寻找这样的错误报告 – 2012-08-20 03:54:54

相关问题