我正在使用boost::program_options
來解析argv
。我希望雙方-c
和--config
boost :: 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'
已經有一個默認參數。
長選項使用' - 選項=值'格式,IIRC。 – Xeo 2012-08-03 15:27:24
我也試過這個。這也沒有工作 – 2012-08-03 15:30:14
您的代碼在VC10上的預期效果與'--config = f'和'-c f' – Praetorian 2012-08-03 15:44:57