Crate command_error

Source
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§

ChildContext
A Child process combined with context about the Command that produced it.
ExecError
An error from failing to execute a command. Produced by CommandExt.
OutputContext
Output combined with context about the Command that produced it.
OutputConversionError
An error produced when attempting to convert Command Output to a custom format (such as Utf8Output).
OutputError
An error from a failed command, typically due to a non-zero exit status.
TryWaitContext
An optional ExitStatus combined with context about the Command that produced it.
Utf8ProgramAndArgs
A program name and arguments stored as UTF-8 Strings.
WaitError
An error from failing to wait for a command. Produced by ChildExt.

Enums§

Error
An error produced by a Command failure.

Traits§

ChildExt
Checked methods for Child processes.
CommandDisplay
A Command that can be Displayed.
CommandExt
Extension trait for Command.
OutputLike
A command output type.