mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 06:12:40 +00:00
imp: when AppSettings::SubcommandsNegateReqs and ArgsNegateSubcommands are used, a new more accurate double line usage string is shown
Closes #871
This commit is contained in:
parent
d63d404e5e
commit
c8ab24bafa
2 changed files with 21 additions and 12 deletions
|
@ -578,7 +578,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]
|
||||
|
@ -2120,7 +2120,7 @@ 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(|| {
|
||||
|
@ -2128,7 +2128,8 @@ impl<'a, 'b> Parser<'a, 'b>
|
|||
.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)
|
||||
|
@ -2141,9 +2142,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]");
|
||||
}
|
||||
|
||||
|
@ -2154,7 +2155,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() &&
|
||||
|
@ -2168,11 +2169,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);
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue