Source Edit

The std/envvars module implements environment variable handling.

Imports

oserrors, win_setenv, winlean

Types

  1. ReadEnvEffect = object of ReadIOEffect

Effect that denotes a read from an environment variable. Source Edit

  1. WriteEnvEffect = object of WriteIOEffect

Effect that denotes a write to an environment variable. Source Edit

Procs

  1. proc delEnv(key: string) {....tags: [WriteEnvEffect], raises: [OSError],
  2. forbids: [].}

Deletes the environment variable named key. If an error occurs, OSError is raised.

See also:ven

Source Edit

  1. proc existsEnv(key: string): bool {....tags: [ReadEnvEffect], raises: [],
  2. forbids: [].}

Checks whether the environment variable named key exists. Returns true if it exists, false otherwise.

See also:

Example:

  1. assert not existsEnv("unknownEnv")

Source Edit

  1. proc getEnv(key: string; default = ""): string {....tags: [ReadEnvEffect],
  2. raises: [], forbids: [].}

Returns the value of the environment variable named key.

If the variable does not exist, “” is returned. To distinguish whether a variable exists or it’s value is just “”, call existsEnv(key) proc.

See also:

Example:

  1. assert getEnv("unknownEnv") == ""
  2. assert getEnv("unknownEnv", "doesn't exist") == "doesn't exist"

Source Edit

  1. proc putEnv(key, val: string) {....tags: [WriteEnvEffect], raises: [OSError],
  2. forbids: [].}

Sets the value of the environment variable named key to val. If an error occurs, OSError is raised.

See also:

Source Edit

Iterators

  1. iterator envPairs(): tuple[key, value: string] {....tags: [ReadEnvEffect],
  2. raises: [], forbids: [].}

Iterate over all environments variables.

In the first component of the tuple is the name of the current variable stored, in the second its value.

Works in native backends, nodejs and vm, like the following APIs:

Source Edit