Command
import "std/sys" (included in import "std") · namespace std.sys
Command runs another program directly, without a shell. Arguments are passed exactly as given, so spaces and special characters in them are safe and never re-interpreted. It captures standard output and standard error separately, can feed standard input, set the working directory and environment, and enforce a timeout.
import "std"
fn main() {
let r = new Command("sh").arg("-c").arg("echo out; echo err >&2; exit 3").run()
print("code ${r.code}, stdout [${Str.trim(r.stdout)}], stderr [${Str.trim(r.stderr)}]")
print(r.ok())
print(new Command("tr").arg("a-z").arg("A-Z").stdin("shout\n").output())
let slow = new Command("sleep").arg("10").timeoutMs(50).run()
print("timed out: ${slow.timedOut}")
try {
new Command("no-such-program").run()
} catch (e: IOError) {
print(e.message())
}
}code 3, stdout [out], stderr [err]
false
SHOUT
timed out: true
cannot run 'no-such-program': No such file or directoryrun()waits and returns aCommandResultwithcode,stdout,stderr,timedOutandok()(exit code 0, no timeout).output()returns standard output and throwsIOErrorwhen the program fails;status()returns just the exit code.spawn()starts the program and returns aChildat once:pid(),isRunning(),kill(),signal(n), andwait()for the result.inheritOutput()lets the program write straight to your terminal.- A program that cannot be started throws
IOError.
To run a shell command line (with pipes, globs and redirections), use Process or new Command("sh").arg("-c").arg(line), and never build the line from untrusted input.
API
class CommandResult
What a finished command produced.
Constructors
new CommandResult(code: int, stdout: string, stderr: string, timedOut: bool)
Fields
readonly code: int— The exit status (128 + signal number when killed by a signal).readonly stdout: stringreadonly stderr: stringreadonly timedOut: bool— True when the command was killed for running past its timeout.
Methods
fn ok() -> bool— Whether the program exited with status 0 (and did not time out).fn toString() -> string— "CommandResult(code n)", with ", timed out" when it did.
class Child
A running program started by Command.spawn().
Constructors
new Child(h: byte*, input: string, timeout: int)
Methods
fn pid() -> int— The child process's id.fn isRunning() -> bool— Whether the program is still running.fn kill()— Send a signal (default SIGTERM = 15; 9 is SIGKILL).fn signal(sig: int)— Sends a signal (e.g. 15 for SIGTERM) to the child.fn wait() -> CommandResult— Wait for the program to exit, collecting what it printed (when captured).
class Command
Builds a program invocation: new Command(program).arg(...).cwd(...).run().
Constructors
new Command(program: string)
Methods
fn arg(a: string) -> Command— Adds one argument; returns this command.fn args(list: Array<string>) -> Command— Adds several arguments; returns this command.fn cwd(dir: string) -> Command— Run in this working directory.fn env(key: string, value: string) -> Command— Set an environment variable for the program (on top of the inherited ones).fn clearEnv() -> Command— Start from an empty environment (variables set with env() still apply).fn stdin(text: string) -> Command— Text written to the program's standard input (then closed).fn timeoutMs(ms: int) -> Command— Kill the program if it runs longer thanmsmilliseconds.fn inheritOutput() -> Command— Let the program print straight to this terminal instead of capturing output.fn describe() -> string— "program arg1 arg2", for logs and error messages.fn spawn() -> Child— Start the program without waiting. Throws IOError if it cannot be started (for example, the program does not exist).fn run() -> CommandResult— Run to completion and collect the exit code and output.fn output() -> string— Run and return stdout; throws IOError when the program fails.fn status() -> int— Run and return only the exit code.