c++: program settings - boost.PropertyTree or boost.program_options?
Asked Answered
P

3

13

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.

Photographic answered 9/5, 2011 at 23:53 Comment(0)
P
6

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.

Photographic answered 12/5, 2011 at 0:37 Comment(0)
C
10

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.

Codicodices answered 10/5, 2011 at 0:8 Comment(4)
forgot to mention (I edited my post) that I will not be using command line options at all. Does that change your answer or do you think even if you purely use the INI functionality it's a better option?Photographic
@User, I don't think it does. As @Codicodices stated, it considers all 3 sources. I think the order is: ini file -> environment -> command line, with the last encountered value winning. Strictly speaking, one does not even need to pass in the command-line args, so you should be fine if you completely omitted them.Illicit
It doesn't change my answer. I for example sometimes use boost::program_options even though I don't use environment variables or INI files. Or even when I don't need either of these.Codicodices
does the library support saving options back to the ini file? For example the user changes an option through a GUI and then my program saves the changed option value back to the ini file?Photographic
P
6

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.

Photographic answered 12/5, 2011 at 0:37 Comment(0)
T
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.

Traditional answered 11/5, 2011 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.