https://github.com/cieslarmichal/config-cxx
C++ Config library for managing application configuration.
https://github.com/cieslarmichal/config-cxx
config config-management cpp cpp20 json
Last synced: 3 days ago
JSON representation
C++ Config library for managing application configuration.
- Host: GitHub
- URL: https://github.com/cieslarmichal/config-cxx
- Owner: cieslarmichal
- License: mit
- Created: 2023-12-12T22:05:13.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-27T19:15:24.000Z (about 1 year ago)
- Last Synced: 2024-10-23T08:17:30.261Z (12 months ago)
- Topics: config, config-management, cpp, cpp20, json
- Language: C++
- Homepage:
- Size: 491 KB
- Stars: 21
- Watchers: 3
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Config C++
Manage your C++ application config.
[](https://github.com/cieslarmichal/config-cxx/actions/workflows/linux-clang-build.yml?query=branch%3Amain)
[](https://github.com/cieslarmichal/config-cxx/actions/workflows/macos-clang-build.yml?query=branch%3Amain)
[](https://github.com/cieslarmichal/config-cxx/actions/workflows/linux-gxx-build.yml?query=branch%3Amain)
[](https://github.com/cieslarmichal/config-cxx/actions/workflows/windows-msvc-build.yml?query=branch%3Amain)
[](https://codecov.io/github/cieslarmichal/config-cxx)
[](http://makeapullrequest.com)
[](https://discord.gg/h2ur8H6mK6)## Table of contents
- [Goal](#goal)
- [Introduction](#introduction)
- [Quick start](#quick-start)
- [Installing config-cxx](#install-in-your-app-directory-and-edit-the-default-config-file)
- [Editing config overrides](#edit-config-overrides-for-production-deployment)
- [Usage](#use-configs-in-your-code)
- [Articles](#-articles)
- [Configuration Files](./docs/wiki/configuration-files.md)
- [Environment Variables](./docs/wiki/environment-variables.md)
- [Examples](#examples)
- [Compiler support](#compiler-support)## 🎯 Goal
Goal of the Config C++ is to provide a library like [node-config](https://github.com/node-config/node-config) to the 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](./docs/wiki/configuration-files.md) within your application, and can be overridden and extended by [environment variables](./docs/wiki/environment-variables.md).
## Quick Start
The following examples are in JSON format (YAML and XML are also supported).
### Install in your app directory, and edit the default config file:
1. Add config to git submodules (execute in project root):
```shell
$ mkdir externals && cd externals
$ git submodule add https://github.com/cieslarmichal/config-cxx.git
$ git submodule update --init --recursive
```2. Link with library:
```cmake
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
``````js
{
"db": {
"name": "users",
"host": "localhost",
"port": 3306,
"user": "default",
"password": "default"
}
}
```### Edit config overrides for production deployment:
```shell
$ vi config/production.json
``````json
{
"db": {
"host": "postgres",
"user": "admin",
"password": "secretpassword"
}
}
```### Use configs in your code:
```cpp
#include
#include "config-cxx/Config.h"config::Config config;
auto dbName = config.get("db.name"); // "users"
auto dbHost = config.get("db.host"); // "postgres"
auto dbPort = config.get("db.port"); // 3306
auto dbUser = config.get("db.user"); // "admin"
auto dbPassword = config.get("db.password"); // "secretpassword"
auto authEnabled = config.get("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:
```shell
$ 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
* [Configuration Files](./docs/wiki/configuration-files.md)
* [Environment Variables](./docs/wiki/environment-variables.md)## Examples
Check out the example project in [examples directory](https://github.com/cieslarmichal/config-cxx/tree/main/examples/project).
## Compiler support
- [MSVC➚](https://en.wikipedia.org/wiki/Microsoft_Visual_Studio) version 143 or newer.
- [GCC➚](https://gcc.gnu.org/) version 13 or newer.
- [Clang➚](https://clang.llvm.org/) version 16 or newer.
- [Apple Clang➚](https://clang.llvm.org/) version 16 or newer.## Dependencies
- GTest (set ```BUILD_CONFIG_CXX_TESTS=OFF``` CMake flag to disable this dependency)
- nlohmann/json## Contributing
Feel free to join Config C++ development! 🚀
Please check [CONTRIBUTING](https://github.com/cieslarmichal/config-cxx/blob/main/CONTRIBUTING.md) guide.
Visit us on [discord](https://discord.gg/h2ur8H6mK6) if you want to know more about contributing.
## 📝 Compilation guides
- [Clang++](./docs/guides/clang-compilation-guide.md)
- [Apple Clang++](./docs/guides/apple-clang-compilation-guide.md)
- [G++](./docs/guides/gcc-compilation-guide.md)
- [MSVC](./docs/guides/msvc-compilation-guide.md)