使用boost program_options进行动态配置

时间:2022-09-08 23:47:29

Is there a way to load a dynamic INI file like the one below.

有没有办法加载动态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

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?

有没有办法在boost program_options中处理这个问题?

4 个解决方案

#1


3  

Another, potentially more standard way, would be like this:

另一种可能更标准的方式是:

[basic]
number_of_servers=3

[server]
name=server1
ip=10.20.30.40
password=sdfslkhf    

[server]
name=server2
ip=10.20.30.41
password=sdfslkhf

[server]
name=server3
ip=10.20.30.42
password=sdfslkhf

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).

这样你就不必担心未定义的部分名称,我认为这种风格也被更广泛地使用(肯定是QuickFIX如何做到的,其方式与我概述的方式非常相似)。

And you can probably remove the number_of_servers entry, and just use the count() function to find how many server sections there are.

您可以删除number_of_servers条目,只需使用count()函数查找有多少服务器部分。

#2


2  

There is an optional bool parameter to allow for unregistered entries in the parse_config_file function. It's set to false by default. See the documentation here:

有一个可选的bool参数,允许在parse_config_file函数中使用未注册的条目。默认设置为false。请参阅此处的文档:

http://www.boost.org/doc/libs/1_45_0/doc/html/boost/program_options/parse_config_file_id991860.html

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 the variables_map as strings. You can check whether they exist with the variables_map::count function.

如果使用true调用此函数,则会将任何未注册的条目作为字符串添加到variables_map中。您可以使用variables_map :: count函数检查它们是否存在。

I hope that helps.

我希望有所帮助。

#3


0  

Sure you can. The server sections have a pattern: just load all those matching the pattern into a list of servers.

你当然可以。服务器部分有一个模式:只需将与模式匹配的所有模块加载到服务器列表中。

#4


0  

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.

最后,我依赖于带有已知/有限选项的options_description,然后使用来自parse_config_file的parsed_options,我必须收集所有无法识别的选项(collect_unrecognized)。然后我不得不迭代它以按顺序选择选项。

Thanks every one for their contribution.

感谢每一位人士的贡献。

#1


3  

Another, potentially more standard way, would be like this:

另一种可能更标准的方式是:

[basic]
number_of_servers=3

[server]
name=server1
ip=10.20.30.40
password=sdfslkhf    

[server]
name=server2
ip=10.20.30.41
password=sdfslkhf

[server]
name=server3
ip=10.20.30.42
password=sdfslkhf

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).

这样你就不必担心未定义的部分名称,我认为这种风格也被更广泛地使用(肯定是QuickFIX如何做到的,其方式与我概述的方式非常相似)。

And you can probably remove the number_of_servers entry, and just use the count() function to find how many server sections there are.

您可以删除number_of_servers条目,只需使用count()函数查找有多少服务器部分。

#2


2  

There is an optional bool parameter to allow for unregistered entries in the parse_config_file function. It's set to false by default. See the documentation here:

有一个可选的bool参数,允许在parse_config_file函数中使用未注册的条目。默认设置为false。请参阅此处的文档:

http://www.boost.org/doc/libs/1_45_0/doc/html/boost/program_options/parse_config_file_id991860.html

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 the variables_map as strings. You can check whether they exist with the variables_map::count function.

如果使用true调用此函数,则会将任何未注册的条目作为字符串添加到variables_map中。您可以使用variables_map :: count函数检查它们是否存在。

I hope that helps.

我希望有所帮助。

#3


0  

Sure you can. The server sections have a pattern: just load all those matching the pattern into a list of servers.

你当然可以。服务器部分有一个模式:只需将与模式匹配的所有模块加载到服务器列表中。

#4


0  

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.

最后,我依赖于带有已知/有限选项的options_description,然后使用来自parse_config_file的parsed_options,我必须收集所有无法识别的选项(collect_unrecognized)。然后我不得不迭代它以按顺序选择选项。

Thanks every one for their contribution.

感谢每一位人士的贡献。