Skip to content

Commit

Permalink
ParseFailure::exit_code
Browse files Browse the repository at this point in the history
  • Loading branch information
pacak committed Feb 11, 2023
1 parent 6c22fd3 commit 95b5db1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,7 @@ impl<T> OptionParser<T> {
{
match self.try_run() {
Ok(t) => t,
Err(ParseFailure::Stdout(msg)) => {
print!("{}", msg); // completions are sad otherwise
std::process::exit(0);
}
Err(ParseFailure::Stderr(msg)) => {
eprintln!("{}", msg);
std::process::exit(1);
}
Err(err) => std::process::exit(err.exit_code()),
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,22 @@ impl ParseFailure {
}
}
}

/// Run an action appropriate to the failure and produce the exit code
///
/// Prints a message to `stdout` or `stderr` and returns the exit code
pub fn exit_code(self) -> i32 {
match self {
ParseFailure::Stdout(msg) => {
print!("{}", msg); // completions are sad otherwise
0
}
ParseFailure::Stderr(msg) => {
eprintln!("{}", msg);
1
}
}
}
}

/// Strip a command name if present at the front when used as a `cargo` command
Expand Down

0 comments on commit 95b5db1

Please sign in to comment.