Repository: kbknapp/clap-rs
Pull request #1048 (2017-09-14 17:39:54)

Issues 1046,1047


<!-- 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/1048)
<!-- Reviewable:end -->



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 99e16d31..ecdc381c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,17 @@
+<a name="v2.26.2"></a>
+### v2.26.2 (2017-09-14)
+
+
+#### Improvements
+
+*   if all subcommands are hidden, the subcommands section of the help message is no longer displayed ([4ae7b046](https://github.com/kbknapp/clap-rs/commit/4ae7b0464750bc07ec80ece38e43f003fdd1b8ae), closes [#1046](https://github.com/kbknapp/clap-rs/issues/1046))
+
+#### Bug Fixes
+
+*   fixes a bug where default values are not applied if the option supports zero values ([9c248cbf](https://github.com/kbknapp/clap-rs/commit/9c248cbf7d8a825119bc387c23e9a1d1989682b0), closes [#1047](https://github.com/kbknapp/clap-rs/issues/1047))
+
+
+
 <a name="v2.26.1"></a>
 ### v2.26.1 (2017-09-14)
 
diff --git a/Cargo.toml b/Cargo.toml
index fec6f56b..bcb684bb 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,7 +1,7 @@
 [package]
 
 name = "clap"
-version = "2.26.1"
+version = "2.26.2"
 authors = ["Kevin K. <kbknapp@gmail.com>"]
 exclude = ["examples/*", "clap-test/*", "tests/*", "benches/*", "*.png", "clap-perf/*", "*.dot"]
 repository = "https://github.com/kbknapp/clap-rs.git"
diff --git a/README.md b/README.md
index e9c1b0af..6a42faee 100644
--- a/README.md
+++ b/README.md
@@ -45,8 +45,10 @@ Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)
 
 ## What's New
 
-Here's what's new in 2.26.1:
+Here's what's new in 2.26.2:
 
+*   if all subcommands are hidden, the subcommands section of the help message is no longer displayed 
+*   fixes a bug where default values are not applied if the option supports zero values 
 *   fixes using require_equals(true) and min_values(0) together 
 *   escape special characters in zsh and fish completions 
 *   avoid panic generating default help msg if term width set to 0 due to bug in textwrap 0.7.0 
diff --git a/src/app/help.rs b/src/app/help.rs
index 223e7ae5..6c2941b5 100644
--- a/src/app/help.rs
+++ b/src/app/help.rs
@@ -521,7 +521,7 @@ impl<'a> Help<'a> {
             .filter(|arg| !arg.is_set(ArgSettings::Hidden))
             .count() > 0;
         let opts = parser.has_opts();
-        let subcmds = parser.has_subcommands();
+        let subcmds = parser.has_visible_subcommands();
 
         let unified_help = parser.is_set(AppSettings::UnifiedHelpMessage);
 
diff --git a/src/app/parser.rs b/src/app/parser.rs
index bc5f43e4..6ec72034 100644
--- a/src/app/parser.rs
+++ b/src/app/parser.rs
@@ -475,10 +475,7 @@ impl<'a, 'b> Parser<'a, 'b>
 
     #[inline]
     pub fn has_visible_subcommands(&self) -> bool {
-        if self.subcommands.is_empty() {
-            return false;
-        }
-        self.subcommands.iter().any(|s| !s.p.is_set(AS::Hidden))
+        self.has_subcommands() && self.subcommands.iter().filter(|sc| sc.p.meta.name != "help").any(|sc| !sc.p.is_set(AS::Hidden))
     }
 
     #[inline]
@@ -1734,7 +1731,14 @@ impl<'a, 'b> Parser<'a, 'b>
         macro_rules! add_val {
             (@default $_self:ident, $a:ident, $m:ident) => {
                 if let Some(ref val) = $a.v.default_val {
-                    if $m.get($a.b.name).is_none() {
+                    if $m.get($a.b.name).map(|ma| ma.vals.len()).map(|len| len == 0).unwrap_or(false) {
+                        $_self.add_val_to_arg($a, OsStr::new(val), $m)?;
+
+                        if $_self.cache.map_or(true, |name| name != $a.name()) {
+                            arg_post_processing!($_self, $a, $m);
+                            $_self.cache = Some($a.name());
+                        }
+                    } else {
                         $_self.add_val_to_arg($a, OsStr::new(val), $m)?;
 
                         if $_self.cache.map_or(true, |name| name != $a.name()) {


Test patch:
diff --git a/tests/help.rs b/tests/help.rs
index d92a56a5..945156df 100644
--- a/tests/help.rs
+++ b/tests/help.rs
@@ -124,6 +124,22 @@ OPTIONS:
 ARGS:
     <scpositional>    tests positionals";
 
+static ISSUE_1046_HIDDEN_SCS: &'static str = "prog 1.0
+
+USAGE:
+    prog [FLAGS] [OPTIONS] [PATH]
+
+FLAGS:
+    -f, --flag       testing flags
+    -h, --help       Prints help information
+    -V, --version    Prints version information
+
+OPTIONS:
+    -o, --opt <FILE>    tests options
+
+ARGS:
+    <PATH>    some";
+
 // Using number_of_values(1) with multiple(true) misaligns help message
 static ISSUE_760: &'static str = "ctest 0.1
 
@@ -899,6 +915,17 @@ fn args_negate_sc() {
     assert!(test::compare_output(app, "prog --help", ARGS_NEGATE_SC, false));
 }
 
+#[test]
+fn issue_1046_hidden_scs() {
+    let app = App::new("prog")
+        .version("1.0")
+        .args_from_usage("-f, --flag 'testing flags'
+                          -o, --opt [FILE] 'tests options'")
+        .arg(Arg::with_name("PATH").help("some"))
+        .subcommand(SubCommand::with_name("test").setting(AppSettings::Hidden));
+    assert!(test::compare_output(app, "prog --help", ISSUE_1046_HIDDEN_SCS, false));
+}
+
 #[test]
 fn issue_777_wrap_all_things() {
     let app = App::new("A app with a crazy very long long long name hahaha")
diff --git a/tests/opts.rs b/tests/opts.rs
index 6dccb94b..8a2deee9 100644
--- a/tests/opts.rs
+++ b/tests/opts.rs
@@ -392,4 +392,21 @@ fn issue_665() {
 
     assert!(res.is_err());
     assert_eq!(res.unwrap_err().kind, ErrorKind::EmptyValue);
+}
+
+#[test]
+fn issue_1047_min_zero_vals_default_val() {
+    let m = App::new("foo")
+        .arg(
+            Arg::with_name("del")
+                .short("d")
+                .long("del")
+                .takes_value(true)
+                .require_equals(true)
+                .min_values(0)
+                .default_value("default"),
+        )
+        .get_matches_from(vec!["foo", "-d"]);
+    assert_eq!(m.occurrences_of("del"), 1);
+    assert_eq!(m.value_of("del"), Some("default"));
 }
\ No newline at end of file