Repository: kbknapp/clap-rs
Pull request #1113 (2017-11-22 09:16:55)

Issue 1112


<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/kbknapp/clap-rs/1113)
<!-- Reviewable:end -->



Patch:
diff --git a/src/app/parser.rs b/src/app/parser.rs
index dafcf076..a5db8828 100644
--- a/src/app/parser.rs
+++ b/src/app/parser.rs
@@ -75,6 +75,7 @@ impl<'a, 'b> Parser<'a, 'b>
     pub fn with_name(n: String) -> Self {
         Parser {
             meta: AppMeta::with_name(n),
+            g_settings: AppFlags::zeroed(),
             ..Default::default()
         }
     }
@@ -1811,7 +1812,7 @@ impl<'a, 'b> Parser<'a, 'b>
         }
         Ok(())
     }
-    
+
     pub fn add_env(&mut self, matcher: &mut ArgMatcher<'a>) -> ClapResult<()> {
         macro_rules! add_val {
             ($_self:ident, $a:ident, $m:ident) => {
diff --git a/src/app/settings.rs b/src/app/settings.rs
index 916c32a1..92c685f2 100644
--- a/src/app/settings.rs
+++ b/src/app/settings.rs
@@ -67,6 +67,7 @@ impl Default for AppFlags {
 #[allow(deprecated)]
 impl AppFlags {
     pub fn new() -> Self { AppFlags::default() }
+    pub fn zeroed() -> Self { AppFlags(Flags::empty()) }
 
     impl_settings! { AppSettings,
         ArgRequiredElseHelp => Flags::A_REQUIRED_ELSE_HELP,


Test patch:
diff --git a/tests/help.rs b/tests/help.rs
index 633923b3..7cdca9ce 100644
--- a/tests/help.rs
+++ b/tests/help.rs
@@ -65,7 +65,7 @@ OPTIONS:
     -o, --opt <FILE>    tests options
 
 ARGS:
-    <PATH>    
+    <PATH>    help
 
 SUBCOMMANDS:
     help    Prints this message or the help of the given subcommand(s)
@@ -86,7 +86,7 @@ OPTIONS:
     -o, --opt <FILE>    tests options
 
 ARGS:
-    <PATH>    
+    <PATH>    help
 
 SUBCOMMANDS:
     help    Prints this message or the help of the given subcommand(s)
@@ -461,7 +461,7 @@ ARGS:
     <FIRST>...     First
     <SECOND>...    Second";
 
-static DEFAULT_HELP: &'static str = "ctest 
+static DEFAULT_HELP: &'static str = "ctest 1.0
 
 USAGE:
     ctest
@@ -490,12 +490,17 @@ FLAGS:
 ARGS:
     <arg1>    
             some option";
-#[test]
-fn help_short() {
-    let m = App::new("test")
+
+fn setup() -> App<'static, 'static> {
+    App::new("test")
         .author("Kevin K.")
         .about("tests stuff")
         .version("1.3")
+}
+
+#[test]
+fn help_short() {
+    let m = setup()
         .get_matches_from_safe(vec!["myprog", "-h"]);
 
     assert!(m.is_err());
@@ -504,10 +509,7 @@ fn help_short() {
 
 #[test]
 fn help_long() {
-    let m = App::new("test")
-        .author("Kevin K.")
-        .about("tests stuff")
-        .version("1.3")
+    let m = setup()
         .get_matches_from_safe(vec!["myprog", "--help"]);
 
     assert!(m.is_err());
@@ -516,10 +518,7 @@ fn help_long() {
 
 #[test]
 fn help_no_subcommand() {
-    let m = App::new("test")
-        .author("Kevin K.")
-        .about("tests stuff")
-        .version("1.3")
+    let m = setup() 
         .get_matches_from_safe(vec!["myprog", "help"]);
 
     assert!(m.is_err());
@@ -528,10 +527,7 @@ fn help_no_subcommand() {
 
 #[test]
 fn help_subcommand() {
-    let m = App::new("test")
-        .author("Kevin K.")
-        .about("tests stuff")
-        .version("1.3")
+    let m = setup()
         .subcommand(SubCommand::with_name("test")
             .about("tests things")
             .arg_from_usage("-v --verbose 'with verbosity'"))
@@ -540,7 +536,7 @@ fn help_subcommand() {
     assert!(m.is_err());
     assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
 }
- 
+
 #[test]
 fn req_last_arg_usage() {
     let app = clap_app!(example =>
@@ -653,7 +649,7 @@ fn no_wrap_help() {
 
 #[test]
 fn no_wrap_default_help() {
-    let app = App::new("ctest").set_term_width(0);
+    let app = App::new("ctest").version("1.0").set_term_width(0);
     assert!(test::compare_output(app, "ctest --help", DEFAULT_HELP, false));
 }
 
@@ -896,27 +892,27 @@ FLAGS:
     assert!(test::compare_output(app, "rg --help", RIPGREP_USAGE, false));
 }
 
-#[test]
-fn hidden_args() {
-    let app = App::new("prog")
-        .version("1.0")
-        .args_from_usage("-f, --flag 'testing flags'
-                          -o, --opt [FILE] 'tests options'")
-        .arg(Arg::with_name("pos").hidden(true));
-    assert!(test::compare_output(app, "prog --help", HIDDEN_ARGS, false));
-}
-
 #[test]
 fn sc_negates_reqs() {
     let app = App::new("prog")
         .version("1.0")
         .setting(AppSettings::SubcommandsNegateReqs)
         .arg_from_usage("-o, --opt <FILE> 'tests options'")
-        .arg(Arg::with_name("PATH"))
+        .arg(Arg::with_name("PATH").help("help"))
         .subcommand(SubCommand::with_name("test"));
     assert!(test::compare_output(app, "prog --help", SC_NEGATES_REQS, false));
 }
 
+#[test]
+fn hidden_args() {
+    let app = App::new("prog")
+        .version("1.0")
+        .args_from_usage("-f, --flag 'testing flags'
+                          -o, --opt [FILE] 'tests options'")
+        .arg(Arg::with_name("pos").hidden(true));
+    assert!(test::compare_output(app, "prog --help", HIDDEN_ARGS, false));
+}
+
 #[test]
 fn args_negate_sc() {
     let app = App::new("prog")
@@ -924,7 +920,7 @@ fn args_negate_sc() {
         .setting(AppSettings::ArgsNegateSubcommands)
         .args_from_usage("-f, --flag 'testing flags'
                           -o, --opt [FILE] 'tests options'")
-        .arg(Arg::with_name("PATH"))
+        .arg(Arg::with_name("PATH").help("help"))
         .subcommand(SubCommand::with_name("test"));
     assert!(test::compare_output(app, "prog --help", ARGS_NEGATE_SC, false));
 }
@@ -1030,19 +1026,52 @@ fn hidden_default_val() {
     assert!(test::compare_output(app2, "default --help", HIDE_DEFAULT_VAL, false));
 }
 
-#[test]
-fn override_help() {
-    let m = App::new("test")
+fn issue_1112_setup() -> App<'static, 'static> {
+    App::new("test")
         .author("Kevin K.")
         .about("tests stuff")
         .version("1.3")
-        .arg(Arg::from_usage("-H, --help 'some help'"))
+        .arg(Arg::from_usage("-h, --help 'some help'"))
+        .subcommand(SubCommand::with_name("foo")
+            .arg(Arg::from_usage("-h, --help 'some help'")))
+}
+
+#[test]
+fn issue_1112_override_help_long() {
+    let m = issue_1112_setup()
         .get_matches_from_safe(vec!["test", "--help"]);
 
     assert!(m.is_ok());
     assert!(m.unwrap().is_present("help"));
 }
 
+#[test]
+fn issue_1112_override_help_short() {
+    let m = issue_1112_setup()
+        .get_matches_from_safe(vec!["test", "-h"]);
+
+    assert!(m.is_ok());
+    assert!(m.unwrap().is_present("help"));
+}
+
+#[test]
+fn issue_1112_override_help_subcmd_long() {
+    let m = issue_1112_setup()
+        .get_matches_from_safe(vec!["test", "foo", "--help"]);
+
+    assert!(m.is_ok());
+    assert!(m.unwrap().subcommand_matches("foo").unwrap().is_present("help"));
+}
+
+#[test]
+fn issue_1112_override_help_subcmd_short() {
+    let m = issue_1112_setup()
+        .get_matches_from_safe(vec!["test", "foo", "-h"]);
+
+    assert!(m.is_ok());
+    assert!(m.unwrap().subcommand_matches("foo").unwrap().is_present("help"));
+}
+
 #[test]
 fn issue_1052_require_delim_help() {
     let app = App::new("test")