Coder Social home page Coder Social logo

brianpmaher / dotenv-cpp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from laserpants/dotenv-cpp

0.0 0.0 0.0 83 KB

:cow: A utility to load environment variables from a .env file

License: BSD 3-Clause "New" or "Revised" License

C++ 83.28% CMake 16.72%

dotenv-cpp's Introduction

dotenv-cpp

Installation

The library is header-only, so there is nothing to build. You could simply merge the include directory into your project root, entering something like

mkdir -p include
cp -rn <path-to-this-repo>/dotenv-cpp/include/ .

and compile, adding -I include/laserpants/dotenv to your build command, which then allows you to #include "dotenv.h".

Although the above method is sufficient for many use cases; for proper CMake support, you should install the library using a sequence of command such as

cd <path-to-this-repo>
mkdir -p build
cd build
cmake ..
make
sudo make install

By default, the headers will then be installed to include/laserpants/dotenv-<VERSION>/, relative to the CMake install prefix (/usr/local/ on Linux/Unix). To compile without CMake, you can then use a flag

-I /usr/local/include/laserpants/dotenv-<VERSION>

or the equivalent path on other platforms.

For CMake-based projects, your project's CMakeLists.txt file could look something like the following:

cmake_minimum_required(VERSION 3.2)
project(test)

find_package(laserpants_dotenv)

add_executable(example example.cpp)
target_link_libraries(example laserpants::dotenv)

Then, in example.cpp, just

#include <dotenv.h>

Usage

Example

Given a file .env

DATABASE_HOST=localhost
DATABASE_USERNAME=user
DATABASE_PASSWORD="antipasto"

and a program example.cpp

// example.cpp
#include <iostream>
#include <dotenv.h>

int main()
{
    dotenv::init();

    std::cout << std::getenv("DATABASE_USERNAME") << std::endl;
    std::cout << std::getenv("DATABASE_PASSWORD") << std::endl;

    return 0;
}

the output is:

user
antipasto

Default values

dotenv::getenv() is a wrapper for std::getenv() that also takes a default value, in case the variable is empty:

std::cout << dotenv::getenv("DATABASE_USERNAME", "anonymous") << std::endl;

Referencing other variables

Other variables can be referenced using either ${VARIABLE} or $VARIABLE.

Example:

Given this program

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

int main(int argc, char **argv)
{
   dotenv::init("resolve.env");
   std::cout << std::getenv("MESSAGE") << std::endl;
   return 0;
}

and this resolve.env

TEMPERATURE = cold
EXTENT      =    "  long  "
SEASON      = '$EXTENT $TEMPERATURE winter'
MORE        =    and $TEMPERATURE ice
CONTAINS    = lots of ${TEMPERATURE} snow $MORE
MESSAGE     =    I wish you a ${SEASON}, with $CONTAINS

the output becomes

I wish you a   long   cold winter, with lots of cold snow and cold ice

Options

By default, if a name is already present in the environment, dotenv::init() will replace it with the new value. To preserve existing variables, you must pass the Preserve flag.

dotenv::init(dotenv::Preserve);

std::cout << std::getenv("DATABASE_USERNAME") << std::endl;
std::cout << std::getenv("DATABASE_PASSWORD") << std::endl;

Updating the previous example, recompiling and running the program again, after setting the DATABASE_USERNAME variable, e.g. as

DATABASE_USERNAME=root ./example

the output this time is:

root
antipasto

Changelog

0.9.3

Added

  • Add cctype header needed by std::isspace

0.9.2

Added

  • Add support for referencing other variables in variable definitions

0.9.1

Added

  • Add wrapper for setenv to support MSVC

0.9.0

  • Initial version

dotenv-cpp's People

Contributors

laserpants avatar arnholm avatar nagulv avatar brianpmaher avatar ken-matsui avatar ssivanov19 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.