Struct std::old_io::process::CommandUnstable
[-] [+]
[src]
pub struct Command { // some fields omitted }
The Command
type acts as a process builder, providing fine-grained control
over how a new process should be spawned. A default configuration can be
generated using Command::new(program)
, where program
gives a path to the
program to be executed. Additional builder methods allow the configuration
to be changed (for example, by adding arguments) prior to spawning:
use std::old_io::Command; let mut process = match Command::new("sh").arg("-c").arg("echo hello").spawn() { Ok(p) => p, Err(e) => panic!("failed to execute process: {}", e), }; let output = process.stdout.as_mut().unwrap().read_to_end();
Methods
impl Command
fn new<T: BytesContainer>(program: T) -> Command
Constructs a new Command
for launching the program at
path program
, with the following default configuration:
- No arguments to the program
- Inherit the current process's environment
- Inherit the current process's working directory
- A readable pipe for stdin (file descriptor 0)
- A writeable pipe for stdout and stderr (file descriptors 1 and 2)
Builder methods are provided to change these defaults and otherwise configure the process.
fn arg<'a, T: BytesContainer>(&'a mut self, arg: T) -> &'a mut Command
Add an argument to pass to the program.
fn args<'a, T: BytesContainer>(&'a mut self, args: &[T]) -> &'a mut Command
Add multiple arguments to pass to the program.
fn env<'a, T, U>(&'a mut self, key: T, val: U) -> &'a mut Command where T: BytesContainer, U: BytesContainer
Inserts or updates an environment variable mapping.
Note that environment variable names are case-insensitive (but case-preserving) on Windows, and case-sensitive on all other platforms.
fn env_remove<'a, T>(&'a mut self, key: T) -> &'a mut Command where T: BytesContainer
Removes an environment variable mapping.
fn env_set_all<'a, T, U>(&'a mut self, env: &[(T, U)]) -> &'a mut Command where T: BytesContainer, U: BytesContainer
Sets the entire environment map for the child process.
If the given slice contains multiple instances of an environment variable, the rightmost instance will determine the value.
fn cwd<'a>(&'a mut self, dir: &Path) -> &'a mut Command
Set the working directory for the child process.
fn stdin<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command
Configuration for the child process's stdin handle (file descriptor 0).
Defaults to CreatePipe(true, false)
so the input can be written to.
fn stdout<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command
Configuration for the child process's stdout handle (file descriptor 1).
Defaults to CreatePipe(false, true)
so the output can be collected.
fn stderr<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command
Configuration for the child process's stderr handle (file descriptor 2).
Defaults to CreatePipe(false, true)
so the output can be collected.
fn uid<'a>(&'a mut self, id: usize) -> &'a mut Command
Sets the child process's user id. This translates to a setuid
call in
the child process. Setting this value on windows will cause the spawn to
fail. Failure in the setuid
call on unix will also cause the spawn to
fail.
fn gid<'a>(&'a mut self, id: usize) -> &'a mut Command
Similar to uid
, but sets the group id of the child process. This has
the same semantics as the uid
field.
fn detached<'a>(&'a mut self) -> &'a mut Command
Sets the child process to be spawned in a detached state. On unix, this means that the child is the leader of a new process group.
fn spawn(&self) -> IoResult<Process>
Executes the command as a child process, which is returned.
fn output(&self) -> IoResult<ProcessOutput>
Executes the command as a child process, waiting for it to finish and collecting all of its output.
Example
fn main() { use std::old_io::Command; let output = match Command::new("cat").arg("foot.txt").output() { Ok(output) => output, Err(e) => panic!("failed to execute process: {}", e), }; println!("status: {}", output.status); println!("stdout: {}", String::from_utf8_lossy(output.output.as_slice())); println!("stderr: {}", String::from_utf8_lossy(output.error.as_slice())); }use std::old_io::Command; let output = match Command::new("cat").arg("foot.txt").output() { Ok(output) => output, Err(e) => panic!("failed to execute process: {}", e), }; println!("status: {}", output.status); println!("stdout: {}", String::from_utf8_lossy(output.output.as_slice())); println!("stderr: {}", String::from_utf8_lossy(output.error.as_slice()));
fn status(&self) -> IoResult<ProcessExit>
Executes a command as a child process, waiting for it to finish and collecting its exit status.
Example
fn main() { use std::old_io::Command; let status = match Command::new("ls").status() { Ok(status) => status, Err(e) => panic!("failed to execute process: {}", e), }; println!("process exited with: {}", status); }use std::old_io::Command; let status = match Command::new("ls").status() { Ok(status) => status, Err(e) => panic!("failed to execute process: {}", e), }; println!("process exited with: {}", status);
Trait Implementations
impl Debug for Command
fn fmt(&self, f: &mut Formatter) -> Result
Format the program and arguments of a Command for display. Any non-utf8 data is lossily converted using the utf8 replacement character.