Struct Command

Source
pub struct Command {
    pub program: PathBuf,
    pub args: Vec<OsString>,
    pub dir: Option<PathBuf>,
    pub log_to: LogTo,
    pub log_command: bool,
    pub log_output_on_error: bool,
    pub check: bool,
    pub capture: bool,
    pub combine_output: bool,
    pub clear_env: bool,
    pub env: HashMap<OsString, OsString>,
}
Expand description

A command to run in a subprocess and options for how it is run.

Some notable trait implementations:

Fields§

§program: PathBuf

Program path.

The path can be just a file name, in which case the $PATH is searched.

§args: Vec<OsString>

Arguments passed to the program.

§dir: Option<PathBuf>

Directory from which to run the program.

If not set (the default), the current working directory is used.

§log_to: LogTo

Where log messages go. The default is stdout.

§log_command: bool

If true (the default), log the command before running it.

§log_output_on_error: bool

If true, log the output if the command exits non-zero or due to a signal. This does nothing is capture is false or if check is false. The default is false.

§check: bool

If true (the default), check if the command exited successfully and return an error if not.

§capture: bool

If true, capture the stdout and stderr of the command. The default is false.

§combine_output: bool

If true, send stderr to stdout; the stderr field in Output will be empty. The default is false.

§clear_env: bool

If false (the default), inherit environment variables from the current process.

§env: HashMap<OsString, OsString>

Add or update environment variables in the child process.

Implementations§

Source§

impl Command

Source

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

Make a new Command with the given program.

All other fields are set to the defaults.

Source

pub fn with_args<I, S1, S2>(program: S1, args: I) -> Self
where S1: AsRef<OsStr>, S2: AsRef<OsStr>, I: IntoIterator<Item = S2>,

Make a new Command with the given program and args.

All other fields are set to the defaults.

Source

pub fn from_whitespace_separated_str(s: &str) -> Option<Self>

Create a Command from a whitespace-separated string. If the string is empty or all whitespace, None is returned.

This function does not do unquoting or escaping.

Source

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

Append a single argument.

Source

pub fn add_arg_pair<S1, S2>(&mut self, arg1: S1, arg2: S2) -> &mut Self
where S1: AsRef<OsStr>, S2: AsRef<OsStr>,

Append two arguments.

This is equivalent to calling add_arg twice; it is for the common case where the arguments have different types, e.g. a literal string for the first argument and a Path for the second argument.

Source

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

Append multiple arguments.

Source

pub fn enable_capture(&mut self) -> &mut Self

Set capture to true.

Source

pub fn combine_output(&mut self) -> &mut Self

Set combine_output to true.

Source

pub fn set_dir<S: AsRef<OsStr>>(&mut self, dir: S) -> &mut Self

Set the directory from which to run the program.

Source

pub fn disable_check(&mut self) -> &mut Self

Set check to false.

Source

pub fn run(&self) -> Result<Output, Error>

Run the command.

If capture is true, the command’s output (stdout and stderr) is returned along with the status. If not, the stdout and stderr are empty.

If the command fails to start an error is returned. If check is set, an error is also returned if the command exits non-zero or due to a signal.

If log_command is true then the command line is logged before running it. If the command fails the error is not logged or printed, but the resulting error type implements Display and can be used for this purpose.

Source

pub fn command_line_lossy(&self) -> String

Format as a space-separated command line.

The program path and the arguments are converted to strings with String::from_utf8_lossy.

If any component contains characters that are not ASCII alphanumeric or in the set /-_,:.=+, the component is quoted with ' (single quotes). This is both too aggressive (unnecessarily quoting things that don’t need to be quoted) and incorrect (e.g. a single quote will itself be quoted with a single quote). This method is mostly intended for logging though, and it should work reasonably well for that.

Trait Implementations§

Source§

impl Clone for Command

Source§

fn clone(&self) -> Command

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Command

Source§

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

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

impl Default for Command

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<&Command> for Command

Source§

fn from(cmd: &Command) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Command

Source§

fn eq(&self, other: &Command) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Command

Source§

impl StructuralPartialEq for Command

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.