Skip to content

Commit

Permalink
fix(App): using App::print_help now prints the same as would have b…
Browse files Browse the repository at this point in the history
…een 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
  • Loading branch information
kbknapp committed Jun 24, 2016
1 parent 4f805d5 commit e84cc01
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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")
Expand Down

0 comments on commit e84cc01

Please sign in to comment.