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

  • Users starred: 8
  • Users forked: 1
  • Users watching: 8
  • Updated at: 2020-05-15 22:37:33

Build Status Coverage Status


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
  • unmarshalling configuration data to native C++ types
  • setting defaults
  • TODO: reading from environment variables
  • TODO: 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


Also one or both 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
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";


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("logLevel", "trace");

Environment Variables


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 and YAML libraries for requirements.

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

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