Expand description
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]"Structs§
- Child
Context - A
Childprocess combined with context about theCommandthat produced it. - Exec
Error - An error from failing to execute a command. Produced by
CommandExt. - Output
Context Outputcombined with context about theCommandthat produced it.- Output
Conversion Error - An error produced when attempting to convert
CommandOutputto a custom format (such asUtf8Output). - Output
Error - An error from a failed command, typically due to a non-zero exit status.
- TryWait
Context - An optional
ExitStatuscombined with context about theCommandthat produced it. - Utf8
Program AndArgs - A program name and arguments stored as UTF-8
Strings. - Wait
Error - An error from failing to wait for a command. Produced by
ChildExt.
Enums§
Traits§
- Child
Ext - Checked methods for
Childprocesses. - Command
Display - A
Commandthat can beDisplayed. - Command
Ext - Extension trait for
Command. - Output
Like - A command output type.