使用 boost program_options 进行动态配置
有没有一种方法可以加载如下所示的动态 INI 文件。
[basic]
number_of_servers=3
[server1]
ip=10.20.30.40
password=sdfslkhf
[server2]
ip=10.20.30.41
password=sdfslkhf
[server3]
ip=10.20.30.42
password=sdfslkhf
这里的想法是,这里定义的服务器非常特定于软件的部署;因此管理员决定有多少服务器参与配置。
有没有办法在 boost program_options 中处理这个问题?
Is there a way to load a dynamic INI file like the one below.
[basic]
number_of_servers=3
[server1]
ip=10.20.30.40
password=sdfslkhf
[server2]
ip=10.20.30.41
password=sdfslkhf
[server3]
ip=10.20.30.42
password=sdfslkhf
Here the idea is that the servers that are defined here is very specific to the software's deployment; so the admin decides how many servers participate in the configuration.
Is there a way to handle this in boost program_options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种可能更标准的方法是这样的:
这样你就不需要担心未定义的部分名称,而且我认为这种风格也被更广泛地使用(绝对是 QuickFIX 的做法,以一种非常相似的方式)到我概述的内容)。
您可以删除
number_of_servers
条目,然后使用count()
函数来查找有多少个server
部分。Another, potentially more standard way, would be like this:
This way you don't need to worry about undefined section names, and I think this style is more widely used as well (definitely it's how QuickFIX does it, in a way very similar to what I outlined).
And you can probably remove the
number_of_servers
entry, and just use thecount()
function to find how manyserver
sections there are.有一个可选的 bool 参数允许在 parse_config_file 函数中未注册的条目。默认情况下它设置为 false。请参阅此处的文档:
http://www. boost.org/doc/libs/1_45_0/doc/html/boost/program_options/parse_config_file_id991860.html
如果您使用
true
调用此函数,那么它会将所有未注册的条目添加到variables_map
作为字符串。您可以使用variables_map::count
函数检查它们是否存在。我希望这有帮助。
There is an optional
bool
parameter to allow for unregistered entries in theparse_config_file
function. It's set to false by default. See the documentation here:http://www.boost.org/doc/libs/1_45_0/doc/html/boost/program_options/parse_config_file_id991860.html
If you call this function with
true
then it will add any unregistered entries into thevariables_map
as strings. You can check whether they exist with thevariables_map::count
function.I hope that helps.
当然可以。服务器部分有一个模式:只需将所有与该模式匹配的内容加载到服务器列表中即可。
Sure you can. The server sections have a pattern: just load all those matching the pattern into a list of servers.
我在解决这个问题时面临的挑战是确保各个部分保持在一起并且不会混淆。
最后,我依赖于具有已知/有限选项的 options_description ,然后使用来自 parse_config_file 的 parsed_options ,我必须收集所有无法识别的选项(collect_unrecognized)。然后我必须迭代它以按顺序选择选项。
感谢每一个人的贡献。
The challenges I faced while resolving this was to make sure the sections are kept together and are no way mixed up.
In the end I relied on a options_description with the known/finite options and then using the parsed_options that come out of parse_config_file, I had to collect all unrecognized options ( collect_unrecognized ). Then I had to iterate it to pick the options in order.
Thanks every one for their contribution.