Code Monkey home page Code Monkey logo

cpp-dotenv's Introduction

cpp-dotenv

version 0.2.0

C++ implementation of NodeJS dotenv project. Loads environment variables from .env for C++ projects.

Please take into account this is still a developing project.

cpp-dotenv is implemented as a single C++ header file, so there is no need to compile nor to add complex file dependencies to your project. Simply include the header file wherever you want to use it and ta-da!, you're done.

Table of contents

  1. Dependencies
  2. Usage
    1. CMake
  3. Examples
    1. Basic usage
    2. Reference renaming
    3. Several dotenv files
  4. Grammar

Dependencies

NONE, for sure! ๐Ÿ˜Ž If it had, it wouldn't follow the basic dotenv principles.

Usage

To be able to use the dotenv classes, simply include the header file:

#include "dotenv.h"

For the sake of simplycity (and if your project namespace density allows to), you can also use the dotenv namespace under which all definitions are:

using namespace dotenv;

For convenience, cpp-dotenv auto-configures a class object (which is instance of the singleton class dotenv) by calling the load_dotenv() method at the very beginning of your file (just right before the end of dotenv.h) and trying to load a .env file, although if you need to add-in your own files (like .myenv), simply re-run the loading step passing the file name as parameter; everything new will show up on the dotenv instances.

By default, already-defined environment variables are not overwritten even if redefined in some of the loaded files. This behavior can be changed, however, by calling the load_config() function with the overwrite parameter set to true. For an example, take a look at this one.

Also for convenience, there is a namespace-global pre-loaded reference variable to the dotenv singleton class instance named env. Simply use it as you would use a dotenv object on NodeJS, or you can define your own references:

auto& dotenv = env; // 'auto' here is 'dotenv::dotenv'

CMake

cpp-dotenv also comes with support for CMake right out of the box. In order to use it, simply include this repository's directory and link the CPP_DOTENV_LIB library to your own targets where needed:

add_subdirectory(cpp-dotenv)
target_link_libraries(YOUR_TARGET ${CPP_DOTENV_LIB})

After this, you might use the library as described in usage; no extra scoping, no need to worry about the project's directory structure.

Examples

Basic usage

Assume the following .env file:

# DB THINGS
DB_NAME=DontDoThisAtHome
DB_PASS=such_security

# CONNECTIONS THINGS
COMMAND=ping
HOST=8.8.8.8
MESSAGE="Hey buddy!"

The following .cpp file:

#include "dotenv.h"
#include <iostream>

using namespace dotenv;
using namespace std;

int main()
{
    cout << "DB_NAME: " << env["DB_NAME"] << endl;
    cout << "eval \"" << env["COMMAND"] << " " << env["HOST"] << "\"" << endl;
}

would produce the following output:

$ ./main
  DB_NAME: DontDoThisAtHome
  eval "ping 8.8.8.8"

Reference renaming

Assuming the same .env file as in the previous case, the predefined env reference can be easily renamed and used just exactly as the original one.

The following code:

#include "dotenv.h"
#include <iostream>

using namespace std;

int main()
{
    auto& dotenv = dotenv::env;
    cout << "DB_NAME: " << dotenv["DB_NAME"] << endl;
    cout << "eval \"" << dotenv["COMMAND"] << " " << dotenv["HOST"] << "\"" << endl;
}

would produce the following output:

$ ./main
  DB_NAME: DontDoThisAtHome
  eval "ping 8.8.8.8"

Several dotenv files

The situation of having several different dotenv files is no stranger one (.env for private configuration variables, .pubenv for public variables, etc.). Loading several files in addition to the default one and overwritting any variables that are redefined on the files can be done as follows:

Assume the following .env file:

# DB THINGS
DB_NAME=DontDoThisAtHome
DB_PASS=such_security

And the following .pubenv file:

# CONNECTIONS THINGS
COMMAND=ping
HOST=8.8.8.8
MESSAGE="Hey buddy!"

The following source file:

#include "dotenv.h"
#include <iostream>

using namespace dotenv;
using namespace std;

int main()
{
    env.load_dotenv(".env", true);
    env.load_dotenv(".pubenv", true);
    cout << "DB_NAME: " << env["DB_NAME"] << endl;
    cout << "eval \"" << env["COMMAND"] << " " << env["HOST"] << "\"" << endl;
}

would produce the following output:

$ ./main
  DB_NAME: DontDoThisAtHome
  eval "ping 8.8.8.8"

Grammar

For the geeks, you can check the grammar I've implemented on the grammar/env.g4 file. Despite being written in an ANTLR4 fashion, I've implemented a simple recursive parser myself given the basic nature of the language. The parser and its methods are publicly available under the dotenv::parser class.

Known issues

The complete list of issues can be consulted at the issues page.

  1. Variable resolution on values not yet vailable

cpp-dotenv's People

Contributors

adeharo9 avatar apotema avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.