Skip to main content

Command

Struct Command 

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

A process builder, providing control over how a new process should be spawned.

Implementations§

Source§

impl Command

Source

pub fn new(command: impl Into<OsString>) -> Self

Create a new Command, with the following default configuration:

  • Do not Inherit handles of the calling process.
  • Inherit the current drive and directory of the calling process.
  • Inherit the environment of the calling process.

Builder methods are provided to change these defaults and otherwise configure the process.

§Examples
use CreateProcessW::Command;

Command::new("notepad.exe")
    .spawn()
    .expect("notepad failed to start");

Equivalent to the lpCommandLine parameter of the CreateProcessW function.

Source

pub fn inherit_handles(&mut self, inherit: bool) -> &mut Self

Enable/disable handle inheritance.

When true, two things happen:

  1. Each inheritable handle in the calling process is inherited by the new process (maps to bInheritHandles).
  2. The process and thread handles returned in Child are themselves marked as inheritable, so future child processes can inherit them too (maps to bInheritHandle on lpProcessAttributes / lpThreadAttributes).

When false, neither happens.

Equivalent to the bInheritHandles parameter of the CreateProcessW function.

Source

pub fn current_dir(&mut self, dir: impl Into<PathBuf>) -> &mut Self

Sets the working directory for the child process.

It’s the full path to the current directory for the process. Note that you can use a raw string to avoid error when copy-pasting the path.

§Examples
use CreateProcessW::Command;

let check = Command::new("cargo.exe check")
    .current_dir(r"C:\Users\<user>\repos\<repo_name>")
    .status()
    .expect("cargo check command failed");

Equivalent to the lpCurrentDirectory parameter of the CreateProcessW function.

Source

pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Self
where K: AsRef<OsStr>, V: AsRef<OsStr>,

Inserts or updates an environment variable mapping.

When inheriting the parent’s environment (the default), the last call to env for a given key wins. Earlier calls with the same key are overridden.

A key should not contain ASCII = or a NUL byte.

§Examples
use CreateProcessW::Command;

Command::new("cmd.exe /c echo %MY_VAR%")
    .env("MY_VAR", "hello")
    .spawn()
    .expect("failed to execute process");
Source

pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
where I: IntoIterator<Item = (K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr>,

Adds or updates multiple environment variable mappings.

Works like repeated calls to env. The last occurrence of a duplicate key wins.

Source

pub fn env_remove<K>(&mut self, key: K) -> &mut Self
where K: AsRef<OsStr>,

Removes an environment variable from the inherited environment.

If env_clear is called before this method, removal is a no-op at that point (the child already starts with an empty environment). If env_clear is called after this method, the removal is erased along with all other environment configuration.

Note: The last operation on a key wins. Calling env_remove after env for the same key removes it and calling env after env_remove sets it.

§Examples
use CreateProcessW::Command;

Command::new("cmd.exe /c set")
    .env_remove("PATH")
    .spawn()
    .expect("failed to execute process");
Source

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

Clears the entire environment map for the child process.

The child will not inherit any environment variables from the parent process. Only variables added with env or envs will be present.

§Examples
use CreateProcessW::Command;

Command::new("cmd.exe /c set")
    .env_clear()
    .env("MY_VAR", "hello")
    .spawn()
    .expect("failed to execute process");
Source

pub fn spawn(&mut self) -> Result<Child, Error>

Executes the command as a child process, returning a handle to it.

§Examples
use CreateProcessW::Command;

Command::new("notepad.exe")
    .spawn()
    .expect("notepad failed to start");
Source

pub fn status(&mut self) -> Result<ExitStatus, Error>

Executes a command as a child process, waiting for it to finish and collecting its status.

§Examples
use CreateProcessW::Command;

let status = Command::new("notepad.exe")
    .status()
    .expect("failed to execute process");

println!("process finished with: {}", status.code());

assert!(status.success());

Trait Implementations§

Source§

impl Debug for Command

Source§

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

Formats the value using the given formatter. Read more

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.