Skip to content

Commit

Permalink
imp: when AppSettings::SubcommandsNegateReqs and ArgsNegateSubcommand…
Browse files Browse the repository at this point in the history
…s are used, a new more accurate double line usage string is shown

Closes #871
  • Loading branch information
kbknapp committed Mar 4, 2017
1 parent ce5ee5f commit 50f0230
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 20 additions & 11 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,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.is_set(AppSettings::Hidden))
self.subcommands.iter().any(|s| !s.p.is_set(AS::Hidden))
}

#[inline]
Expand Down Expand Up @@ -2118,15 +2118,16 @@ impl<'a, 'b> Parser<'a, 'b>
if let Some(u) = self.meta.usage_str {
usage.push_str(&*u);
} else if used.is_empty() {
usage.push_str(&*self.meta
let name = self.meta
.usage
.as_ref()
.unwrap_or_else(|| {
self.meta
.bin_name
.as_ref()
.unwrap_or(&self.meta.name)
}));
});
usage.push_str(&*name);
let mut reqs: Vec<&str> = self.required().map(|r| &**r).collect();
reqs.dedup();
let req_string = self.get_required_from(&reqs, None, None)
Expand All @@ -2139,9 +2140,9 @@ impl<'a, 'b> Parser<'a, 'b>
} else if flags {
usage.push_str(" [OPTIONS]");
}
if !self.is_set(AS::UnifiedHelpMessage) && self.has_opts() &&
if !self.is_set(AS::UnifiedHelpMessage) &&
self.opts.iter().any(|o| !o.is_set(ArgSettings::Required) &&
!o.is_set!(AS::Hidden)) {
!o.is_set(ArgSettings::Hidden)) {
usage.push_str(" [OPTIONS]");
}

Expand All @@ -2152,7 +2153,7 @@ impl<'a, 'b> Parser<'a, 'b>
if self.has_positionals() &&
self.opts.iter().any(|o| o.is_set(ArgSettings::Multiple)) &&
self.positionals.values().any(|p| !p.is_set(ArgSettings::Required)) &&
!self.has_subcommands() {
!self.has_visible_subcommands() {
usage.push_str(" [--]")
}
if self.has_positionals() &&
Expand All @@ -2166,11 +2167,19 @@ impl<'a, 'b> Parser<'a, 'b>
}


if self.has_subcommands() && !self.is_set(AS::SubcommandRequired) {
usage.push_str(" [SUBCOMMAND]");
} else if (self.is_set(AS::SubcommandRequired) ||
self.is_set(AS::SubcommandRequiredElseHelp)) && self.has_subcommands() {
usage.push_str(" <SUBCOMMAND>");
if self.is_set(AS::SubcommandsNegateReqs) || self.is_set(AS::ArgsNegateSubcommands) {
if self.has_visible_subcommands() {
usage.push_str("\n ");
usage.push_str(&*name);
usage.push_str(" <SUBCOMMAND>");
}
} else {
if self.has_visible_subcommands() && !self.is_set(AS::SubcommandRequired) {
usage.push_str(" [SUBCOMMAND]");
} else if (self.is_set(AS::SubcommandRequired) ||
self.is_set(AS::SubcommandRequiredElseHelp)) && self.has_subcommands() {
usage.push_str(" <SUBCOMMAND>");
}
}
} else {
self.smart_usage(&mut usage, used);
Expand Down
2 changes: 1 addition & 1 deletion tests/positionals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ fn multiple_positional_one_required_usage_string() {
.arg_from_usage("<FILE> 'some file'")
.arg_from_usage("[FILES]... 'some file'")
.get_matches_from(vec!["test", "file"]);
assert_eq!(m.usage(), "USAGE:\n test <FILE> [ARGS]");
assert_eq!(m.usage(), "USAGE:\n test <FILE> [FILES]...");
}

#[test]
Expand Down

0 comments on commit 50f0230

Please sign in to comment.