LEVIATHAN v962456e · 962456eee1

Standard Library

interface IEnv

What a program may learn from the process environment: its arguments and its variables.

since 0.1.0-alpha.1linuxwindowswasm

Overview

A function that takes an IEnv parameter does not depend on the real process, so a test can pass an implementation that returns fixed values. use std::IEnv; makes the real environment (SystemEnv) the default for such parameters; a bind supplies another.

Examples

class ScriptedEnv : IEnv {
    Array<string> canned;
    new ScriptedEnv(Array<string> a) { canned = a; }
    Array<string> args() => canned;
    string? variable(string name) => name == "MODE" ? "test" : None;
}

void describe(IEnv env) {
    console.writeln("first argument: ${env.args().at(0)}");
    console.writeln(env.variable("MODE") == None ? "no mode" : "mode is set");
    console.writeln(env.variable("OTHER") == None);
}

bind IEnv => ScriptedEnv(["prog", "-v"]);
describe();
first argument: prog
mode is set
true

Methods

args

args() -> Array<string>

The program's arguments, with the program name first.

Returns

The program name followed by its arguments.

Examples

class FixedArgs : IEnv {
    Array<string> args() => ["tool", "--fast"];
    string? variable(string name) => None;
}
IEnv e = FixedArgs();
console.writeln(e.args().length());
console.writeln(e.args().at(1));
2
--fast

variable

variable(string name) -> string | None

Read an environment variable.

Parameters

name
The name of the variable.

Returns

The value, or None when the variable is not set. A variable set to the empty string is not None.

Examples

class FixedVars : IEnv {
    Array<string> args() => ["tool"];
    string? variable(string name) => name == "MODE" ? "test" : None;
}
IEnv e = FixedVars();
console.writeln(e.variable("MODE"));
console.writeln(e.variable("OTHER") == None);
test
true

See also

  • SystemEnv — The real process environment behind the IEnv interface.
  • env — The running process: its command-line arguments, its environment variables, and how it exits.