From 29fbfa3b963f2f3ca7704bf5d3e1201531baa373 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 2 May 2016 14:25:05 -0400 Subject: [PATCH] feat(Help): adds support for displaying info before help message Can now use the `App::before_help` method to add additional information that will be displayed prior to the help message. Common uses are copyright, or license information. --- src/app/help.rs | 8 +++++++- src/app/meta.rs | 3 +++ src/app/mod.rs | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/app/help.rs b/src/app/help.rs index 2a1d1a718d7e..7a871379ebf4 100644 --- a/src/app/help.rs +++ b/src/app/help.rs @@ -692,7 +692,8 @@ impl<'a> Help<'a> { /// * `{options}` - Help for options. /// * `{positionals}` - Help for positionals arguments. /// * `{subcommands}` - Help for subcommands. - /// * `{after-help}` - Help for flags. + /// * `{after-help}` - Info to be displayed after the help message. + /// * `{before-help}` - Info to be displayed before the help message. /// /// The template system is, on purpose, very simple. Therefore the tags have to writen /// in the lowercase and without spacing. @@ -768,6 +769,11 @@ impl<'a> Help<'a> { "{}", parser.meta.more_help.unwrap_or("unknown after-help"))); } + b"before-help" => { + try!(write!(self.writer, + "{}", + parser.meta.pre_help.unwrap_or("unknown before-help"))); + } // Unknown tag, write it back. ref r => { try!(self.writer.write(b"{")); diff --git a/src/app/meta.rs b/src/app/meta.rs index c49f46379bf1..f4eb8e1a6434 100644 --- a/src/app/meta.rs +++ b/src/app/meta.rs @@ -7,6 +7,7 @@ pub struct AppMeta<'b> { pub version: Option<&'b str>, pub about: Option<&'b str>, pub more_help: Option<&'b str>, + pub pre_help: Option<&'b str>, pub usage_str: Option<&'b str>, pub usage: Option, pub help_str: Option<&'b str>, @@ -21,6 +22,7 @@ impl<'b> Default for AppMeta<'b> { author: None, about: None, more_help: None, + pre_help: None, version: None, usage_str: None, usage: None, @@ -49,6 +51,7 @@ impl<'b> Clone for AppMeta<'b> { author: self.author, about: self.about, more_help: self.more_help, + pre_help: self.pre_help, version: self.version, usage_str: self.usage_str, usage: self.usage.clone(), diff --git a/src/app/mod.rs b/src/app/mod.rs index 0cf44fcab479..11ffc55efccb 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -189,6 +189,23 @@ impl<'a, 'b> App<'a, 'b> { self } + /// Adds additional help information to be displayed in addition to auto-generated help. This + /// information is displayed **before** the auto-generated help information. This is often used + /// for header information. + /// + /// # Examples + /// + /// ```no_run + /// # use clap::App; + /// App::new("myprog") + /// .before_help("Some info I'd like to appear before the help info") + /// # ; + /// ``` + pub fn before_help>(mut self, help: S) -> Self { + self.p.meta.pre_help = Some(help.into()); + self + } + /// Sets a string of the version number to be displayed when displaying version or help /// information. ///