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