Skip to content

Commit

Permalink
Show user defined help flags during try_help if present
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Jul 8, 2024
1 parent 866d7d1 commit 4431d99
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
34 changes: 29 additions & 5 deletions clap_builder/src/error/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#![cfg_attr(not(feature = "error-context"), allow(dead_code))]
#![cfg_attr(not(feature = "error-context"), allow(unused_imports))]

use std::borrow::Cow;

use crate::builder::Command;
use crate::builder::StyledStr;
use crate::builder::Styles;
Expand All @@ -12,6 +14,7 @@ use crate::error::ContextKind;
use crate::error::ContextValue;
use crate::error::ErrorKind;
use crate::output::TAB;
use crate::ArgAction;

/// Defines how to format an error for displaying to the user
pub trait ErrorFormatter: Sized {
Expand Down Expand Up @@ -120,7 +123,7 @@ impl ErrorFormatter for RichFormatter {
put_usage(&mut styled, usage);
}

try_help(&mut styled, styles, error.inner.help_flag);
try_help(&mut styled, styles, error.inner.help_flag.as_deref());

styled
}
Expand Down Expand Up @@ -461,7 +464,7 @@ pub(crate) fn format_error_message(
put_usage(&mut styled, usage);
}
if let Some(cmd) = cmd {
try_help(&mut styled, styles, get_help_flag(cmd));
try_help(&mut styled, styles, get_help_flag(cmd).as_deref());
}
styled
}
Expand All @@ -480,11 +483,32 @@ fn put_usage(styled: &mut StyledStr, usage: &StyledStr) {
styled.push_styled(usage);
}

pub(crate) fn get_help_flag(cmd: &Command) -> Option<&'static str> {
pub(crate) fn get_help_flag(cmd: &Command) -> Option<Cow<'static, str>> {
if !cmd.is_disable_help_flag_set() {
Some("--help")
Some(Cow::Borrowed("--help"))
} else if let Some(flag) = get_user_help_flag(cmd) {
Some(Cow::Owned(flag))
} else if cmd.has_subcommands() && !cmd.is_disable_help_subcommand_set() {
Some("help")
Some(Cow::Borrowed("help"))
} else {
None
}
}

fn get_user_help_flag(cmd: &Command) -> Option<String> {
let Some(arg) = cmd.get_arguments().find(|arg| {
matches!(
arg.get_action(),
ArgAction::Help | ArgAction::HelpShort | ArgAction::HelpLong
)
}) else {
return None;
};

if let Some(long) = arg.get_long() {
Some(format!("--{long}"))
} else if let Some(short) = arg.get_short() {
Some(format!("-{short}"))
} else {
None
}
Expand Down
4 changes: 2 additions & 2 deletions clap_builder/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct ErrorInner {
context: FlatMap<ContextKind, ContextValue>,
message: Option<Message>,
source: Option<Box<dyn error::Error + Send + Sync>>,
help_flag: Option<&'static str>,
help_flag: Option<Cow<'static, str>>,
styles: Styles,
color_when: ColorChoice,
color_help_when: ColorChoice,
Expand Down Expand Up @@ -319,7 +319,7 @@ impl<F: ErrorFormatter> Error<F> {
self
}

pub(crate) fn set_help_flag(mut self, help_flag: Option<&'static str>) -> Self {
pub(crate) fn set_help_flag(mut self, help_flag: Option<Cow<'static, str>>) -> Self {
self.inner.help_flag = help_flag;
self
}
Expand Down

0 comments on commit 4431d99

Please sign in to comment.