Skip to main content

Command

Struct Command 

Source
pub struct Command { /* private fields */ }

Implementations§

Source§

impl Command

Source

pub fn new<S: AsRef<OsStr>>(program: S) -> Self

Source

pub fn arg_would_fit<S: AsRef<OsStr>>(&self, arg: S) -> bool

Check if an additional argument would fit in the command.

Source

pub fn args_would_fit<I, S>(&self, args: I) -> bool
where I: IntoIterator<Item = S>, S: AsRef<OsStr>,

Check if multiple additional arguments would all fit in the command.

Source

pub fn try_arg<S: AsRef<OsStr>>(&mut self, arg: S) -> Result<&mut Self>

Like std::process::Command::arg, add an argument to the command, but only if it will fit.

Source

pub fn try_args<I, S>(&mut self, args: I) -> Result<&mut Self>
where I: IntoIterator<Item = S> + Copy, S: AsRef<OsStr>,

Like std::process::Command::arg, add multiple arguments to the command, but only if they will all fit.

Source

pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self

Source

pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self

Source

pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self

Source

pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self

Source

pub fn spawn(&mut self) -> Result<Child>

Source

pub fn output(&mut self) -> Result<Output>

Source

pub fn status(&mut self) -> Result<ExitStatus>

Methods from Deref<Target = Command>§

1.57.0 · Source

pub fn get_program(&self) -> &OsStr

Returns the path to the program that was given to Command::new.

§Examples
use std::process::Command;

let cmd = Command::new("echo");
assert_eq!(cmd.get_program(), "echo");
1.57.0 · Source

pub fn get_args(&self) -> CommandArgs<'_>

Returns an iterator of the arguments that will be passed to the program.

This does not include the path to the program as the first argument; it only includes the arguments specified with Command::arg and Command::args.

§Examples
use std::ffi::OsStr;
use std::process::Command;

let mut cmd = Command::new("echo");
cmd.arg("first").arg("second");
let args: Vec<&OsStr> = cmd.get_args().collect();
assert_eq!(args, &["first", "second"]);
1.57.0 · Source

pub fn get_envs(&self) -> CommandEnvs<'_>

Returns an iterator of the environment variables explicitly set for the child process.

Environment variables explicitly set using Command::env, Command::envs, and Command::env_remove can be retrieved with this method.

Note that this output does not include environment variables inherited from the parent process. To see the full list of environment variables, including those inherited from the parent process, use Command::get_resolved_envs.

Each element is a tuple key/value pair (&OsStr, Option<&OsStr>). A None value indicates its key was explicitly removed via Command::env_remove. The associated key for the None value will no longer inherit from its parent process.

An empty iterator can indicate that no explicit mappings were added or that Command::env_clear was called. After calling Command::env_clear, the child process will not inherit any environment variables from its parent process.

§Examples
use std::ffi::OsStr;
use std::process::Command;

let mut cmd = Command::new("ls");
cmd.env("TERM", "dumb").env_remove("TZ");
let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();
assert_eq!(envs, &[
    (OsStr::new("TERM"), Some(OsStr::new("dumb"))),
    (OsStr::new("TZ"), None)
]);
Source

pub fn get_resolved_envs(&self) -> CommandResolvedEnvs

🔬This is a nightly-only experimental API. (command_resolved_envs)

Returns an iterator of the environment variables that will be set when the process is spawned.

This returns the environment as it would be if the command were executed at the time of calling this method. The returned environment includes:

Note that the returned environment is a snapshot at the time this method is called and will not reflect any subsequent changes to the Command or the parent process’s environment. Additionally, it will not reflect changes made in a pre_exec hook (on Unix platforms).

Each element is a tuple (OsString, OsString) representing an environment variable key and value.

§Examples
#![feature(command_resolved_envs)]
use std::process::Command;
use std::ffi::{OsString, OsStr};
use std::env;
use std::collections::HashMap;

let mut cmd = Command::new("ls");
cmd.env("TZ", "UTC");
unsafe { env::set_var("EDITOR", "vim"); }

let resolved: HashMap<OsString, OsString> = cmd.get_resolved_envs().collect();
assert_eq!(resolved.get(OsStr::new("TZ")), Some(&OsString::from("UTC")));
assert_eq!(resolved.get(OsStr::new("EDITOR")), Some(&OsString::from("vim")));
1.57.0 · Source

pub fn get_current_dir(&self) -> Option<&Path>

Returns the working directory for the child process.

This returns None if the working directory will not be changed.

§Examples
use std::path::Path;
use std::process::Command;

let mut cmd = Command::new("ls");
assert_eq!(cmd.get_current_dir(), None);
cmd.current_dir("/bin");
assert_eq!(cmd.get_current_dir(), Some(Path::new("/bin")));
Source

pub fn get_env_clear(&self) -> bool

🔬This is a nightly-only experimental API. (command_resolved_envs)

Returns whether the environment will be cleared for the child process.

This returns true if Command::env_clear was called, and false otherwise. When true, the child process will not inherit any environment variables from its parent process.

§Examples
#![feature(command_resolved_envs)]
use std::process::Command;

let mut cmd = Command::new("ls");
assert_eq!(cmd.get_env_clear(), false);

cmd.env_clear();
assert_eq!(cmd.get_env_clear(), true);

Trait Implementations§

Source§

impl Debug for Command

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for Command

Source§

type Target = Command

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.