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:
Kevin K 2016-06-03 23:14:02 -04:00
parent 17bc17c699
commit ec86f2dada
2 changed files with 31 additions and 1 deletions

View file

@ -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]");
}
}

View file

@ -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> {