mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 15:27:16 +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]
|
#[inline]
|
||||||
pub fn has_visible_subcommands(&self) -> bool {
|
pub fn has_visible_subcommands(&self) -> bool {
|
||||||
if self.subcommands.is_empty() { return false; }
|
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]
|
#[inline]
|
||||||
|
@ -2120,7 +2120,7 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
if let Some(u) = self.meta.usage_str {
|
if let Some(u) = self.meta.usage_str {
|
||||||
usage.push_str(&*u);
|
usage.push_str(&*u);
|
||||||
} else if used.is_empty() {
|
} else if used.is_empty() {
|
||||||
usage.push_str(&*self.meta
|
let name = self.meta
|
||||||
.usage
|
.usage
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
|
@ -2128,7 +2128,8 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
.bin_name
|
.bin_name
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap_or(&self.meta.name)
|
.unwrap_or(&self.meta.name)
|
||||||
}));
|
});
|
||||||
|
usage.push_str(&*name);
|
||||||
let mut reqs: Vec<&str> = self.required().map(|r| &**r).collect();
|
let mut reqs: Vec<&str> = self.required().map(|r| &**r).collect();
|
||||||
reqs.dedup();
|
reqs.dedup();
|
||||||
let req_string = self.get_required_from(&reqs, None, None)
|
let req_string = self.get_required_from(&reqs, None, None)
|
||||||
|
@ -2141,9 +2142,9 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
} else if flags {
|
} else if flags {
|
||||||
usage.push_str(" [OPTIONS]");
|
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) &&
|
self.opts.iter().any(|o| !o.is_set(ArgSettings::Required) &&
|
||||||
!o.is_set!(AS::Hidden)) {
|
!o.is_set(ArgSettings::Hidden)) {
|
||||||
usage.push_str(" [OPTIONS]");
|
usage.push_str(" [OPTIONS]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2154,7 +2155,7 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
if self.has_positionals() &&
|
if self.has_positionals() &&
|
||||||
self.opts.iter().any(|o| o.is_set(ArgSettings::Multiple)) &&
|
self.opts.iter().any(|o| o.is_set(ArgSettings::Multiple)) &&
|
||||||
self.positionals.values().any(|p| !p.is_set(ArgSettings::Required)) &&
|
self.positionals.values().any(|p| !p.is_set(ArgSettings::Required)) &&
|
||||||
!self.has_subcommands() {
|
!self.has_visible_subcommands() {
|
||||||
usage.push_str(" [--]")
|
usage.push_str(" [--]")
|
||||||
}
|
}
|
||||||
if self.has_positionals() &&
|
if self.has_positionals() &&
|
||||||
|
@ -2168,11 +2169,19 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if self.has_subcommands() && !self.is_set(AS::SubcommandRequired) {
|
if self.is_set(AS::SubcommandsNegateReqs) || self.is_set(AS::ArgsNegateSubcommands) {
|
||||||
usage.push_str(" [SUBCOMMAND]");
|
if self.has_visible_subcommands() {
|
||||||
} else if (self.is_set(AS::SubcommandRequired) ||
|
usage.push_str("\n ");
|
||||||
self.is_set(AS::SubcommandRequiredElseHelp)) && self.has_subcommands() {
|
usage.push_str(&*name);
|
||||||
usage.push_str(" <SUBCOMMAND>");
|
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 {
|
} else {
|
||||||
self.smart_usage(&mut usage, used);
|
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("<FILE> 'some file'")
|
||||||
.arg_from_usage("[FILES]... 'some file'")
|
.arg_from_usage("[FILES]... 'some file'")
|
||||||
.get_matches_from(vec!["test", "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]
|
#[test]
|
||||||
|
|
Loading…
Add table
Reference in a new issue