2012-08-03 87 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

相關問題