|
| SimpleConfig () |
| The constructor doesn't need any initialisation. More...
|
|
| SimpleConfig (std::string &prefix) |
| The constructor can explicitly set the environment prefix. More...
|
|
int | Load (const char *path) |
| Load key-value pairs from a file. More...
|
|
int | Load (std::string &path) |
| Load key-value pairs from a file. More...
|
|
void | SetPrefix (const std::string &prefix) |
| Set the prefix in use by the config. More...
|
|
std::vector< std::string > | KeyList () |
| Return the keys cached in the config. More...
|
|
std::string | Get (std::string &key) |
| Get the value stored for the key from the config. More...
|
|
std::string | Get (const char *key) |
| Get the value stored for the key from the config. More...
|
|
std::string | Get (std::string &key, std::string defaultValue) |
| Get the value stored for the key from the config. More...
|
|
std::string | Get (const char *key, std::string defaultValue) |
| Get the value stored for the key from the config. More...
|
|
void | Put (std::string &key, const std::string value) |
| Set a configuration value. This will override any value set. More...
|
|
void | Put (const char *key, const std::string value) |
| Set a configuration value. This will override any value set. More...
|
|
SimpleConfig is a basic configuration for projects.
SimpleConfig is a basic key-value system. It can optionally load key-value pairs from a file, which should consist of key = value lines. Comments may be added by starting the line with a '#'; these lines will be skipped. Comments may have leading whitespace. Any empty lines or lines consisting solely of whitespace will also be skipped.
When values are retrieved by one of the variants on Get, they are looked up in the following order, assuming key
and an optional prefix
set on the config:
- Any cached key-value pairs. Loading a file caches the key-value pairs in the file. The file is not used again, unless another call to Load is made. If the cache has a name for
key
, it will be returned.
- The value is looked up from the environment. An optional prefix can be set; if so, if there is an environment named
{prefix}{key}
, the value will be cached and it will be returned.
- If a default value has been provided, it will be cached and returned.
- If none of the previous steps has provided a value, an empty string will be returned.
In Go projects, I've used the global config to great success. However, callers may set up an explicit configuration instance.