[][src]Crate devx_cmd

devx-cmd provides more convenient primitives for spawning child processes than std::process targeted for use in development scripts specifically.

The main entities of the crate are Cmd (builder for executable commands), and Child (represents a spawned process).

There are also some convenient macros to reduce boilerplate. Here is the basic usage example:

use devx_cmd::{read, run, cmd, Cmd};

// Run the program, logging the invocation to `stderr` and waiting until it finishes
// This is used only for side-effects.
// Note that if the process ends with a non-zero status code, this will return an error.
run!("ls", "-la")?;

// Same as `run!()`, but captures the stdout and returns it as a `String`
// there is also a `read_bytes!()` for non-utf8 sequences
let output = read!("echo", "foo")?;
assert_eq!(output.trim(), "foo");

let mut cmd = cmd!("rustfmt");
cmd
    // Don't log command invocation and output to stderr
    .echo_cmd(false)
    // Don't log invocation error to stderr
    .echo_err(false)
    .stdin("fn foo () -> u32 {42}\n");

// Spawn without waiting for its completion, but capturing the stdout
let mut child = cmd.spawn_piped()?;

// Read output line-by-line
let first_line = child.stdout_lines().next().unwrap();

assert_eq!(first_line.trim(), "fn foo() -> u32 {");

// Dropping the child process `kill()`s it (and ignores the `Result`)
// Use `.wait()/.read()` to wait until its completion.
drop(child);

Macros

cmd

Create a Cmd with the given binary and arguments.

read

Shortcut for cmd!(...).read(). See Cmd::read for details

read_bytes

Shortcut for cmd!(...).read_bytes(). See Cmd::read for details

run

Shortcut for cmd!(...).run(). See Cmd::run for details

Structs

Child

Wraps std::process::Child, kills and waits for the process on Drop. It will log the fact that std::process::Child::kill was called in Drop. You should use Child::wait for the process to finish with any of the available methods if you want to handle the error, otherwise it will be ignored.

Cmd

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

Error

Opaque error which happened during command execution.

Type Definitions

Result

Shortcut for Result<T, devx_cmd::Error>