Skip to content
forked from muxelplexer/dotenv

A simple C++ library to read .env files into the local environment.

License

Notifications You must be signed in to change notification settings

oxin-ros/dotenv

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dotenv - A simple c++ dotenv & environment variable handler

The goal of this library is to provide a easy way to load a .env file from some directory.

Requirements

  • C++20 supporting Compiler (gcc 12.2.0+)
  • CMake 3.24+
  • Doxygen (if building the documentation is desired)

Example Usage

#include <iostream>
#include "dotenv/dotenv.hpp"

int main()
{
    // dotenv::load takes the path to the directory
    // where the .env file is located
    dotenv::load("../");

    // Gets all Variables succesfully parsed from the .env file
    auto dot_env_vars = dotenv::get_variables();
    for (auto env_var : dot_env_vars)
    {
        // dotenv::getenv returns a std::optional object.
        // Since we know that these variables got set it's safe to call .value() on it
        std::cout << env_var << "=" << dotenv::get_env(std::string(env_var)).value() << "\n";
    }

    // dotenv::get_env can also be used to retrieve general environment variables.
    auto home_var = dotenv::get_env("HOME");
    if (home_var != std::nullopt)
    {
        std::cout << home_var.value() << "\n";
    }

    return 0;
}

About

A simple C++ library to read .env files into the local environment.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 82.0%
  • CMake 18.0%