Command

Struct Command 

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

Implementations§

Source§

impl Command

Source

pub fn new(value: impl AsRef<OsStr>) -> Self

Source

pub fn argument(self, value: impl AsRef<OsStr>) -> Self

Source

pub fn optional_argument(self, optional: Option<impl AsRef<OsStr>>) -> Self

Source

pub fn option(self, name: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> Self

Adds a CLI option (name + value).

command.option("--message", "hello")
// equivalent to: command.argument("--message").argument("hello")
Source

pub fn optional_option( self, name: impl AsRef<OsStr>, value: Option<impl AsRef<OsStr>>, ) -> Self

Adds a CLI option (name + value) only if the value is Some.

command.optional_option("--name", optional)
// adds "--name <value>" only if optional is Some
Source

pub fn arguments<T: AsRef<OsStr>>( self, value: impl IntoIterator<Item = T>, ) -> Self

Source

pub fn working_directory(self, dir: impl AsRef<Path>) -> Self

Source

pub fn env(self, key: &EnvVariableName<'_>, val: impl AsRef<OsStr>) -> Self

Source

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

Source

pub fn env_remove(self, key: &EnvVariableName<'_>) -> Self

Remove an environment variable from the child process.

Source

pub fn stdin_bytes(self, data: impl Into<Vec<u8>>) -> Self

Source

pub fn stdout(self) -> Capture

Capture stdout from this command.

Source

pub fn stderr(self) -> Capture

Capture stderr from this command.

Source

pub fn spawn(self) -> Spawn

Spawn the command as a child process.

Returns a Spawn builder to configure stdin/stdout/stderr before running.

§Example
use cmd_proc::{Command, Stdio};
use std::io::BufRead;

let mut child = Command::new("server")
    .argument("--port=8080")
    .spawn()
    .stdin(Stdio::Piped)
    .stdout(Stdio::Piped)
    .stderr(Stdio::Inherit)
    .run()
    .unwrap();

// Read a line from stdout
let line = std::io::BufReader::new(child.stdout().unwrap())
    .lines()
    .next()
    .unwrap()
    .unwrap();

// Close stdin to signal the child to exit
drop(child.take_stdin());
child.wait().unwrap();
Source

pub fn output(self) -> Result<Output, CommandError>

Execute the command and return full output regardless of exit status.

Unlike stdout() and stderr(), this does not treat non-zero exit as an error. Use this when you need to inspect both streams or handle failure cases with stderr.

Source

pub fn status(self) -> Result<(), CommandError>

Execute the command and return success or an error.

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<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.