2 unstable releases

0.2.0 Feb 16, 2024
0.1.0 Jan 5, 2024

#429 in Testing

Download history 1610/week @ 2024-08-04 1575/week @ 2024-08-11 2117/week @ 2024-08-18 1862/week @ 2024-08-25 1906/week @ 2024-09-01 1526/week @ 2024-09-08 1654/week @ 2024-09-15 1409/week @ 2024-09-22 1600/week @ 2024-09-29 1040/week @ 2024-10-06 1562/week @ 2024-10-13 2074/week @ 2024-10-20 2196/week @ 2024-10-27 2157/week @ 2024-11-03 1379/week @ 2024-11-10 1657/week @ 2024-11-17

7,534 downloads per month
Used in 11 crates (4 directly)

MIT/Apache

17KB
253 lines

axoprocess

crates.io docs Rust CI

Nicer defaults for invoking CLI Commands.

License

Licensed under either of

at your option.


lib.rs:

Nicer defaults for invoking CLI Commands.

[Cmd][] is a wrapper around std::process::Command with largely the same API except we want to be able to:

  • Produce nicer errors that explain what was being run (using thiserror/miette)
  • Log every time the command is executed (defaults tracing::info!)
  • Automatically check the return status's success() (can be opted-out per Cmd)

If you like the defaults then mostly all you need to know is that Cmd::new takes a second argument for "what should I tell the user this Command was trying to do at a high level".

This lets us turn the following logic:

#
let mut cmd = Command::new("cargo");
cmd.arg("-V");

info!("exec {:?}", cmd);

let output = cmd.output()
  .map_err(|cause| MyCmdError {
      desc: "failed to get your cargo toolchain's version",
      cause
  })?;

if !output.status.success() {
    Err(MyStatusError {
        desc: "failed to get your cargo toolchain's version",
        status: output.status
    })?;
}

println!("version was {}", String::from_utf8_lossy(&output.stdout));

Into this:

let output = Cmd::new("cargo", "get your cargo toolchain's version")
  .arg("-V")
  .output()?;

println!("version was {}", String::from_utf8_lossy(&output.stdout));

Which is, a lot nicer!

Dependencies

~2–2.7MB
~46K SLoC