From e84cc01836bbe0527e97de6db9889bd9e0fd6ba1 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Fri, 24 Jun 2016 00:26:07 -0400 Subject: [PATCH] fix(App): using `App::print_help` now prints the same as would have been printed by `--help` or the like Using `App::print_help` before wasn't building the default `--help` and `--version` flags properly. This has now been fixed. Closes #536 --- src/app/mod.rs | 5 +++-- src/app/parser.rs | 11 ++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/app/mod.rs b/src/app/mod.rs index f8006adf..0227102c 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -895,12 +895,13 @@ impl<'a, 'b> App<'a, 'b> { /// /// ```rust /// # use clap::App; - /// let app = App::new("myprog"); + /// let mut app = App::new("myprog"); /// app.print_help(); /// ``` /// [`io::stdout()`]: https://doc.rust-lang.org/std/io/fn.stdout.html /// [`BufWriter`]: https://doc.rust-lang.org/std/io/struct.BufWriter.html - pub fn print_help(&self) -> ClapResult<()> { + pub fn print_help(&mut self) -> ClapResult<()> { + self.p.create_help_and_version(); let out = io::stdout(); let mut buf_w = BufWriter::new(out.lock()); self.write_help(&mut buf_w) diff --git a/src/app/parser.rs b/src/app/parser.rs index 9bdb9d64..00f61ed7 100644 --- a/src/app/parser.rs +++ b/src/app/parser.rs @@ -967,10 +967,10 @@ impl<'a, 'b> Parser<'a, 'b> args.iter().map(|s| *s).collect() } - fn create_help_and_version(&mut self) { + pub fn create_help_and_version(&mut self) { debugln!("fn=create_help_and_version;"); // name is "hclap_help" because flags are sorted by name - if !self.flags.iter().any(|a| a.long.is_some() && a.long.unwrap() == "help") { + if self.is_set(AppSettings::NeedsLongHelp) { debugln!("Building --help"); if self.help_short.is_none() && !self.short_list.contains(&'h') { self.help_short = Some('h'); @@ -986,7 +986,7 @@ impl<'a, 'b> Parser<'a, 'b> self.flags.push(arg); } if !self.settings.is_set(AppSettings::DisableVersion) && - !self.flags.iter().any(|a| a.long.is_some() && a.long.unwrap() == "version") { + self.is_set(AppSettings::NeedsLongVersion) { debugln!("Building --version"); if self.version_short.is_none() && !self.short_list.contains(&'V') { self.version_short = Some('V'); @@ -1002,10 +1002,7 @@ impl<'a, 'b> Parser<'a, 'b> self.long_list.push("version"); self.flags.push(arg); } - if !self.subcommands.is_empty() && - !self.subcommands - .iter() - .any(|s| &s.p.meta.name[..] == "help") { + if !self.subcommands.is_empty() && self.is_set(AppSettings::NeedsSubcommandHelp) { debugln!("Building help"); self.subcommands .push(App::new("help")