env
The env package reads and writes environment variables and gives access to the arguments a script was started with.
Environment variables
Section titled “Environment variables”env::set("APP_MODE", "test")println env::get("APP_MODE") # testprintln env::get("NOT_SET").empty() # true: missing variables read as ""println typeof(env::all()) # hashmapenv::set is unavailable in safe mode. To load variables from a .env file, see dotenv.
Command-line arguments
Section titled “Command-line arguments”| Function | Returns |
|---|---|
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.
Functions
Section titled “Functions”env::parse_args
Section titled “env::parse_args”env::parse_args(args: list = [], value_opts: list = []): hashmapParses command-line arguments into a key-value hashmap.
| Parameter | Type | Default | Description |
|---|---|---|---|
args | list | [] | The list of argument strings to parse (default: the raw args() list). |
value_opts | list | [] | 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
Section titled “env::argv”env::argv(): hashmapGet the hashmap of command-line arguments.
Returns hashmap — A hashmap of command-line arguments.
env::args
Section titled “env::args”env::args(): listGet the raw, ordered list of command-line arguments, exactly as passed (duplicates preserved).
Returns list — The raw command-line arguments, in order.
env::opt
Section titled “env::opt”env::opt(_key: string): stringGet a named option value by key.
| Parameter | Type | Default | Description |
|---|---|---|---|
_key | string | The option key. |
Returns string — The option value.
env::all
Section titled “env::all”env::all(): hashmapGet all environment variables as a hashmap.
Returns hashmap — A hashmap of all environment variables.
env::get
Section titled “env::get”env::get(_var_name: string): stringGet an environment variable.
| Parameter | Type | Default | Description |
|---|---|---|---|
_var_name | string | The environment variable name. |
Returns string — The environment variable value, or an empty string if not found.
env::set
Section titled “env::set”env::set(_var_name: string, _var_value: string): booleanSet an environment variable.
| Parameter | Type | Default | Description |
|---|---|---|---|
_var_name | string | The environment variable name. | |
_var_value | string | The environment variable value. |
Returns boolean — true on success, false on failure.
env::bin
Section titled “env::bin”env::bin(): stringGet the path to the Kiwi binary.
Returns string — The path to the Kiwi binary.
env::lib
Section titled “env::lib”env::lib(): stringGet the path to the Kiwi Standard Library.
Returns string — The path to the Kiwi Standard Library directory.