mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 14:54:15 +00:00
imp(Usage Strings): improves the default usage string when only a single positional arg is present
Instead of blindly printing `[ARGS]` when only a single positional arg is present, it will now print `[NAME]` (or `[NAME]...` for multiple values allowed) Closes #518
This commit is contained in:
parent
17bc17c699
commit
ec86f2dada
2 changed files with 31 additions and 1 deletions
|
@ -1514,7 +1514,17 @@ impl<'a, 'b> Parser<'a, 'b>
|
|||
}
|
||||
if self.has_positionals() &&
|
||||
self.positionals.values().any(|a| !a.settings.is_set(ArgSettings::Required)) {
|
||||
usage.push_str(" [ARGS]");
|
||||
if self.positionals.len() == 1 {
|
||||
let p = self.positionals.values().next().expect(INTERNAL_ERROR_MSG);
|
||||
if !self.groups.values().any(|g| g.args.iter().any(|a| a == &p.name)) {
|
||||
usage.push_str(&*format!(" [{}]{}", p.name_no_brackets(),
|
||||
p.multiple_str()));
|
||||
} else {
|
||||
usage.push_str(" [ARGS]");
|
||||
}
|
||||
} else {
|
||||
usage.push_str(" [ARGS]");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use std::fmt::{Display, Formatter, Result};
|
||||
use std::result::Result as StdResult;
|
||||
use std::rc::Rc;
|
||||
use std::borrow::Cow;
|
||||
|
||||
use vec_map::VecMap;
|
||||
|
||||
|
@ -108,6 +109,25 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
|
|||
}
|
||||
pb
|
||||
}
|
||||
|
||||
pub fn multiple_str(&self) -> &str {
|
||||
if self.settings.is_set(ArgSettings::Multiple) && self.val_names.is_none() {
|
||||
"..."
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name_no_brackets(&self) -> Cow<str> {
|
||||
if let Some(ref names) = self.val_names {
|
||||
Cow::Owned(names.values()
|
||||
.map(|n| format!("<{}>", n))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" "))
|
||||
} else {
|
||||
Cow::Borrowed(self.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'n, 'e> Display for PosBuilder<'n, 'e> {
|
||||
|
|
Loading…
Reference in a new issue