سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

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

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top