13

我正在寻找一种在 C++ 中存储程序设置或选项或配置的解决方案。这些可能是在 GUI 中公开的设置,需要在我的代码运行之间保存。

在我的搜索中,我遇到了boost.PropertyTree,这似乎是一个不错的选择。我知道 boost 是受人尊敬的代码,所以我很喜欢使用它,所以我开始使用它进行开发。然后我遇到了boost.program_options,它似乎允许你做同样的事情,但对于程序设置的特定用例来说看起来更专业。

现在我想知道哪个最适合这份工作?(或者是否有比两者都好的第三种选择)

编辑: 仅供参考,这是一个插件,因此它不会使用命令行选项(如,它甚至不可能)。

更新

我最终坚持使用 boost.PropertyTree。我需要能够将更改的选项保存回 INI,但我没有看到使用 boost.program_options 的方法。

4

3 回答 3

10

使用boost::program_options. 这正是它的用途。在一个库中,您可以获得命令行选项、环境变量选项和类似 INI 的配置文件解析器。并且它们都以正确的方式集成在一起,因此当用户在多个这些源中指定相同的选项时,库知道要考虑的正确优先级顺序。

boost::property_tree另一方面是一个更通用的库。该库将文本流解析为统一的数据模型。但是需要进行真正的解析——根据您的需要理解数据块。当看到特定选项字符串时,库不知道何时需要参数,或者不允许特定选项的特定值或值类型。

于 2011-05-10T00:08:45.007 回答
6

经过一番挖掘,我认为 boost.PropertyTree 仍然是最好的解决方案,因为它使我能够在从程序中更改选项后保存选项,这是一项要求。

于 2011-05-12T00:37:29.993 回答
1

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.

于 2011-05-11T16:16:05.080 回答