hyperrealm / libconfig

C/C++ library for processing configuration files
https://hyperrealm.github.io/libconfig/
GNU Lesser General Public License v2.1
1.1k stars 359 forks source link

how to use libconfig to read several config files in one object? #197

Closed nickhuangxinyu closed 2 years ago

nickhuangxinyu commented 2 years ago

Assume I have a libconfig::config object, can i readFile for several times like this:

libconfig::Config cfg;
cfg.readFiles("./a.cfg");
cfg.readFiles("./b.cfg");

and what will happen if a.cfg and b.cfg have same key?

If this doesnt work, is there any methods can let me merge two config files' setting into one object?

thomastrapp commented 2 years ago

can i readFile for several times like this

readFiles clears the config (libconfig.c): The values from "a.cfg" will be discarded, when calling readFiles a second time.

You can use @include:

A configuration file may “include” the contents of other files using an include directive. This directive has the effect of inlining the contents of the named file(s) at the point of inclusion.

See also the option Config::OptionAllowOverrides, which allows you to include configuration files that override already existing settings.

nickhuangxinyu commented 2 years ago

@thomastrapp thanks very much.

@inlcude is cool, can you teach me about, where can i find all @ wildcards setting (for example: @include, @...) for libconfig?