Skip to content

C++ Configuration management library inspired by the Viper package for golang.

License

Notifications You must be signed in to change notification settings

CJLove/config-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

61c2a92 · Dec 14, 2024
Dec 7, 2024
Dec 14, 2024
Feb 18, 2023
Jul 8, 2021
Feb 4, 2024
Jan 1, 2019
Sep 29, 2023
Oct 21, 2022
Jul 8, 2020
Feb 25, 2023
Nov 4, 2023
Sep 29, 2023
Jan 1, 2019
Sep 29, 2023
Jan 1, 2019
Sep 30, 2023
Dec 31, 2018
Sep 30, 2023
Feb 4, 2024
Jul 8, 2019

Repository files navigation

ci CodeQL

config-cpp

C++ Configuration library patterned after viper package for golang. It is designed to work within an application and handle configuration needs.

  • framework for companion libraries which can read from JSON, YAML, TOML
  • unmarshalling configuration data to native C++ types where supported
  • setting defaults
  • TODO: reading from environment variables
  • reading from command line flags
  • live watching and re-reading of config files or Kubernetes ConfigMaps

ConfigCpp uses the following precedence order. Each item takes precedence over the item(s) below it:

  • command-line flags
  • TODO: environment variables
  • configuration file
  • default

Dependencies

Also one or more of the following:

Basic Usage

Reading Config Files

ConfigCpp::ConfigCpp config;
config.SetConfigName("config"); // Name of config file minus extension
config.AddConfigPath("/etc/appname/"); // Path to look for config files
config.AddConfigPath("/home/.appname/"); // call as many times as needed
config.AddConfigPath(".");
if (!config.ReadInConfig()) {
    std::cerr << "Failed to read in config\n";
}

Watching and Re-reading Config Files

// Callback function called upon config file change
void onConfigChange(ConfigCpp::ConfigCpp &config) {
    std::cout << "Config changed\n";
    config.ReadInConfig();
    ...
}

config.OnConfigChange(onConfigChange);
config.WatchConfig();

Setting Defaults

Default values can be specified for keys in case values aren't specified in config file(s), environment variables, or command-line flags.

config.SetDefault("key1",false);
config.SetDefault("key2",255);
config.SetDefault("key3",0.95);
config.SetDefault("logLevel", "trace");

Environment Variables

TBD

Command-line Flags

Command line flags can be added as follows, following the cxxopts convention of specifying the long name optionally preceded by the short name separated by a comma.

config.AddBoolOption("b,top-bool","Bool option");
config.AddIntOption("t,top-int","Integer option");
config.AddStringOption("s,top-string","String option");
config.AddDoubleOption("d,top-double","Double option");
config.AddStringOption("long-only","String option w/no short name");

By default a --help option is added, which will result in displaying the help output from cxxopts and then exiting with return code 1. Errors encountered while parsing command-line arguments will result displaying the error message and help output from cxxopts and then exiting with return code 1.

Retrieving Configuration Values

std::string stringVal = config.GetString("key");
bool flagVal = config.GetBool("key.subkey");
int intVal = config.GetInt("key.intVal");
double doubleVal = config.GetDouble("key.double");

Unmarshalling to native types

See the JSON, YAML and TOML libraries for requirements.

JsonType myJsonConfig;
if (config.UnmarshalJson<JsonType>(myJsonConfig)) {
    ...
}

YamlType myYamlConfig;
if (config.UnmarshalYaml<YamlType>(myYamlConfig)) {
    ...
}

TomlType myTomlConfig;
if (config.UnmarshalToml<TomlType>(myTomlConfig)) {
    ...
}

About

C++ Configuration management library inspired by the Viper package for golang.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published