Use a different default template when there are no args.

This eliminates extraneous empty lines when there are no user-defined
args, the default args are disabled, and `after_help` is set.
This commit is contained in:
Matt Kantor 2020-08-14 13:06:15 -07:00
parent de6a5af1b2
commit 85d3daa8c1
3 changed files with 25 additions and 1 deletions

View file

@ -2454,6 +2454,11 @@ impl<'help> App<'help> {
!self.args.is_empty()
}
#[inline]
pub(crate) fn has_opts(&self) -> bool {
self.get_opts_no_heading().count() > 0
}
#[inline]
pub(crate) fn has_flags(&self) -> bool {
self.get_flags_no_heading().count() > 0

View file

@ -82,6 +82,12 @@ impl<'help, 'app, 'parser, 'writer> Help<'help, 'app, 'parser, 'writer> {
{all-args}{after-help}\
";
const DEFAULT_NO_ARGS_TEMPLATE: &'static str = "\
{before-help}{bin} {version}\n\
{author-with-newline}{about-with-newline}\n\
USAGE:\n {usage}{after-help}\
";
/// Create a new `Help` instance.
pub(crate) fn new(
w: HelpWriter<'writer>,
@ -124,7 +130,16 @@ impl<'help, 'app, 'parser, 'writer> Help<'help, 'app, 'parser, 'writer> {
} else if let Some(tmpl) = self.parser.app.template {
self.write_templated_help(tmpl)?;
} else {
self.write_templated_help(Self::DEFAULT_TEMPLATE)?;
let flags = self.parser.has_flags();
let pos = self.parser.has_positionals();
let opts = self.parser.has_opts();
let subcmds = self.parser.has_subcommands();
if flags || opts || pos || subcmds {
self.write_templated_help(Self::DEFAULT_TEMPLATE)?;
} else {
self.write_templated_help(Self::DEFAULT_NO_ARGS_TEMPLATE)?;
}
}
self.none("\n")?;

View file

@ -1790,6 +1790,10 @@ impl<'help, 'app> Parser<'help, 'app> {
self.app.has_args()
}
pub(crate) fn has_opts(&self) -> bool {
self.app.has_opts()
}
pub(crate) fn has_flags(&self) -> bool {
self.app.has_flags()
}