Pelago
stdSystem

std.os

The process: arguments and exit status.

import std.os

This module is small on purpose for now. std.os has one function, os.exit. The arguments come from the main parameter, not from a function. These are not available yet:

  • os.args() as a function, the process id, the current directory and changing it, the home and temporary directories, the executable path and the host name.
  • Platform constants such as the operating system, the architecture, the path separator and the line ending.
  • Signals, abort, exit-code constants and a typed OS error.
  • Running other programs (std.process: spawn, pipes, wait, kill).

Environment variables are in std.env and are a read-only snapshot taken at start-up.

ItemNotes
main(args: [string])main may declare a list of strings as its parameter. The first element is the program name. Works in the interpreter and on Linux, Windows and macOS builds.
os.exit(code)End the program at once with this status, 0 to 255. Standard output is flushed, but deinit does not run. Any other value stops the program with the message os.exit: the status must be 0 to 255, because Linux and macOS would keep only the low byte (os.exit(256) would exit 0).
import std.os

fn main(args: [string]) {
    if args.len() < 2 {
        print("usage: greet <name>")
        os.exit(2)
    }
    print("hello, #(args[1])")
}
usage: greet <name>

os.exit has the type never, so the compiler knows the code after it is not reached. Use the exit status to tell scripts whether your tool succeeded: 0 for success and non-zero for failure.