mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
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
This commit is contained in:
parent
4f805d53e7
commit
e84cc01836
2 changed files with 7 additions and 9 deletions
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue