Module process/command

process/command

High-level child-process spawning with builder-style configuration.

Wraps std/sys/process with a fluent API for constructing argv, capturing stdout/stderr through pipes, and waiting for the child to exit.

Example

{ Command } :: import "std/process/command";
{ Exception, AnyError } :: import "std/error";

main :: (fn(io : Io, exn : Exception) -> unit)({
  cmd := Command.new(`echo`);
  cmd.arg(`hello`);
  cmd.arg(`world`);
  out := io.await(cmd.output(io), { io, exn });
  assert(out.status.success(), "echo should succeed");
});

Types

Command object
Command

Builder for a child-process invocation.

Construct with Command.new(program), then chain mutating builder methods (arg, args, env_clear) before invoking status() or output().

Fields

NameTypeDescription
_programString
_argsArrayList(String)
_stdin_fdi32
_stdout_fdi32
_stderr_fdi32
impl(Command, ...)
new : (Command) fn(program : String) -> Command

Create a new Command invoking program. The program name is also used as argv[0] unless a different first arg is pushed manually before invocation.

Parameters

NameTypeNotes
programString

Returns: Command

arg : (Command) fn(self : Command, a : String) -> unit

Append a single argument to the argv list.

Parameters

NameTypeNotes
selfCommand
aString

Returns: unit

args : (Command) fn(self : Command, more : ArrayList(String)) -> unit

Append multiple arguments to the argv list.

Parameters

NameTypeNotes
selfCommand
moreArrayList(String)

Returns: unit

impl(Command, ...)
status : (Command) fn(self : Command, io : Io) -> Impl(Future(ExitStatus, IoExn))

Spawn the child with stdio inherited from the parent, wait for it to exit, and return its ExitStatus.

Parameters

NameTypeNotes
selfCommand
ioIo

Returns: Impl(Future(ExitStatus, IoExn))

output : (Command) fn(self : Command, io : Io) -> Impl(Future(Output, IoExn))

Spawn the child with stdout and stderr captured through pipes. Waits for the child to exit and returns the exit status plus the captured bytes.

Parameters

NameTypeNotes
selfCommand
ioIo

Returns: Impl(Future(Output, IoExn))

ExitStatus struct
ExitStatus

The result of a finished child process.

Holds the raw waitpid status code along with helpers to extract the exit code and termination signal.

Fields

NameTypeDescription
rawi32

Encoded waitpid status. Use code() / signal() helpers instead of reading this directly when possible.

impl(ExitStatus, ...)
code : (ExitStatus) fn(self : ExitStatus) -> i32

Returns the process exit code (0..255 on Unix). Returns 0 when the process was terminated by a signal.

Parameters

NameTypeNotes
selfExitStatus

Returns: i32

signal : (ExitStatus) fn(self : ExitStatus) -> i32

Returns the signal number that terminated the process, or 0 if the process exited normally.

Parameters

NameTypeNotes
selfExitStatus

Returns: i32

success : (ExitStatus) fn(self : ExitStatus) -> bool

Returns true if the child exited with status code 0.

Parameters

NameTypeNotes
selfExitStatus

Returns: bool

Output object
Output

Captured output of a child process.

Returned by Command.output. Holds the exit status plus the bytes captured from the child's stdout and stderr.

Fields

NameTypeDescription
statusExitStatus

Spawn the child with stdio inherited from the parent, wait for it to exit, and return its ExitStatus.

stdoutArrayList(u8)
stderrArrayList(u8)