Skip to content

Commit

Permalink
Auto merge of #894 - porglezomp:feature/customize-version-help, r=kbk…
Browse files Browse the repository at this point in the history
…napp

Allow customizing the --version and --help messages

Fixes #889

This currently doesn't support customizing them from YAML, so it still needs some work.
  • Loading branch information
homu committed Mar 10, 2017
2 parents cb5c9f7 + c5dac3f commit 0e1e6dc
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
40 changes: 40 additions & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,44 @@ impl<'a, 'b> App<'a, 'b> {
self
}

/// Sets the help text for the auto-generated `help` argument.
///
/// By default `clap` sets this to `"Prints help information"`, but if you're using a
/// different convention for your help messages and would prefer a different phrasing you can
/// override it.
///
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg};
/// App::new("myprog")
/// .help_message("Print help information") // Perhaps you want imperative help messages
///
/// # ;
/// ```
pub fn help_message<S: Into<&'a str>>(mut self, s: S) -> Self {
self.p.help_message(s.into());
self
}

/// Sets the help text for the auto-generated `version` argument.
///
/// By default `clap` sets this to `"Prints version information"`, but if you're using a
/// different convention for your help messages and would prefer a different phrasing then you
/// can change it.
///
/// # Examples
/// ```no_run
/// # use clap::{App, Arg};
/// App::new("myprog")
/// .version_message("Print version information") // Perhaps you want imperative help messages
/// # ;
/// ```
pub fn version_message<S: Into<&'a str>>(mut self, s: S) -> Self {
self.p.version_message(s.into());
self
}

/// Sets the help template to be used, overriding the default format.
///
/// Tags arg given inside curly brackets.
Expand Down Expand Up @@ -1429,6 +1467,8 @@ impl<'a> From<&'a Yaml> for App<'a, 'a> {
yaml_str!(a, yaml, help);
yaml_str!(a, yaml, help_short);
yaml_str!(a, yaml, version_short);
yaml_str!(a, yaml, help_message);
yaml_str!(a, yaml, version_message);
yaml_str!(a, yaml, alias);
yaml_str!(a, yaml, visible_alias);

Expand Down
14 changes: 12 additions & 2 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct Parser<'a, 'b>
overrides: Vec<&'b str>,
help_short: Option<char>,
version_short: Option<char>,
help_message: Option<&'a str>,
version_message: Option<&'a str>,
}

impl<'a, 'b> Parser<'a, 'b>
Expand All @@ -79,6 +81,14 @@ impl<'a, 'b> Parser<'a, 'b>
self.version_short = Some(c);
}

pub fn help_message(&mut self, s: &'a str) {
self.help_message = Some(s);
}

pub fn version_message(&mut self, s: &'a str) {
self.version_message = Some(s);
}

pub fn gen_completions_to<W: Write>(&mut self, for_shell: Shell, buf: &mut W) {
if !self.is_set(AS::Propogated) {
self.propogate_help_version();
Expand Down Expand Up @@ -1251,7 +1261,7 @@ impl<'a, 'b> Parser<'a, 'b>
let arg = FlagBuilder {
b: Base {
name: "hclap_help",
help: Some("Prints help information"),
help: Some(self.help_message.unwrap_or("Prints help information")),
..Default::default()
},
s: Switched {
Expand All @@ -1271,7 +1281,7 @@ impl<'a, 'b> Parser<'a, 'b>
let arg = FlagBuilder {
b: Base {
name: "vclap_version",
help: Some("Prints version information"),
help: Some(self.version_message.unwrap_or("Prints version information")),
..Default::default()
},
s: Switched {
Expand Down
1 change: 1 addition & 0 deletions tests/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ about: tests clap library
author: Kevin K. <[email protected]>
settings:
- ArgRequiredElseHelp
help_message: prints help with a nonstandard description
args:
- opt:
short: o
Expand Down
24 changes: 24 additions & 0 deletions tests/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ FLAGS:
Prints version
information";

static CUSTOM_VERSION_AND_HELP: &'static str = "customize 0.1
Nobody <[email protected]>
You can customize the version and help text
USAGE:
customize
FLAGS:
-H, --help Print help information
-v, --version Print version information";

#[test]
fn help_short() {
let m = App::new("test")
Expand Down Expand Up @@ -529,3 +540,16 @@ fn issue_777_wrap_all_things() {
.set_term_width(35);
assert!(test::compare_output(app, "ctest --help", ISSUE_777, false));
}

#[test]
fn customize_version_and_help() {
let app = App::new("customize")
.version("0.1")
.author("Nobody <[email protected]>")
.about("You can customize the version and help text")
.help_short("H")
.help_message("Print help information")
.version_short("v")
.version_message("Print version information");
assert!(test::compare_output(app, "customize --help", CUSTOM_VERSION_AND_HELP, false));
}
14 changes: 14 additions & 0 deletions tests/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,17 @@ fn create_app_from_yaml() {
let yml = load_yaml!("app.yml");
App::from_yaml(yml);
}

#[test]
fn help_message() {
let yml = load_yaml!("app.yml");
let mut app = App::from_yaml(yml);
// Generate the full help message!
let _ = app.get_matches_from_safe_borrow(Vec::<String>::new());

let mut help_buffer = Vec::new();
app.write_help(&mut help_buffer).unwrap();
let help_string = String::from_utf8(help_buffer).unwrap();
assert!(help_string.contains(
"-h, --help prints help with a nonstandard description\n"));
}

0 comments on commit 0e1e6dc

Please sign in to comment.