pub struct Command { /* private fields */ }Implementations§
Source§impl Command
impl Command
pub fn new(value: impl AsRef<OsStr>) -> Self
pub fn argument(self, value: impl AsRef<OsStr>) -> Self
pub fn optional_argument(self, optional: Option<impl AsRef<OsStr>>) -> Self
Sourcepub fn optional_flag(self, condition: bool, flag: impl AsRef<OsStr>) -> Self
pub fn optional_flag(self, condition: bool, flag: impl AsRef<OsStr>) -> Self
Adds a flag argument only if the condition is true.
ⓘ
command.optional_flag(verbose, "--verbose")
// adds "--verbose" only if verbose is trueSourcepub fn option(self, name: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> Self
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")Sourcepub fn optional_option(
self,
name: impl AsRef<OsStr>,
value: Option<impl AsRef<OsStr>>,
) -> Self
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 Somepub fn arguments<T: AsRef<OsStr>>( self, value: impl IntoIterator<Item = T>, ) -> Self
pub fn working_directory(self, dir: impl AsRef<Path>) -> Self
pub fn env(self, key: &EnvVariableName<'_>, val: impl AsRef<OsStr>) -> Self
pub fn envs<'a, I, V>(self, vars: I) -> Self
Sourcepub fn env_remove(self, key: &EnvVariableName<'_>) -> Self
pub fn env_remove(self, key: &EnvVariableName<'_>) -> Self
Remove an environment variable from the child process.
pub fn stdin_bytes(self, data: impl Into<Vec<u8>>) -> Self
Sourcepub fn capture_stdout(self) -> Capture
pub fn capture_stdout(self) -> Capture
Capture stdout from this command.
Sourcepub fn capture_stderr(self) -> Capture
pub fn capture_stderr(self) -> Capture
Capture stderr from this command.
Sourcepub fn capture_stderr_stdout(self) -> CaptureAllStreams
pub fn capture_stderr_stdout(self) -> CaptureAllStreams
Capture both stdout and stderr from this command.
Sourcepub fn spawn(self) -> Spawn
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 tokio::io::AsyncBufReadExt;
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 mut line = String::new();
tokio::io::BufReader::new(child.stdout().unwrap())
.read_line(&mut line)
.await
.unwrap();
// Close stdin to signal the child to exit
drop(child.take_stdin());
child.wait().await.unwrap();Sourcepub async fn output(self) -> Result<Output, CommandError>
pub async 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.
Sourcepub async fn status(self) -> Result<(), CommandError>
pub async fn status(self) -> Result<(), CommandError>
Execute the command and return success or an error.
Auto Trait Implementations§
impl Freeze for Command
impl RefUnwindSafe for Command
impl Send for Command
impl Sync for Command
impl Unpin for Command
impl UnwindSafe for Command
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
Mutably borrows from an owned value. Read more