boost::program_options - how to handle multiple sections with the same name in INI file

蓝咒 提交于 2020-01-02 02:38:06

问题


In a config like below; is there a way to handle individual sections.

I am looking for a way to validate individual "server" sections below, in a reliable manner.

[basic]
number_of_servers=3

[server]
ip=10.20.30.40
password=sdfslkhf    

[server]
ip=10.20.30.41
password=sdfslkhf

[server]
ip=10.20.30.42
password=sdfslkhf

[server]
password=sdfslkhf

[server]
ip=10.20.30.42

回答1:


When using boost::program_options to parse a INI file, the option names must be prefixed by their enclosing section names.

In other words, sections are part of the option 'identifier', but I don't think you have a way to identify to which section a given server.ip variable belongs (and thus, which is the associated server.password).

I think you should consider Boost.PropertyTree (which also supports INI file parsing) for this task.




回答2:


From here:

The option names are relative to the section names, so the following configuration file part:

  [gui.accessibility]
  visual_bell=yes

is equivalent to

  gui.accessibility.visual_bell=yes

But there is currently no way to distinguish sections with the same name.

UPDATE:

Qt's QSettings usually solves this by postfixing values (sections?) from an array with "/n". So you could use:

[server/0]
...
[server/1]
...
[server/2]
...


来源:https://stackoverflow.com/questions/4509062/boostprogram-options-how-to-handle-multiple-sections-with-the-same-name-in-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!