Custom help message when using argh

Help messages generated from argh is nearly useless and very hard to
customize. Fork argh and disable all code for generating help messages.

Use a closure to print the help message when handling EarlyExit.
This commit is contained in:
topjohnwu
2023-07-05 17:05:39 -07:00
parent 5ee6daf126
commit d778b0b0a7
7 changed files with 79 additions and 77 deletions

View File

@@ -382,20 +382,21 @@ pub fn map_args<'a>(argc: i32, argv: *const *const c_char) -> Result<Vec<&'a Utf
}
pub trait EarlyExitExt<T> {
fn early_exit(self) -> T;
fn on_early_exit<F: FnOnce()>(self, print_help_msg: F) -> T;
}
impl<T> EarlyExitExt<T> for Result<T, EarlyExit> {
fn early_exit(self) -> T {
fn on_early_exit<F: FnOnce()>(self, print_help_msg: F) -> T {
match self {
Ok(t) => t,
Err(EarlyExit { output, status }) => match status {
Ok(_) => {
eprintln!("{}", output);
print_help_msg();
exit(0)
}
Err(_) => {
eprintln!("{}", output);
print_help_msg();
exit(1)
}
},