Update default help triggers

This commit is contained in:
topjohnwu
2025-10-18 17:18:02 -07:00
committed by John Wu
parent b72ba6759e
commit c94bd49a89
2 changed files with 17 additions and 9 deletions

View File

@@ -704,7 +704,7 @@ pub fn parse_struct_args(
'parse_args: while let Some(&next_arg) = remaining_args.first() {
remaining_args = &remaining_args[1..];
if (parse_options.help_triggers.contains(&next_arg)) && !options_ended {
if (parse_options.help_triggers().contains(&next_arg)) && !options_ended {
help = true;
continue;
}
@@ -761,7 +761,7 @@ pub struct ParseStructOptions<'a> {
pub help_triggers: &'a [&'a str],
}
impl ParseStructOptions<'_> {
impl<'a> ParseStructOptions<'a> {
/// Parse a commandline option.
///
/// `arg`: the current option argument being parsed (e.g. `--foo`).
@@ -772,7 +772,7 @@ impl ParseStructOptions<'_> {
.arg_to_slot
.iter()
.find_map(|&(name, pos)| if name == arg { Some(pos) } else { None })
.ok_or_else(|| unrecognized_argument(arg, self.arg_to_slot, self.help_triggers))?;
.ok_or_else(|| unrecognized_argument(arg, self.arg_to_slot, self.help_triggers()))?;
match self.slots[pos] {
ParseStructOption::Flag(ref mut b) => b.set_flag(arg),
@@ -798,6 +798,14 @@ impl ParseStructOptions<'_> {
Ok(())
}
fn help_triggers(&self) -> &'a [&'a str] {
if self.help_triggers.is_empty() {
&["-h", "--help"]
} else {
self.help_triggers
}
}
}
fn unrecognized_argument(

View File

@@ -425,11 +425,12 @@ fn impl_from_args_struct_from_args<'a>(
/// get help triggers vector from type_attrs.help_triggers as a [`Vec<String>`]
///
/// Defaults to vec!["--help", "help"] if type_attrs.help_triggers is None
/// Defaults to vec!["-h", "--help"] if type_attrs.help_triggers is None
fn get_help_triggers(type_attrs: &TypeAttrs) -> Vec<String> {
type_attrs.help_triggers.as_ref().map_or_else(
|| vec!["--help".to_owned(), "help".to_owned()],
|s| {
type_attrs
.help_triggers
.as_ref()
.map_or_else(Vec::new, |s| {
s.iter()
.filter_map(|s| {
let trigger = s.value();
@@ -441,8 +442,7 @@ fn get_help_triggers(type_attrs: &TypeAttrs) -> Vec<String> {
}
})
.collect::<Vec<_>>()
},
)
})
}
/// Ensures that only the last positional arg is non-required.