Skip to content

Commit

Permalink
feat(Help): adds support for displaying info before help message
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kbknapp committed May 3, 2016
1 parent 9fdad2e commit d8e4dbc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/app/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"{"));
Expand Down
3 changes: 3 additions & 0 deletions src/app/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub help_str: Option<&'b str>,
Expand All @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
17 changes: 17 additions & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S: Into<&'b str>>(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.
///
Expand Down

0 comments on commit d8e4dbc

Please sign in to comment.