SultanC
  1. Home
  2. Examples
  3. Environment

Reading environment variables and the working directory

env/environment.sn
// Reading environment variables and the working directory.
import std.io.println;
import std.env.environment;

def main(): int {
    let path_name: string = "PATH";
    match get_environment_variable(&path_name) with
    | result.ok(option.some(_)) -> { println("PATH is set"); }
    | result.ok(option.none) -> { println("PATH is not set"); }
    | result.err(_) -> { return 1; };
    let missing_name: string = "SULTANC_EXAMPLE_UNSET_VARIABLE";
    match get_environment_variable(&missing_name) with
    | result.ok(option.none) -> { println("unset variable reported as none"); }
    | _ -> { println("unexpected"); };
    match current_directory() with
    | result.ok(_) -> { println("working directory known"); }
    | result.err(_) -> { return 2; };
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/env/environment.sn

Or compile it to an executable:

./build/sultanc examples/en/env/environment.sn -o build/example && ./build/example

Source on GitHub   All examples