【问题标题】:boost::program_options - how to handle multiple sections with the same name in INI fileboost::program_options - 如何处理 INI 文件中具有相同名称的多个部分
【发布时间】:2011-05-29 09:17:04
【问题描述】:

在如下配置中;有没有办法处理各个部分。

我正在寻找一种方法来以可靠的方式验证下面的各个“服务器”部分。

[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

【问题讨论】:

    标签: c++ configuration-files ini boost-program-options


    【解决方案1】:

    使用boost::program_options 解析INI 文件时,选项名称​​必须以其封闭的部分名称作为前缀。

    换句话说,部分是选项“标识符”的一部分,但我认为您没有办法识别给定server.ip 变量属于哪个部分(因此,这是关联的server.password)。

    我认为你应该考虑Boost.PropertyTree(它也支持INI文件解析)来完成这个任务。

    【讨论】:

      【解决方案2】:

      来自here

      选项名是相对于节名的,所以下面的配置文件部分:

        [gui.accessibility]
        visual_bell=yes
      

      等价于

        gui.accessibility.visual_bell=yes
      

      但目前没有办法区分同名的部分。

      更新:

      Qt 的 QSettings 通常通过使用“/n”对数组中的值(节?)进行后缀来解决这个问题。所以你可以使用:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-17
        • 1970-01-01
        • 1970-01-01
        • 2016-02-04
        • 1970-01-01
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多