7 releases (4 breaking)
0.6.0 | Oct 19, 2024 |
---|---|
0.5.0 | Oct 19, 2024 |
0.4.1 | Sep 24, 2024 |
0.4.0 | Mar 28, 2024 |
0.2.1 | Mar 21, 2024 |
#986 in Parser implementations
46 downloads per month
Used in 3 crates
69KB
1K
SLoC
command-error
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
~3.5MB
~64K SLoC