Skip to content

Commit

Permalink
Merge pull request #1048 from kbknapp/issues-1046,1047
Browse files Browse the repository at this point in the history
Issues 1046,1047
  • Loading branch information
kbknapp authored Sep 15, 2017
2 parents 326d0a5 + f093b4c commit 6e94899
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "clap"
version = "2.26.1"
version = "2.26.2"
authors = ["Kevin K. <[email protected]>"]
exclude = ["examples/*", "clap-test/*", "tests/*", "benches/*", "*.png", "clap-perf/*", "*.dot"]
repository = "https://github.com/kbknapp/clap-rs.git"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/app/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 9 additions & 5 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()) {
Expand Down
27 changes: 27 additions & 0 deletions tests/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
17 changes: 17 additions & 0 deletions tests/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

0 comments on commit 6e94899

Please sign in to comment.