Struct xshell::Cmd

source ·
pub struct Cmd<'a> { /* private fields */ }
Expand description

A builder object for constructing a subprocess.

A Cmd is usually created with the cmd! macro. The command exists within a context of a Shell and uses its working directory and environment.

Example

use xshell::{Shell, cmd};

let sh = Shell::new()?;

let branch = "main";
let cmd = cmd!(sh, "git switch {branch}").quiet().run()?;

Implementations§

source§

impl<'a> Cmd<'a>

source

pub fn arg<P: AsRef<OsStr>>(self, arg: P) -> Cmd<'a>

Adds an argument to this commands.

source

pub fn args<I>(self, args: I) -> Cmd<'a>where I: IntoIterator, I::Item: AsRef<OsStr>,

Adds all of the arguments to this command.

source

pub fn env<K: AsRef<OsStr>, V: AsRef<OsStr>>(self, key: K, val: V) -> Cmd<'a>

Overrides the value of the environmental variable for this command.

source

pub fn envs<I, K, V>(self, vars: I) -> Cmd<'a>where I: IntoIterator<Item = (K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr>,

Overrides the values of specified environmental variables for this command.

source

pub fn env_remove<K: AsRef<OsStr>>(self, key: K) -> Cmd<'a>

Removes the environment variable from this command.

source

pub fn env_clear(self) -> Cmd<'a>

Removes all of the environment variables from this command.

source

pub fn ignore_status(self) -> Cmd<'a>

Don’t return an error if command the command exits with non-zero status.

By default, non-zero exit status is considered an error.

source

pub fn set_ignore_status(&mut self, yes: bool)

Controls whether non-zero exit status is considered an error.

source

pub fn quiet(self) -> Cmd<'a>

Don’t echo the command itself to stderr.

By default, the command itself will be printed to stderr when executed via Cmd::run.

source

pub fn set_quiet(&mut self, yes: bool)

Controls whether the command itself is printed to stderr.

source

pub fn secret(self) -> Cmd<'a>

Marks the command as secret.

If a command is secret, it echoes <secret> instead of the program and its arguments, even in error messages.

source

pub fn set_secret(&mut self, yes: bool)

Controls whether the command is secret.

source

pub fn stdin(self, stdin: impl AsRef<[u8]>) -> Cmd<'a>

Pass the given slice to the standard input of the spawned process.

source

pub fn ignore_stdout(self) -> Cmd<'a>

Ignores the standard output stream of the process.

This is equivalent to redirecting stdout to /dev/null. By default, the stdout is inherited or captured.

source

pub fn set_ignore_stdout(&mut self, yes: bool)

Controls whether the standard output is ignored.

source

pub fn ignore_stderr(self) -> Cmd<'a>

Ignores the standard output stream of the process.

This is equivalent redirecting stderr to /dev/null. By default, the stderr is inherited or captured.

source

pub fn set_ignore_stderr(&mut self, yes: bool)

Controls whether the standard error is ignored.

source

pub fn run(&self) -> Result<()>

Runs the command.

By default the command itself is echoed to stderr, its standard streams are inherited, and non-zero return code is considered an error. These behaviors can be overridden by using various builder methods of the Cmd.

source

pub fn read(&self) -> Result<String>

Run the command and return its stdout as a string.

source

pub fn read_stderr(&self) -> Result<String>

Run the command and return its stderr as a string.

source

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

Run the command and return its output.

Trait Implementations§

source§

impl<'a> Debug for Cmd<'a>

source§

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

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

impl Display for Cmd<'_>

source§

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

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

impl From<Cmd<'_>> for Command

source§

fn from(cmd: Cmd<'_>) -> Command

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Cmd<'a>

§

impl<'a> !Send for Cmd<'a>

§

impl<'a> !Sync for Cmd<'a>

§

impl<'a> Unpin for Cmd<'a>

§

impl<'a> UnwindSafe for Cmd<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.