Struct devx_cmd::Cmd[][src]

pub struct Cmd(_);
Expand description

More convenient version of std::process::Command. Allows for spawning child processes with or without capturing their stdout. It also comes with inbuilt logging of the invocations via log crate.

All the methods for invoking a Cmd:

For more laconic usage see cmd and other macros.

Example:

let mut cmd = Cmd::new("cargo");
cmd
    // `arg*()` methods append arguments
    .arg("metadata")
    .arg2("--color", "never")
    .args(&["--verbose", "--no-deps", "--all-features"])
    .replace_arg(3, "--quiet")
    // These are at `debug` and `error` level by default, `None` disables logging
    .log_cmd(None)
    .log_err(log::Level::Warn)
    // repetated `stdin*()` calls overwrite previous ones
    .stdin("Hi")
    .stdin_bytes(vec![0, 1, 2]);

let () = cmd.run()?;
let output: String = cmd.read()?;
let output: Vec<u8> = cmd.read_bytes()?;
let process: Child = cmd.spawn()?;

Implementations

Returns a command builder that invokes the binary at bin. You should also be able to pass the command by name if it is in PATH.

Does not verify that the binary is actually available at the given path. If it isn’t, then an error will be returned when executing the command.

Returns a command builder if there is some file available at bin_path. If there is no file at the given path returns None. Beware that this won’t take PATH env variable into account. This function expects a relative or absolute filesystem path to the binary, and tries to check if there is some file there (retrying with .exe extension on windows).

If you want to find a binary through PATH, you should use Cmd::lookup_in_path()

Returns a command builder for the given bin_name only if this bin_name is accessible trough PATH env variable, otherwise returns None

Set binary path, overwrites the path that was set before.

Retuns the currently configured binary path.

Set the current directory for the child process.

Inherits this process current dir by default.

Returns the currently configured current process directory path

When set to some log::Level the command with its arguments and output will be logged via log crate.

Note that this method is independent from Cmd::log_err().

Default: Some(log::Level::Debug)

When set to some log::Level the invocation error will be logged. Set it to None or log::Level::Trace if non-zero exit code is an expected/recoverable error which doesn’t need to be logged.

Note that this method is independent from Cmd::log_cmd().

Default: Some(log::Level::Error)

Sets the string input passed to child process’s stdin. This overwrites the previous value.

Use Cmd::stdin_bytes() if you need to pass non-utf8 byte sequences.

Nothing is written to stdin by default.

Sets the bytes input passed to child process’s stdin. This overwrites the previous value.

Nothing is written to stdin by default.

Same as cmd.arg(arg1).arg(arg2). This is just a convenient shortcut mostly used to lexically group related arguments (for example named arguments).

Appends a single argument to the list of arguments passed to the child process.

Replaces the argument at the given index with a new value.

Panics

Panics if the given index is out of range of the arguments already set on this command builder.

Extends the array of arguments passed to the child process with args.

Returns the currently configured list of command line arguments

Inserts or updates an environment variable mapping.

Note that environment variable names are case-insensitive (but case-preserving) on Windows, and case-sensitive on all other platforms.

Same as cmd.spawn()?.wait() See Child::wait() for details.

Same as cmd.spawn_piped()?.read() See Child::read() for details.

Same as cmd.spawn_piped()?.read_bytes() See Child::read_bytes() for details.

Spawns a child process returning a handle to it. The child inherits both stdout and stderr. See the docs for Child for more details. Note that reading the child process output streams will panic! If you want to read the output, see Cmd::spawn_piped()

Spawns a child process returning a handle to it. Child’s stdout will be piped for further reading from it, but stderr will be inherited. See the docs for Child for more details.

More flexible version of spawn methods that allows you to specify any combination of stdout and stderr conifigurations.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.