Skip to content

std.process

Process arguments and explicit exit.

std.process exposes checked process-argument helpers and an explicit exit primitive.

export def process_arg_count() -> usize
export enum ProcessArgError:
case OutOfRange
case InvalidUtf8
export def process_arg(index: usize) -> Result[String, ProcessArgError]
export def process_arg_path(index: usize) -> Result[PathBuf, ProcessArgError]
  • process_arg_count() is the count of program-level arguments (not counting argv[0]).
  • process_arg(0) returns the first user argument as an owned String.
  • process_arg_path(0) returns it as an owned PathBuf, appropriate when the argument is a filesystem path and may legally contain non-UTF-8 bytes.

ProcessArgError::OutOfRange is returned when index >= process_arg_count(). ProcessArgError::InvalidUtf8 is returned from process_arg when the underlying bytes are not valid UTF-8; process_arg_path is byte-shaped and does not surface this error.

export def process_exit(code: i32) -> None

Equivalent to libc exit(code). Use this for early exits from a program whose main returns None; for programs whose main returns i32, just return the code.

import std.io as io
import std.process as process
def main() -> i32:
let arg_result: Result[String, ProcessArgError] = process.process_arg(0)
match arg_result:
case Ok(name):
io.stdout_write_str("hello, ")
io.stdout_write_str(name.as_str())
io.stdout_write_str("\n")
return 0
case Err:
io.stderr_write_str("usage: greet NAME\n")
return 1