Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

envy: Deserialize environment variables into type-safe structs

License

MIT and 2 other licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.json
BSL-1.0
LICENSE.visit_struct
NotificationsYou must be signed in to change notification settings

p-ranav/envy

standardlicenseversion

envy is a small header-only library to deserialize environment variables into type-safe structs.

Quick Start

Start by creating astruct where to store the values of environment variables.

#include<envy/envy.hpp>structServerConfig {int  server_alive_interval =45;bool compression           =false;int  compression_level     =0;bool forward_x11           =true;};ENVY_STRUCT(ServerConfig, server_alive_interval, compression, compression_level, forward_x11);

For each field in the struct,envy will look for an environment variable with the same name in upper case, e.g., for the field namedforward_x11,envy will look for an environment variable namedFORWARD_X11.

Useenvy::get<T>() to get a deserialized, type-safe struct.

intmain() {auto config = envy::get<ServerConfig>();    std::cout <<"Server Alive Interval :" << config.server_alive_interval <<"\n";  std::cout <<"Compression Enabled?  :" << std::boolalpha << config.compression <<"\n";  std::cout <<"Compression Level     :" << config.compression_level <<"\n";  std::cout <<"Forward X11?          :" << std::boolalpha << config.forward_x11 <<"\n"; }

Here's the stdout of the above program:

▶ ./mainServer Alive Interval: 45Compression Enabled?:falseCompression Level: 0Forward X11?:true▶ SERVER_ALIVE_INTERVAL=90 COMPRESSION=1 COMPRESSION_LEVEL=9 FORWARD_X11=0 ./mainServer Alive Interval: 90Compression Enabled?:trueCompression Level: 9Forward X11?:false

Deserialize comma-separated values

Comma-separated values can be deserialized into anstd::vector:

#include<envy/envy.hpp>structConfig {  std::vector<int> values;};ENVY_STRUCT(Config, values);intmain() {auto config = envy::get<Config>();for (auto& v : config.values) {    std::cout << v <<"";  }}
▶ ./main▶ VALUES=1,2,3,4,5 ./main1 2 3 4 5

Deserialize JSON

JSON can be deserialized intostd::map,std::unordered_map or evennlohmann::json:

#include<envy/envy.hpp>structConfig {  std::map<std::string,int> values;};ENVY_STRUCT(Config, values);intmain() {auto config = envy::get<Config>();for (auto& kv : config.values) {    std::cout << kv.first <<" :" << kv.second <<"\n";  }}
▶ ./main▶ VALUES='{"a": 1, "b": 2, "c": 3}' ./maina: 1b: 2c: 3

Application Prefix for Environment Variables

#include<envy/envy.hpp>structConfig {int   foo =0;float bar =0.0f;bool  baz =false;};ENVY_STRUCT(Config, foo, bar, baz);intmain() {auto config = envy::get<Config>("APP_");// prefix for env vars  std::cout <<"foo :" << config.foo <<"\n";  std::cout <<"bar :" << config.bar <<"\n";  std::cout <<"baz :" << std::boolalpha << config.baz <<"\n";}
▶ ./mainfoo: 0bar: 0baz:false▶ APP_FOO=1 APP_BAR=3.14 APP_BAZ=1 ./mainfoo: 1bar: 3.14baz:true

Generating Single Header

python3 utils/amalgamate/amalgamate.py -c single_include.json -s.

Contributing

Contributions are welcome, have a look at theCONTRIBUTING.md document for more information.

License

The project is available under theMIT license.


[8]ページ先頭

©2009-2025 Movatter.jp