Nanyx
PlaygroundPackages

env

A program's arguments, environment variables and exit code

Env is in the console host’s interface package, nanyx.console.core, and the console host’s prelude brings it into every module of a program, so it needs no import. The host provides Env to the program’s @entry and to a script’s statements, so args, variable and exit can be called there without any setup; any other function that calls them must declare [Env] in its type. nanyx run tool.nyx a b passes a and b as the arguments.

def name =
  match args()
    | [] -> "world"
    | [first, ..._] -> first

match variable("GREETING")
  | #some(greeting) -> println("{greeting}, {name}!")
  | #notSet -> println("Hello, {name}!")

if name == "nobody" then exit(1) else ()

Reference

What a program is started with, and how it ends: its command-line arguments, its environment variables and its exit code.

Env is a context the host provides: under the console host (nanyx.console), whose prelude brings it into every module of the program, a program calls args, variable and exit from its entry point, or from any function that declares [Env]. A library depends on nanyx.console.core and imports nanyx.console.core/env.

Env

context Env = (
  args: () -> list(string)
  variable: string -> #some(string) | #notSet
  exit: int -> undefined
)

The process the program runs as. The host supplies the instance: the console host’s reads what the program was started with.

  • args: The program’s command-line arguments, after its name: nanyx run tool.nyx a b gives ["a", "b"].
  • variable: The value of the environment variable name, or #notSet when it has none.
  • exit: Ends the program with the exit code code, without running the rest of it. It never returns, so its result is undefined and a call fits wherever a value of any type is expected.