pub struct Command { /* private fields */ }tokio only.Expand description
A command to run inside an asynchronous pseudoconsole session.
Mirrors std::process::Command: the builder methods take &mut self and
return &mut Self, so a whole invocation can be written as one expression.
The differences from the standard library are the ones a pseudoconsole
forces:
- There is no stdio configuration. The child’s console is the
pseudoconsole; its standard handles are deliberately set to
INVALID_HANDLE_VALUEso a redirected parent cannot leak its own stdio into the child. - No handles are inherited (
bInheritHandlesisFALSE), because a leaked copy of the output pipe would keep the session from ever reaching end-of-file. - The child and every descendant it creates join a job object, which is
what makes
Child::killterminate the whole tree.
A command is intentionally not Clone: managed spawning must not copy or
mutate its potentially large argument and environment buffers. Low-level
lifecycle and unvalidated process flags are intentionally absent; hidden
compile-fail doctests pin both boundaries.
Implementations§
Source§impl Command
impl Command
Sourcepub fn new(program: impl AsRef<OsStr>) -> Self
pub fn new(program: impl AsRef<OsStr>) -> Self
Creates a builder for launching program.
The program is not resolved here; a missing executable surfaces as
an error with crate::ErrorKind::Spawn and a
std::io::ErrorKind::NotFound source.
Sourcepub fn arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self
pub fn arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self
Appends one argument, quoted and escaped as the MSVC C runtime expects.
Sourcepub fn args<I, S>(&mut self, args: I) -> &mut Self
pub fn args<I, S>(&mut self, args: I) -> &mut Self
Appends several arguments; equivalent to calling Command::arg for
each one.
Sourcepub fn raw_arg(&mut self, text: impl AsRef<OsStr>) -> &mut Self
pub fn raw_arg(&mut self, text: impl AsRef<OsStr>) -> &mut Self
Appends literal text to the command line, bypassing all quoting.
Same semantics as std::os::windows::process::CommandExt::raw_arg:
intended for callees such as cmd.exe /c that parse the raw command
line themselves.
Sourcepub fn env(
&mut self,
key: impl AsRef<OsStr>,
value: impl AsRef<OsStr>,
) -> &mut Self
pub fn env( &mut self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>, ) -> &mut Self
Sets an environment variable for the child (case-insensitively, as Windows does).
Sourcepub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
Sets several environment variables; equivalent to calling
Command::env for each pair.
Sourcepub fn env_remove(&mut self, key: impl AsRef<OsStr>) -> &mut Self
pub fn env_remove(&mut self, key: impl AsRef<OsStr>) -> &mut Self
Removes an environment variable from the child’s environment.
Sourcepub fn env_clear(&mut self) -> &mut Self
pub fn env_clear(&mut self) -> &mut Self
Clears the child’s environment, including modifications recorded so far. Variables set afterwards still apply.
Sourcepub fn current_dir(&mut self, dir: impl AsRef<Path>) -> &mut Self
pub fn current_dir(&mut self, dir: impl AsRef<Path>) -> &mut Self
Sets the child’s working directory.
Sourcepub fn spawn(&mut self) -> Result<Session>
pub fn spawn(&mut self) -> Result<Session>
Spawns a managed asynchronous session with default options.
This is synchronous because process creation itself does not block. The returned session owns a kill-on-close Job; dropping it before completion terminates the entire process tree.
§Errors
Returns an error when the backend or pipes cannot be initialized —
including when no Tokio runtime with an enabled I/O driver is
current, which surfaces as crate::ErrorKind::CreateConsole — or
when the root process cannot be spawned.
Sourcepub fn spawn_with(&mut self, options: SessionOptions) -> Result<Session>
pub fn spawn_with(&mut self, options: SessionOptions) -> Result<Session>
Spawns a managed asynchronous session with explicit safe options.
§Errors
Returns an error when the selected backend or pipes cannot be
initialized — including when no Tokio runtime with an enabled I/O
driver is current, which surfaces as
crate::ErrorKind::CreateConsole — or when the root process cannot
be spawned.