您能帮我解决boost::program_options
吗?
我希望解析器忽略保存在配置文件中的未知选项。
我知道 allow_unregistered()
可用于 cmd 行选项,我该如何处理文本文件?
这里是剥离的代码:
namespace po = boost::program_options;
try {
string config_file;
string gps_source;
int op_baud;
po::options_description generic("Generic options");
generic.add_options()
("ssdvpacksize", po::value<int>(),
"ssdv packets size in bytes")
("ssdvdir", po::value<string>()->default_value("/ARY1/ssdv"),
"ssdv image dir")
//unused
//I have to specify these even if they're unused
("ssdvproc_dir", po::value<string>(), "")
;
po::options_description file_options;
file_options.add(generic);
po::options_description cli_options("command line interface options");
cli_options.add(generic);
cli_options.add_options()
("config", po::value<string>(&config_file)->default_value("/boot/ary-1.cfg"), "name of a file of a configuration.");
po::variables_map vm;
store( po::command_line_parser(ac, av).options(cli_options).allow_unregistered().run(), vm );
//store( po::basic_command_line_parser<char>(ac, av).options(cli_options).allow_unregistered().run(), vm );
notify(vm);
ifstream ifs(config_file.c_str());
if (!ifs)
{
cout << "Can not open config file: " << config_file << "\n";
}
else
{
// probably smth. to do here ?
//store(parse_config_file(ifs, file_options).allow_unregistered(), vm); // does not work
store(parse_config_file(ifs, file_options), vm);
notify(vm);
}
// ...
// rest of program
}
最佳答案
好吧,解决方案非常简单。 第 44 行应该是:
store(parse_config_file(ifs, file_options, true/*allow unregistered*/), vm);
关于c++ - boost::program_options 忽略配置文件中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31892097/