imp: args that have require_delimiter(true) is now reflected in help and usage strings

Closes #1052
This commit is contained in:
Kevin K 2017-10-12 15:11:16 -07:00
parent e36ca736ff
commit dce616998e
No known key found for this signature in database
GPG key ID: 17218E4B3692F01A
3 changed files with 30 additions and 6 deletions

View file

@ -14,6 +14,7 @@ use errors::{Error, Result as ClapResult};
use fmt::{Colorizer, ColorizerOption, Format};
use app::usage;
use map::VecMap;
use INTERNAL_ERROR_MSG;
// Third Party
use unicode_width::UnicodeWidthStr;
@ -315,12 +316,17 @@ impl<'a> Help<'a> {
fn val<'b, 'c>(&mut self, arg: &ArgWithDisplay<'b, 'c>) -> Result<String, io::Error> {
debugln!("Help::val: arg={}", arg);
if arg.takes_value() {
let delim = if arg.is_set(ArgSettings::RequireDelimiter) {
arg.val_delim().expect(INTERNAL_ERROR_MSG)
} else {
' '
};
if let Some(vec) = arg.val_names() {
let mut it = vec.iter().peekable();
while let Some((_, val)) = it.next() {
color!(self, "<{}>", val, good)?;
if it.peek().is_some() {
write!(self.writer, " ")?;
write!(self.writer, "{}", delim)?;
}
}
let num = vec.len();
@ -332,7 +338,7 @@ impl<'a> Help<'a> {
while let Some(_) = it.next() {
color!(self, "<{}>", arg.name(), good)?;
if it.peek().is_some() {
write!(self.writer, " ")?;
write!(self.writer, "{}", delim)?;
}
}
if arg.is_set(ArgSettings::Multiple) && num == 1 {

View file

@ -8,6 +8,7 @@ use std::mem;
// Internal
use args::{AnyArg, Arg, ArgSettings, Base, DispOrder, Switched, Valued};
use map::{self, VecMap};
use INTERNAL_ERROR_MSG;
#[allow(missing_debug_implementations)]
#[doc(hidden)]
@ -65,6 +66,11 @@ impl<'n, 'e> Display for OptBuilder<'n, 'e> {
} else {
write!(f, "-{}{}", self.s.short.unwrap(), sep)?;
}
let delim = if self.is_set(ArgSettings::RequireDelimiter) {
self.v.val_delim.expect(INTERNAL_ERROR_MSG)
} else {
' '
};
// Write the values such as <name1> <name2>
if let Some(ref vec) = self.v.val_names {
@ -72,7 +78,7 @@ impl<'n, 'e> Display for OptBuilder<'n, 'e> {
while let Some((_, val)) = it.next() {
write!(f, "<{}>", val)?;
if it.peek().is_some() {
write!(f, " ")?;
write!(f, "{}", delim)?;
}
}
let num = vec.len();
@ -84,7 +90,7 @@ impl<'n, 'e> Display for OptBuilder<'n, 'e> {
while let Some(_) = it.next() {
write!(f, "<{}>", self.b.name)?;
if it.peek().is_some() {
write!(f, " ")?;
write!(f, "{}", delim)?;
}
}
if self.is_set(ArgSettings::Multiple) && num == 1 {

View file

@ -74,6 +74,12 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
pub fn name_no_brackets(&self) -> Cow<str> {
debugln!("PosBuilder::name_no_brackets;");
let mut delim = String::new();
delim.push(if self.is_set(ArgSettings::RequireDelimiter) {
self.v.val_delim.expect(INTERNAL_ERROR_MSG)
} else {
' '
});
if let Some(ref names) = self.v.val_names {
debugln!("PosBuilder:name_no_brackets: val_names={:#?}", names);
if names.len() > 1 {
@ -82,7 +88,7 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
.values()
.map(|n| format!("<{}>", n))
.collect::<Vec<_>>()
.join(" "),
.join(&&*delim),
)
} else {
Cow::Borrowed(names.values().next().expect(INTERNAL_ERROR_MSG))
@ -96,6 +102,12 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
impl<'n, 'e> Display for PosBuilder<'n, 'e> {
fn fmt(&self, f: &mut Formatter) -> Result {
let mut delim = String::new();
delim.push(if self.is_set(ArgSettings::RequireDelimiter) {
self.v.val_delim.expect(INTERNAL_ERROR_MSG)
} else {
' '
});
if let Some(ref names) = self.v.val_names {
write!(
f,
@ -104,7 +116,7 @@ impl<'n, 'e> Display for PosBuilder<'n, 'e> {
.values()
.map(|n| format!("<{}>", n))
.collect::<Vec<_>>()
.join(" ")
.join(&&*delim)
)?;
} else {
write!(f, "<{}>", self.b.name)?;