13

I have an application that uses Boost.Program_options to store and manage its configuration options. We are currently moving away from configuration files and using database loaded configuration instead. I've written an API that reads configuration options from the database by hostname and instance name. (cool!) However, as far as I can see there is no way to manually insert these options into the boost Program_options. Has anyone used this before, any ideas? The docs from boost seem to indicate the only way to get stuff in that map is by the store function, which either reads from the command line or config file (not what I want). Basically looking for a way to manually insert the DB read values in to the map.

4

2 回答 2

17

我的回答来得太晚了,但我花了一些时间尝试做类似的事情并找到了一个令人讨厌的明显解决方案(以防其他人正在寻找这个)......

回想一下boost::program_options::variables_mapstd::map<std::string, boost::program_options::variable_value>您可以进行完全合法的 STL 映射处理,包括插入...

命名空间 po = boost::program_options;
po::variables_map 虚拟机;
vm.insert(std::make_pair("MyNewEmptyOption", po::variable_value());
vm.insert(std::make_pair("MyNewIntOption", po::variable_value(32, false));
po::notify(vm);

-埃德蒙-

于 2010-04-22T11:11:14.577 回答
3

你看过extra_parser或的allow_unregistered功能Boost::Program_Options吗?根据您的程序的确切运行方式,它们中的一个或两个应该能够支持您想要的。

于 2009-05-11T13:41:08.530 回答