pub struct Exit { /* private fields */ }Expand description
The exit code and the text with which a command ends the process
A wrapper, such as a script or a CI job, reads the exit code of a program to decide what to do next. A command therefore needs to choose the code. The chosen code is not always a failure: a check that finds a problem ends as planned, and it can still exit with a code other than 0 to tell the wrapper what it found.
The text is what the command wants the user to read last, such as a diagnostic that the application rendered itself. Clawless does not render the text. It writes the text to the standard error once, exactly as it is, and adds no prefix and no line break. End the text with a line break unless the text must not have one.
Clawless writes the text after it has rendered all the output that the command sent, and the
process exits after the text. The text therefore appears last on the standard error. The
standard output carries only the result of the command, so JSON output stays valid. The
verbosity does not apply to the text, so --quiet does not hide it.
An error that a command returns always ends the process with the exit code 1. A command that
needs another code for a failure turns the error into an Exit itself, and returns the Exit
in Ok.
Clawless itself uses these exit codes. A command can still use them, because Clawless reserves none of them.
| Code | Meaning |
|---|---|
| 0 | The command returned () |
| 1 | The command returned an error |
| 2 | The command line has an error, which the argument parser reports |
| 101 | The command or Clawless panicked, for example on a closed standard output |
| 130 | The user pressed Ctrl+C for the second time |
§Examples
use std::process::ExitCode;
use clawless_core::exit::Exit;
let exit = Exit::builder()
.code(ExitCode::from(3))
.text("error: 0.7.0 is not a stable release\n")
.build();
assert_eq!(exit.code(), ExitCode::from(3));
assert_eq!(exit.text(), Some("error: 0.7.0 is not a stable release\n"));A command that only chooses the code converts an ExitCode into an Exit without text:
use std::process::ExitCode;
use clawless_core::exit::Exit;
let exit = Exit::from(ExitCode::from(3));
assert_eq!(exit.text(), None);Implementations§
Source§impl Exit
impl Exit
Sourcepub fn builder() -> ExitBuilder
pub fn builder() -> ExitBuilder
Create an instance of Exit using the builder syntax
Source§impl Exit
impl Exit
Sourcepub fn from_error(error: &impl Debug) -> Exit
pub fn from_error(error: &impl Debug) -> Exit
Returns the exit of a command that failed with the given error
The exit code is 1. The text is Error: and the debug representation of the error, the
same as when the main function of a Rust program returns the error.
§Examples
use std::process::ExitCode;
use clawless_core::exit::Exit;
#[derive(Debug)]
struct MissingFile;
let exit = Exit::from_error(&MissingFile);
assert_eq!(exit.code(), ExitCode::FAILURE);
assert_eq!(exit.text(), Some("Error: MissingFile\n"));Sourcepub fn text(&self) -> Option<&str>
pub fn text(&self) -> Option<&str>
Returns the text that the process writes to the standard error before it exits
§Examples
use std::process::ExitCode;
use clawless_core::exit::Exit;
let exit = Exit::builder()
.code(ExitCode::from(3))
.text("error: the check failed\n")
.build();
assert_eq!(exit.text(), Some("error: the check failed\n"));Trait Implementations§
Source§impl From<()> for Exit
Returns the exit of a command that ended without a choice of its own
impl From<()> for Exit
Returns the exit of a command that ended without a choice of its own
The exit code is 0, and the process writes no text.
Source§impl From<Exit> for ProcessExit
Returns the process exit that ends the process with the given exit
impl From<Exit> for ProcessExit
Returns the process exit that ends the process with the given exit