laserpants / dotenv-cpp

:cow: A utility to load environment variables from a .env file
BSD 3-Clause "New" or "Revised" License
30 stars 12 forks source link

Add support for recursive ${VARIABLE} resolution #2

Closed arnholm closed 2 years ago

arnholm commented 2 years ago

This pull request adds support for recursive variable definitions, where variables can be referred to as ${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 and $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 and cold ice