diff --git a/native/src/base/argh.rs b/native/src/base/argh.rs index 4d4a2cf14..995e0aec6 100644 --- a/native/src/base/argh.rs +++ b/native/src/base/argh.rs @@ -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( diff --git a/native/src/base/derive/argh/mod.rs b/native/src/base/derive/argh/mod.rs index 8cdab859a..2ce71d87f 100644 --- a/native/src/base/derive/argh/mod.rs +++ b/native/src/base/derive/argh/mod.rs @@ -425,11 +425,12 @@ fn impl_from_args_struct_from_args<'a>( /// get help triggers vector from type_attrs.help_triggers as a [`Vec`] /// -/// 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 { - 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 { } }) .collect::>() - }, - ) + }) } /// Ensures that only the last positional arg is non-required.