Skip to content

Process

Spice
import "std/os/process";

ProcessResult struct

Result of a process, that was run via runProcessCaptured()

Fields

Name Type Description
exitCode int
output String Captured stdout (and stderr, if requested) of the process

Functions

runProcess

Spice
public f<Result<int>> runProcess(const String& program, const Vector<String>& args)

Run a program and wait for it to terminate. Its standard streams are connected to the ones of the calling process.

No shell is involved: the arguments are passed verbatim to the program, so they are never interpreted as shell syntax (no variable expansion, globbing, redirections, command separators, ...). Use this instead of execCmd() for any command, whose arguments are influenced by (untrusted) input.

Parameters

Name Type Description
program const String& Name or path of the program. A bare name is searched for on the PATH
args const Vector<String>& Arguments for the program (without the program itself)

Returns: Result<int> — Exit code of the program, or an error if it could not be found or started

runProcessCaptured

Spice
public f<Result<ProcessResult>> runProcessCaptured(const String& program, const Vector<String>& args, bool includeStdErr = false)

Run a program, wait for it to terminate and capture its standard output. Like runProcess(), no shell is involved.

Parameters

Name Type Description
program const String& Name or path of the program. A bare name is searched for on the PATH
args const Vector<String>& Arguments for the program (without the program itself)
includeStdErr bool Also capture the standard error stream of the program, interleaved with its standard output (default: false)

Returns: Result<ProcessResult> — Exit code and captured output of the program, or an error if it could not be found or started

findProgram

Spice
public f<String> findProgram(const String& name)

Search for a program on the PATH, like a shell would do it. On Windows, the '.exe' extension is tried as well.

Parameters

Name Type Description
name const String& Name of the program. A name with a path separator is not searched for, but only checked for existence

Returns: String — Path to the program, or an empty string if it could not be found