Skip to content

env

The env package reads and writes environment variables and gives access to the arguments a script was started with.

env::set("APP_MODE", "test")
println env::get("APP_MODE") # test
println env::get("NOT_SET").empty() # true: missing variables read as ""
println typeof(env::all()) # hashmap

env::set is unavailable in safe mode. To load variables from a .env file, see dotenv.

FunctionReturns
env::args()The raw arguments, in order.
env::argv()The arguments as a hashmap: -key=value becomes key: "value", --flag becomes flag: "true", and other arguments map to themselves.
env::opt(key)The value of a -key=value argument.
env::parse_args(args, value_options)A hashmap of flags, where options listed in value_options take the following argument as their value.
config = env::parse_args(["--mode", "fast", "-v"], ["--mode"])
println config # {"--mode": "fast", "-v": true}

See Passing arguments to scripts for examples, and cli::CliParser for a fuller argument parser.

env::parse_args(args: list = [], value_opts: list = []): hashmap

Parses command-line arguments into a key-value hashmap.

ParameterTypeDefaultDescription
argslist[]The list of argument strings to parse (default: the raw args() list).
value_optslist[]Options that expect a following value argument (default []).

Returns hashmap — A hashmap where flag options map to true and value options map to their argument.

Throws string — If a value option is found with no following argument.

env::argv(): hashmap

Get the hashmap of command-line arguments.

Returns hashmap — A hashmap of command-line arguments.

env::args(): list

Get the raw, ordered list of command-line arguments, exactly as passed (duplicates preserved).

Returns list — The raw command-line arguments, in order.

env::opt(_key: string): string

Get a named option value by key.

ParameterTypeDefaultDescription
_keystringThe option key.

Returns string — The option value.

env::all(): hashmap

Get all environment variables as a hashmap.

Returns hashmap — A hashmap of all environment variables.

env::get(_var_name: string): string

Get an environment variable.

ParameterTypeDefaultDescription
_var_namestringThe environment variable name.

Returns string — The environment variable value, or an empty string if not found.

env::set(_var_name: string, _var_value: string): boolean

Set an environment variable.

ParameterTypeDefaultDescription
_var_namestringThe environment variable name.
_var_valuestringThe environment variable value.

Returns boolean — true on success, false on failure.

env::bin(): string

Get the path to the Kiwi binary.

Returns string — The path to the Kiwi binary.

env::lib(): string

Get the path to the Kiwi Standard Library.

Returns string — The path to the Kiwi Standard Library directory.