【发布时间】:2020-12-26 01:04:48
【问题描述】:
我目前正在尝试修改一些交给我的代码。代码的原点是读取配置文件,并在文件中设置不同的选项 boost::program_options::variable_map,然后在代码的其他部分中读取已经可以正常工作的部分。
这是我要替换的代码:
// Some helpful definitions
boost::program_options::variables_map vm;
std::string filecfg = "File_name";
std::ifstream ifs(filecfg.c_str());
// Setting options (This is command line example, but config file is the same)
boost::program_options::options_description df("Command Line");
df.add_options()
("help", "display help message")
("file-cfg,f", boost::program_options::value<std::string>(), "config file")
("version", "display version and revision number");
boost::program_options::parsed_options parsedc = boost::program_options::parse_config_file(ifs, df, true);
boost::program_options::store(parsedc, vm);
boost::program_options::notify(vm);
std::vector <std::string> unrc = boost::program_options::collect_unrecognized(parsedc.options, boost::program_options::include_positional)
我的想法是简单地替换 boost::program_options::parsed_options parsedc 并自己创建这个对象。我遇到的问题只是没有关于如何执行此操作的文档。我认为这主要是因为它不是为这种方式设计的。
无论如何,我只是想用 dc 中描述的选项以及可以保存在单独数据结构(如向量)中的值来填充 vm 对象。
是否可以简单地向 vm 添加值?还是必须通过 boost::program_options::store() 之类的函数?
任何帮助将不胜感激!如果有不清楚的地方,或者有什么想让我尝试的,请告诉我!
谢谢!
【问题讨论】:
标签: c++ boost boost-program-options