#command-output #command #error #error-message #process

command-error

Detailed error messages and status checking for std::process::Command

4 releases (2 breaking)

0.4.0 Mar 28, 2024
0.3.0 Mar 22, 2024
0.2.1 Mar 21, 2024
0.2.0 Mar 21, 2024

#448 in Text processing

Download history 83/week @ 2024-03-15 414/week @ 2024-03-22 117/week @ 2024-03-29 47/week @ 2024-04-05

661 downloads per month
Used in 2 crates

MIT license

61KB
927 lines

command-error

docs.rs Crates.io

An extension trait for std::process::Command with detailed error messages, status code checking, and UTF-8 decoding via utf8-command.


lib.rs:

command_error provides the CommandExt trait, which runs a command and checks its exit status:

use std::process::Command;
use command_error::CommandExt;

let err = Command::new("sh")
    .args(["-c", "echo puppy; false"])
    .output_checked_utf8()
    .unwrap_err();

assert_eq!(
    err.to_string(),
    indoc!(
        "`sh` failed: exit status: 1
        Command failed: `sh -c 'echo puppy; false'`
        Stdout:
          puppy"
    )
);

Error messages are detailed and helpful. Additional methods are provided for overriding the default success logic (for that weird tool that thinks 2 is a reasonable exit code) and for transforming the output (for example, to parse command output as JSON while retaining information about the command that produced the output in the error message).

Enforcing use of command_error

If you'd like to make sure that CommandExt methods are used instead of the plain Command methods in your project, you can add a stanza like this to clippy.toml at your project root:

[[disallowed-methods]]
path = "std::process::Command::output"
reason = "Use command_error::CommandExt::output_checked[_with][_utf8]"

[[disallowed-methods]]
path = "std::process::Command::status"
reason = "Use command_error::CommandExt::status_checked[_with]"

[[disallowed-methods]]
path = "std::process::Command::spawn"
reason = "Use command_error::CommandExt::spawn_checked"

[[disallowed-methods]]
path = "std::process::Child::try_wait"
reason = "Use command_error::ChildExt::try_wait_checked[_with]"

[[disallowed-methods]]
path = "std::process::Child::wait"
reason = "Use command_error::ChildExt::wait_checked[_with]"

[[disallowed-methods]]
path = "std::process::Child::wait_with_output"
reason = "Use command_error::ChildExt::output_checked[_with][_utf8]"

Dependencies

~140KB