DeutscherDude / config-cxx

C++ Config library for managing application configuration.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Config C++

Manage your C++ application config.

clang++ apple clang++ g++ msvc codecov PRs Welcome Chat on Discord

🎯 Goal

Goal of the Config C++ is to provide a library like node-config for C++ community.

Introduction

Config C++ organizes hierarchical configurations for your app deployments.

It lets you define a set of default parameters, and extend them for different deployment environments (development, testing, staging, production, etc.).

Configurations are stored in configuration files within your application, and can be overridden and extended by environment variables.

Quick Start

The following examples are in JSON format (YAML in progress).

Install in your app directory, and edit the default config file:

  1. Add config to git submodules (execute in project root):
$ mkdir externals && cd externals
$ git submodule add https://github.com/cieslarmichal/config-cxx.git
$ git submodule update --init --recursive
  1. Link with library:
set(BUILD_CONFIG_CXX_TESTS OFF)

add_subdirectory(externals/config-cxx)

add_executable(main Main.cpp)

target_link_libraries(main config-cxx)
$ mkdir config
$ vi config/default.json
{
  "db": {
    "name": "users",
    "host": "localhost",
    "port": 3306,
    "user": "default",
    "password": "default"
  }
}

Edit config overrides for production deployment:

 $ vi config/production.json
{
  "db": {
    "host": "postgres",
    "user": "admin",
    "password": "secretpassword"
  }
}

Use configs in your code:

#include <iostream>
#include "config-cxx/Config.h"

config::Config config;

auto dbName = config.get<std::string>("db.name"); // "users"
auto dbHost = config.get<std::string>("db.host"); // "postgres"
auto dbPort = config.get<int>("db.port"); // 3306
auto dbUser = config.get<int>("db.user"); // "admin"
auto dbPassword = config.get<int>("db.password"); // "secretpassword"
auto authEnabled = config.get<bool>("auth.enabled"); // true

config.get() will throw an exception for undefined keys to help catch typos and missing values. Use config.has() to test if a configuration value is defined.

Before starting your app specify target CXX environment:

$ export CXX_ENV=production

Running in this configuration, the port and name elements of db will come from the default.json file, and the host, user and password elements will come from the production.json override file.

📖 Articles

Examples

Check out example project in examples directory.

Compiler support

Dependencies

  • GTest (BUILD_CONFIG_CXX_TESTS=OFF CMake flag to disable)
  • nlohmann/json

Contributing

Feel free to join Config C++ development! 🚀

Please check CONTRIBUTING guide.

Discord Channel for contributors.

📝 Compilation guides

About

C++ Config library for managing application configuration.

License:MIT License


Languages

Language:C++ 56.7%Language:CMake 42.2%Language:Dockerfile 0.7%Language:Shell 0.4%