Repository: kbknapp/clap-rs (rust)

Issue:
Easily replace only the messages for --help and --version
I'm trying to help make a project's help messages consistent, and all of the messages are of the format "Start …", "Check …", "Print …", so the "Prints  version information" stands out. I was unable to find an easy way to replace only the help and version messages without re-wiring the commands themselves. Is there any easy solution I missed, or is this something that would have to be added.

Pull request description:
Allow customizing the --version and --help messages
Fixes #889 

This currently doesn't support customizing them from YAML, so it still needs some work.

Patch:
diff --git a/src/app/mod.rs b/src/app/mod.rs
index 67683f0e..5c25dc89 100644
--- a/src/app/mod.rs
+++ b/src/app/mod.rs
@@ -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.
@@ -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);
 
diff --git a/src/app/parser.rs b/src/app/parser.rs
index 9253b5df..1294df47 100644
--- a/src/app/parser.rs
+++ b/src/app/parser.rs
@@ -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>
@@ -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();
@@ -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 {
@@ -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 {


Test patch:
diff --git a/tests/app.yml b/tests/app.yml
index 48018320..c34ca1bf 100644
--- a/tests/app.yml
+++ b/tests/app.yml
@@ -4,6 +4,7 @@ about: tests clap library
 author: Kevin K. <kbknapp@gmail.com>
 settings:
     - ArgRequiredElseHelp
+help_message: prints help with a nonstandard description
 args:
     - opt:
         short: o
diff --git a/tests/help.rs b/tests/help.rs
index 2390458e..63266442 100644
--- a/tests/help.rs
+++ b/tests/help.rs
@@ -225,6 +225,17 @@ FLAGS:
             Prints version
             information";
 
+static CUSTOM_VERSION_AND_HELP: &'static str = "customize 0.1
+Nobody <odysseus@example.com>
+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")
@@ -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 <odysseus@example.com>")
+        .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));
+}
diff --git a/tests/yaml.rs b/tests/yaml.rs
index ba2429a9..968489a7 100644
--- a/tests/yaml.rs
+++ b/tests/yaml.rs
@@ -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"));
+}