使用 boost program_options 进行动态配置

发布于 2024-10-07 06:31:38 字数 322 浏览 0 评论 0原文

有没有一种方法可以加载如下所示的动态 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ペ泪落弦音 2024-10-14 06:31:38

另一种可能更标准的方法是这样的:

[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

这样你就不需要担心未定义的部分名称,而且我认为这种风格也被更广泛地使用(绝对是 QuickFIX 的做法,以一种非常相似的方式)到我概述的内容)。

您可以删除 number_of_servers 条目,然后使用 count() 函数来查找有多少个 server 部分。

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

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

想你的星星会说话 2024-10-14 06:31:38

有一个可选的 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 the parse_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 the variables_map as strings. You can check whether they exist with the variables_map::count function.

I hope that helps.

尽揽少女心 2024-10-14 06:31:38

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

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

尝蛊 2024-10-14 06:31:38

我在解决这个问题时面临的挑战是确保各个部分保持在一起并且不会混淆。

最后,我依赖于具有已知/有限选项的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文