Search results

There are no results.

std.sys.Command

class pub Command

A builder type for a ChildProcess.

This type is used to define arguments, environment variables, what to do with the standard input/output streams, etc.

Once a command is prepared, you can spawn it using Command.spawn, returning a ChildProcess.

Redirecting standard input/output streams

By default STDIN, STDOUT, and STDERR are inherited. These streams can be changed to being redirected to the null device, or to be piped to a buffer. For example, to pipe STDOUT:

import std.sys (Command, Stream)

let cmd = Command.new('ls')

cmd.stdout = Stream.Piped
cmd.spawn.get

We can also ignore a stream:

import std.sys (Command, Stream)

let cmd = Command.new('ls')

cmd.stderr = Stream.Null
cmd.spawn.get

Waiting for the child process

The method Command.spawn returns a ChildProcess. This object is used for reading/writing the standard input/output streams, and to wait for the process to terminate. Waiting for a process is done using ChildProcess.wait:

import std.sys (Command)

let child = Command.new('ls').spawn
let status = child.wait.get

There's also ChildProcess.try_wait, which returns immediately if the process is still running, instead of waiting for it to finish.

The input and output streams are accessed using ChildProcess.stdin, ChildProcess.stdout, and ChildProcess.stderr. For example, to read from STDOUT:

import std.sys (Command, Stream)

let cmd = Command.new('ls')

cmd.stdout = Stream.Piped

let child = cmd.spawn.get
let status = child.wait.get
let bytes = ByteArray.new

match child.stdout {
  case Some(v) -> v.read_all(bytes).get
  case _ -> {}
}

Fields

program

let pub @program: String

The path to the program to spawn.

stdin

let pub @stdin: Stream

What to do with the STDIN stream.

stdout

let pub @stdout: Stream

What to do with the STDOUT stream.

stderr

let pub @stderr: Stream

What to do with the STDERR stream.

arguments

let pub @arguments: Array[String]

The arguments to pass to the command.

variables

let pub @variables: Map[String, String]

The environment variables to pass to the command.

This Map defaults to all the environment variables available at the time the program started.

directory

let pub @directory: Option[Path]

The working directory to use for the command.

Static methods

new

Show source code
Hide source code
fn pub static new[T: ToString](program: ref T) -> Command {
  Command(
    program: program.to_string,
    stdin: Stream.Inherit,
    stdout: Stream.Inherit,
    stderr: Stream.Inherit,
    arguments: [],
    variables: env.variables,
    directory: Option.None,
  )
}
fn pub static new[T: ToString](program: ref T) -> Command

Creates a new Command that will run the given program.

The program can either be the name (e.g. ls), or a path to the command (e.g. /usr/bin/ls). If just a name is given, the PATH variable is searched to find the path to the command.

The input and output streams default to inheriting from their parent (= the current OS process).

Examples

Using a command name:

import std.sys (Command)

Command.new('ls')

Using a command path:

import std.sys (Command)

Command.new('/usr/bin/ls')

Instance methods

directory

Show source code
Hide source code
fn pub mut directory -> Option[Path] {
  @directory.clone
}
fn pub mut directory -> Option[Path]

Returns the working directory to use for the child process, if any.

directory=

Show source code
Hide source code
fn pub mut directory=(path: Path) {
  @directory = Option.Some(path)
}
fn pub mut directory=(path: Path)

Sets the working directory to use for the child process.

Examples

import std.sys (Command)

let cmd = Command.new('ls')

cmd.directory = '/'.to_path

spawn

Show source code
Hide source code
fn pub spawn -> Result[ChildProcess, Error] {
  sys
    .spawn(
      @program,
      @arguments,
      @variables,
      @directory.as_ref.map(fn (v) { v.to_string }),
      @stdin,
      @stdout,
      @stderr,
    )
    .map(fn (v) { ChildProcess.new(v) })
}
fn pub spawn -> Result[ChildProcess, Error]

Spawns a child process that runs the command.

Examples

import std.sys (Command)

let child = Command.new('ls').spawn.get

child.wait.get