Make App's help heading a priority, optimize it

This commit is contained in:
Artem Vorotnikov 2020-06-01 08:39:58 +03:00
parent 2de5c0e44c
commit fc8a0bba9c
No known key found for this signature in database
GPG key ID: E0148C3F2FBB7A20

View file

@ -94,7 +94,7 @@ pub struct App<'b> {
pub(crate) subcommands: Vec<App<'b>>,
pub(crate) replacers: HashMap<&'b str, &'b [&'b str]>,
pub(crate) groups: Vec<ArgGroup<'b>>,
pub(crate) help_headings: Vec<Option<&'b str>>,
pub(crate) current_help_heading: Option<&'b str>,
}
impl<'b> App<'b> {
@ -690,14 +690,9 @@ impl<'b> App<'b> {
/// ```
/// [argument]: ./struct.Arg.html
pub fn arg<A: Into<Arg<'b>>>(mut self, a: A) -> Self {
let help_heading: Option<&'b str> = if let Some(option_str) = self.help_headings.last() {
*option_str
} else {
None
};
let mut arg = a.into();
if arg.get_help_heading().is_none() {
arg = arg.help_heading(help_heading);
if let Some(help_heading) = self.current_help_heading {
arg = arg.help_heading(Some(help_heading));
}
self.args.push(arg);
self
@ -708,14 +703,14 @@ impl<'b> App<'b> {
/// call to help_heading
#[inline]
pub fn help_heading(mut self, heading: &'b str) -> Self {
self.help_headings.push(Some(heading));
self.current_help_heading = Some(heading);
self
}
/// Stop using custom section headings.
#[inline]
pub fn stop_custom_headings(mut self) -> Self {
self.help_headings.push(None);
self.current_help_heading = None;
self
}