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>
impl<'a> Cmd<'a>
Sourcepub fn env<K, V>(self, key: K, val: V) -> Cmd<'a>
pub fn env<K, V>(self, key: K, val: V) -> Cmd<'a>
Overrides the value of the environmental variable for this command.
Sourcepub fn envs<I, K, V>(self, vars: I) -> Cmd<'a>
pub fn envs<I, K, V>(self, vars: I) -> Cmd<'a>
Overrides the values of specified environmental variables for this command.
Sourcepub fn env_remove<K>(self, key: K) -> Cmd<'a>
pub fn env_remove<K>(self, key: K) -> Cmd<'a>
Removes the environment variable from this command.
Sourcepub fn ignore_status(self) -> Cmd<'a>
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.
Sourcepub fn set_ignore_status(&mut self, yes: bool)
pub fn set_ignore_status(&mut self, yes: bool)
Controls whether non-zero exit status is considered an error.
Sourcepub fn quiet(self) -> Cmd<'a>
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
.
Sourcepub fn set_quiet(&mut self, yes: bool)
pub fn set_quiet(&mut self, yes: bool)
Controls whether the command itself is printed to stderr.
Sourcepub fn secret(self) -> Cmd<'a>
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.
Sourcepub fn set_secret(&mut self, yes: bool)
pub fn set_secret(&mut self, yes: bool)
Controls whether the command is secret.
Sourcepub fn stdin(self, stdin: impl AsRef<[u8]>) -> Cmd<'a>
pub fn stdin(self, stdin: impl AsRef<[u8]>) -> Cmd<'a>
Pass the given slice to the standard input of the spawned process.
Sourcepub fn ignore_stdout(self) -> Cmd<'a>
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.
Sourcepub fn set_ignore_stdout(&mut self, yes: bool)
pub fn set_ignore_stdout(&mut self, yes: bool)
Controls whether the standard output is ignored.
Sourcepub fn ignore_stderr(self) -> Cmd<'a>
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.
Sourcepub fn set_ignore_stderr(&mut self, yes: bool)
pub fn set_ignore_stderr(&mut self, yes: bool)
Controls whether the standard error is ignored.
Sourcepub fn run(&self) -> Result<(), Error>
pub fn run(&self) -> Result<(), Error>
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
.
Sourcepub fn read(&self) -> Result<String, Error>
pub fn read(&self) -> Result<String, Error>
Run the command and return its stdout as a string. Any trailing newline or carriage return will be trimmed.
Sourcepub fn read_stderr(&self) -> Result<String, Error>
pub fn read_stderr(&self) -> Result<String, Error>
Run the command and return its stderr as a string. Any trailing newline or carriage return will be trimmed.
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Cmd<'a>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more