Environment#

Environment variable access and manipulation functions and facilities.

Info

To use declarations from this page, include the <essence/environment.hpp> header.

std::string env::get(const char *key, const char *fallback = "")#

Get the value of an environment variable by name, with a fallback value when the variable is not set.

bool env::set(const char *key, const char *value, bool clobber = false)#

Set the environment variable key to value. If clobber is true, overwrite an existing value.

bool env::unset(const char *key)#

Unset an environment variable by name. Returns true upon success, false otherwise.

std::vector<std::string> env::vec(char *const *envs = environ)#

Construct a std::vector from the a null-terminated environment array. Each member of the result is the entire pair, including the =.

std::unordered_map<std::string, std::string> env::map(char *const *envs = environ)#

Construct a std::unordered_map from the null-terminated environment array.

detail::environment_t environment#

Global proxy object instance for convenient but safe environment access and manipulation. Provides null-safe access to the POSIX getenv, setenv, unsetenv family of functions.

detail::environment_t is an implementation detail, all interaction should be go through this object.

Usage

using namespace essence;
std::string path = environment["PATH"];

std::string shell = environment.get("SHELL", "/bin/sh");

environment.set("SOME_THING", "foo");
environment.set("SOME_THING", "bar", /*clobber=*/ true);

environment.unset("SOME_THING");
class detail::environment_t#
std::string operator[](const char *key)#

Get the value of key in the environment, or an empty string if unset.

std::string get(const char *key, const char *fallback = "")#

Get the value of key in the environment, or the value of fallback if unset.

void set(const char *key, const char *value, bool clobber = false)#

Set the environment variable key to value, overwriting an existing value if clobber is true.

void unset(const char *key)#

Unset the environment variable key.