Skip to content

Commit

Permalink
feat: Added {before/after}_help_long to App struct, closed clap-rs#1903
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaxar committed Jul 12, 2020
1 parent 9fc8ec9 commit c13ba77
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
52 changes: 52 additions & 0 deletions src/build/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ pub struct App<'b> {
pub(crate) about: Option<&'b str>,
pub(crate) long_about: Option<&'b str>,
pub(crate) before_help: Option<&'b str>,
pub(crate) long_before_help: Option<&'b str>,
pub(crate) after_help: Option<&'b str>,
pub(crate) long_after_help: Option<&'b str>,
pub(crate) aliases: Vec<(&'b str, bool)>, // (name, visible)
pub(crate) usage_str: Option<&'b str>,
pub(crate) usage: Option<String>,
Expand Down Expand Up @@ -327,6 +329,9 @@ impl<'b> App<'b> {
/// information is displayed **after** the auto-generated help information. This is often used
/// to describe how to use the arguments, or caveats to be noted.
///
/// **NOTE:** If only `long_after_help` is provided, and not [`App::after_help`] but the user requests
/// `-h` clap will still display the contents of `after_help` appropriately
///
/// # Examples
///
/// ```no_run
Expand All @@ -340,10 +345,35 @@ impl<'b> App<'b> {
self
}

/// Adds additional help information to be displayed in addition to auto-generated help. This
/// information is displayed **after** the auto-generated help information and is meant to be
/// more verbose than `after_help`. This is often used for man pages.. This is often used
/// to describe how to use the arguments, or caveats to be noted in man pages.
///
/// **NOTE:** If only `after_help` is provided, and not [`App::long_after_help`] but the user requests
/// `--help`, clap will still display the contents of `after_help` appropriately
///
/// # Examples
///
/// ```no_run
/// # use clap::App;
/// App::new("myprog")
/// .long_after_help("Does really amazing things to great people... but be careful with -R, \
/// like, for real, be careful with this!")
/// # ;
/// ```
pub fn long_after_help<S: Into<&'b str>>(mut self, help: S) -> Self {
self.long_after_help = Some(help.into());
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.
///
/// **NOTE:** If only `long_before_help` is provided, and not [`App::before_help`] but the user requests
/// `-h` clap will still display the contents of `long_before_help` appropriately
///
/// # Examples
///
/// ```no_run
Expand All @@ -357,6 +387,26 @@ impl<'b> App<'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 and is meant to be more
/// verbose than `before_help`. This is often used for header information in man pages.
///
/// **NOTE:** If only `before_help` is provided, and not [`App::long_before_help`] but the user requests
/// `--help`, clap will still display the contents of `before_help` appropriately
///
/// # Examples
///
/// ```no_run
/// # use clap::App;
/// App::new("myprog")
/// .long_before_help("Some verbose and long info I'd like to appear before the help info")
/// # ;
/// ```
pub fn long_before_help<S: Into<&'b str>>(mut self, help: S) -> Self {
self.long_before_help = Some(help.into());
self
}

/// Sets a string of the version number to be displayed when displaying version or help
/// information with `-V`.
///
Expand Down Expand Up @@ -2091,7 +2141,9 @@ impl<'a> From<&'a Yaml> for App<'a> {
yaml_str!(a, yaml, bin_name);
yaml_str!(a, yaml, about);
yaml_str!(a, yaml, before_help);
yaml_str!(a, yaml, long_before_help);
yaml_str!(a, yaml, after_help);
yaml_str!(a, yaml, long_after_help);
yaml_str!(a, yaml, alias);
yaml_str!(a, yaml, visible_alias);

Expand Down
36 changes: 34 additions & 2 deletions src/output/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,22 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
/// Writes default help for a Parser Object to the wrapped stream.
pub(crate) fn write_default_help(&mut self) -> ClapResult<()> {
debug!("Help::write_default_help");
if let Some(h) = self.parser.app.before_help {
if self.use_long {
if let Some(h) = self
.parser
.app
.long_before_help
.or_else(|| self.parser.app.before_help)
{
self.write_before_after_help(h)?;
self.none("\n\n")?;
}
} else if let Some(h) = self
.parser
.app
.before_help
.or_else(|| self.parser.app.long_before_help)
{
self.write_before_after_help(h)?;
self.none("\n\n")?;
}
Expand Down Expand Up @@ -867,7 +882,24 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
self.write_all_args()?;
}

if let Some(h) = self.parser.app.after_help {
if self.use_long {
if let Some(h) = self
.parser
.app
.long_after_help
.or_else(|| self.parser.app.after_help)
{
if flags || opts || pos || subcmds {
self.none("\n\n")?;
}
self.write_before_after_help(h)?;
}
} else if let Some(h) = self
.parser
.app
.after_help
.or_else(|| self.parser.app.long_after_help)
{
if flags || opts || pos || subcmds {
self.none("\n\n")?;
}
Expand Down
2 changes: 2 additions & 0 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,8 @@ where
};

self.app.long_about.is_some()
|| self.app.long_before_help.is_some()
|| self.app.long_after_help.is_some()
|| self.app.args.args.iter().any(should_long)
|| self.app.subcommands.iter().any(|s| s.long_about.is_some())
}
Expand Down
47 changes: 47 additions & 0 deletions tests/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ FLAGS:
some text that comes after the help";

static LONG_AFTER_HELP: &str = "some longer text that comes before the help
clap-test v1.4.8
tests clap library
USAGE:
clap-test
FLAGS:
-h, --help
Prints help information
-V, --version
Prints version information
some longer text that comes after the help";

static HIDDEN_ARGS: &str = "prog 1.0
USAGE:
Expand Down Expand Up @@ -714,6 +732,12 @@ fn after_and_before_help_output() {
.about("tests clap library")
.before_help("some text that comes before the help")
.after_help("some text that comes after the help");
assert!(utils::compare_output(
app.clone(),
"clap-test -h",
AFTER_HELP,
false
));
assert!(utils::compare_output(
app,
"clap-test --help",
Expand All @@ -722,6 +746,29 @@ fn after_and_before_help_output() {
));
}

#[test]
fn long_after_and_before_help_output() {
let app = App::new("clap-test")
.version("v1.4.8")
.about("tests clap library")
.before_help("some text that comes before the help")
.after_help("some text that comes after the help")
.long_before_help("some longer text that comes before the help")
.long_after_help("some longer text that comes after the help");
assert!(utils::compare_output(
app.clone(),
"clap-test --help",
LONG_AFTER_HELP,
false
));
assert!(utils::compare_output(
app,
"clap-test -h",
AFTER_HELP,
false
));
}

#[test]
fn multi_level_sc_help() {
let app = App::new("ctest").subcommand(
Expand Down

0 comments on commit c13ba77

Please sign in to comment.