我正在寻找一种在 C++ 中存储程序设置或选项或配置的解决方案。这些可能是在 GUI 中公开的设置,需要在代码运行之间保存。
在我的搜索中,我遇到了 boost.PropertyTree 这似乎是一个不错的选择。我知道 boost 是备受推崇的代码,所以我很舒服地使用它,所以我开始使用它进行开发。然后我遇到 boost.program_options 这似乎允许您可以做同样的事情,但对于程序设置的特定用例来说看起来更专业。
现在我想知道哪一个最适合这份工作? (或者是否有第三个选项比这两个选项都更好)
编辑:
仅供参考,这是一个插件,因此它不会使用命令行选项(例如,这甚至是不可能的)。
更新
我最终坚持使用boost.PropertyTree。我需要能够将更改后的选项保存回 INI,但我没有看到使用 boost.program_options 执行此操作的方法。
I was looking for a solution to store program settings or options or configuration in C++. These could be settings that are exposed in a GUI and need to be saved between runs of my code.
In my search I came across boost.PropertyTree which seemed to be a good choice. I know boost is well respected code so I'm comfortable using it and so I started developing using this. Then I come across boost.program_options which seems to allow you to do the same thing but also looks more specialized for the specific use-case of program settings.
Now I'm wondering which is the most appropriate for the job? (or is there a 3rd option that is better than both)
EDIT:
fyi this is for a plugin so it will not use command line options (as in, it's not even possible).
UPDATE
I ended up sticking with boost.PropertyTree. I needed to be able to save changed options back to the INI, and I didn't see a way of doing that with boost.program_options.
发布评论
评论(3)
使用
boost::program_options
。这正是它的用途。在一个库中,您可以获得命令行选项、环境变量选项和类似 INI 的配置文件解析器。它们都以正确的方式集成在一起,因此当用户在多个源中指定相同的选项时,库知道要考虑的正确优先级顺序。另一方面,
boost::property_tree
是一个更通用的库。该库将文本流解析为统一的数据模型。但是您需要进行真正的解析——根据您的需要理解数据块。当库看到特定选项字符串时,它不知道何时需要参数,也不知道何时不允许特定选项使用特定值或值类型。Use
boost::program_options
. It's exactly what it's for. In one library you get command line options, environment variables options and an INI-like configuration file parser. And they're all integrated together in the Right way, so when then the user specifies the same option in more than one of these sources the library knows the Right priority order to consider.boost::property_tree
on the other hand is a more generalized library. The library parses the text stream into a uniform data model. But You need to do the real parsing -- that of making sense of the blob of data for your needs. The library doesn't know when to expect a parameter when it sees a particular option string, or to disallow specific values or types of values for a particular option.经过一番挖掘后,我认为 boost.PropertyTree 仍然是最好的解决方案,因为它使我能够在从程序内更改选项后保存选项,这是一个要求。
After some digging around I think boost.PropertyTree is still the best solution because it gives me the capability to save the options after changing them from within the program which is a requirement.
也存在非 Boost 的可能性。 Config4Cpp 是我编写的一个强大、易于使用且文档齐全的配置文件解析器库。它可以在 www.config4star.org 上找到。
我建议您阅读第 3 章(实用使用指南手册中的 GUI 应用程序首选项),以了解 Config4Cpp 如何执行您想要的操作的概述。然后打开《入门指南》手册,浏览章节2 和 3 和 7.4(您可能更喜欢阅读 该手册的 PDF 版本)。这样做应该为您提供足够的详细信息,以帮助您确定 Config4Cpp 是否比 Boost 更适合您的需求。
顺便说一句,文档中指定的章节很短,因此不需要花很长时间阅读。
There is a non-Boost possibility too. Config4Cpp is a robust, simple-to-use and comprehensively documented configuration-file parser library that I wrote. It is available at www.config4star.org.
I suggest you read Chapter 3 (Preferences for a GUI Application) of the Practical Usage Guide manual to read an overview of how Config4Cpp can do what you want. Then open the Getting Started Guide manual, and skim-read Chapters 2 and 3, and Section 7.4 (you might prefer to read the PDF version of that manual). Doing that should give you sufficient details to help you decide if Config4Cpp suits your needs better or worse than Boost.
By the way, the indicated chapters and sections of documentation are short, so they shouldn't take long to read.