Skip to content
CryoCryo home
Stdlibprocess

command

import std::process::command; · source

Types

Command

type struct Command {
    program:        String;
    args:           Array<String>;
    envs:           Array<EnvEntry>;
    clear_env_flag: boolean;
    cwd:            Option<String>;
    stdin_cfg:      Stdio;
    stdout_cfg:     Stdio;
    stderr_cfg:     Stdio;

    static new(program: Str) -> Command;
    arg(mut &this, value: Str) -> void;
    env(mut &this, key: Str, value: Str) -> void;
    env_clear(mut &this) -> void;
    cwd(mut &this, path: Str) -> void;
    stdin(mut &this, setting: Stdio) -> void;
    stdout(mut &this, setting: Stdio) -> void;
    stderr(mut &this, setting: Stdio) -> void;
    spawn(&this) -> Result<Child, IoError>;
    output(&this) -> Result<Output, IoError>;
    status(&this) -> Result<ExitStatus, IoError>;
    async collect(&this) -> Result<Output, IoError>;
    async run(&this) -> Result<ExitStatus, IoError>;
    static shell(command: string) -> i32;
    static shell(command: string) -> i32;
}

The builder. Configure it, then finish with spawn, output, or status — or their async spellings, collect and run. For one command line through the platform shell, see exec.

import std::process::command;

mut cmd: Command = Command::new(Str::new("git"));
cmd.arg(Str::new("rev-parse"));
cmd.arg(Str::new("HEAD"));
cmd.stdout(Stdio::Piped);

match (cmd.output()) {
    Result::Ok(out)  => { /* out.stdout() is a Slice<u8> */ }
    Result::Err(e)   => { /* IoError */ }
}

new(program) resolves the program against $PATH at spawn time; pass an absolute path to skip the lookup. arg, env, and cwd build up the argv, environment, and working directory; env_clear stops the parent's environment from being inherited, so only env() entries reach the child. stdin, stdout, and stderr each take a Stdio setting.

Properties

PropertyType
programString
argsArray<String>
envsArray<EnvEntry>
clear_env_flagboolean
cwdOption<String>
stdin_cfgStdio
stdout_cfgStdio
stderr_cfgStdio

Constructors

static new(program: Str) -> Command;

Start a new command targeting program. The program name is resolved against $PATH by execvp at spawn time; pass an absolute path to skip the lookup.

Static functions

static shell(command: string) -> i32;

Run command through the platform shell (/bin/sh -c on POSIX, cmd /c on Windows), inheriting stdio, and return the child's exit code (0 on success), or -1 if the shell could not be spawned or the child was killed by a signal. The cross-platform replacement for the raw system(3) intrinsic when a command genuinely needs shell semantics (pipes, redirects, glob expansion). For a plain program invocation, build the argv with new/arg and call status instead.

static shell(command: string) -> i32;

Methods

arg(mut &this, value: Str) -> void;
env(mut &this, key: Str, value: Str) -> void;
env_clear(mut &this) -> void;

Don't inherit the parent's environment. Only the entries added with env() reach the child.

cwd(mut &this, path: Str) -> void;
stdin(mut &this, setting: Stdio) -> void;
stdout(mut &this, setting: Stdio) -> void;
stderr(mut &this, setting: Stdio) -> void;
spawn(&this) -> Result<Child, IoError>;

fork + configure fds/env + execvp. Returns a Child the caller must eventually wait on.

output(&this) -> Result<Output, IoError>;

spawn + wait + collect stdout/stderr into arrays. Stdin is left Inherit (or whatever the caller set). Use this for the common "run this and tell me what it printed" case.

status(&this) -> Result<ExitStatus, IoError>;

spawn + wait. Stdin/out/err go wherever the caller configured (typically Inherit).

async collect(&this) -> Result<Output, IoError>;

Spawn, capture both streams, and await the exit without blocking the calling thread: the async spelling of output.

async run(&this) -> Result<ExitStatus, IoError>;

Spawn and await the exit without capturing anything: the async spelling of status. Stdio goes wherever the caller configured it.

Finishing

spawn forks, configures, and execs, handing back a Child you must eventually wait on. output spawns, waits, and collects stdout and stderr — the "run this and tell me what it printed" case — while status spawns and waits with the streams going wherever you configured them. collect and run are the same two without blocking the executor: the drains and the exit wait sit on the reactor (Linux) or the blocking pool (Windows). Use collect when anything is Piped, or the child can stall on a full pipe.

Output

type struct Output {
    status: ExitStatus;
    stdout: Array<u8>;
    stderr: Array<u8>;

    status(&this) -> ExitStatus;
    stdout(&this) -> Slice<u8>;
    stderr(&this) -> Slice<u8>;
}

Output exposes status(), stdout(), and stderr(). Both slices borrow the Output and are valid only until it drops — copy out anything you need to keep.

output and collect drain the captured pipes before waiting, which is what avoids the classic deadlock of a child blocked on a full pipe while the parent is blocked in wait. On Windows a captured child occupies two blocking-pool threads while draining, so a program running N children concurrently should size the pool for 2N there.

Properties
PropertyType
statusExitStatus
stdoutArray<u8>
stderrArray<u8>
Methods
status(&this) -> ExitStatus;

The child's decoded exit status.

stdout(&this) -> Slice<u8>;

Captured stdout as a borrowed byte slice. Valid until this Output is dropped; copy out anything you need to keep.

stderr(&this) -> Slice<u8>;

Captured stderr as a borrowed byte slice. Valid until this Output is dropped.

Stdio

type enum Stdio {
    Inherit;
    Null;
    Piped;
    Fd(i32);
}

Inherit shares the parent's fd and is the default; Null redirects to or from /dev/null; Piped creates a pipe whose parent end lands in Child::stdin / stdout / stderr; Fd(i32) uses a specific descriptor, and ownership transfers — the spawn dups it into the child and closes it in the parent.

If execvp fails in the child, it exits with 127 — the standard "command not found" status — so the parent sees that rather than a hang.

Variants
VariantNotes
InheritShare the parent's fd (the default).
NullRedirect to / from /dev/null.
PipedCreate a pipe; the parent keeps its end in the resulting Child::stdin / stdout / stderr.
Fd(i32)Use a specific fd. Ownership transfers; the spawn closes the fd in the parent after duping it into the child.

Trait implementations

Command
implement trait Drop for struct Command
Output
implement trait Drop for struct Output
EnvEntry
implement trait Drop for struct EnvEntry