Skip to content

Commit

Permalink
imp: args that have require_delimiter(true) is now reflected in help …
Browse files Browse the repository at this point in the history
…and usage strings

Closes #1052
  • Loading branch information
kbknapp committed Oct 12, 2017
1 parent e36ca73 commit dce6169
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/app/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/args/arg_builder/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -65,14 +66,19 @@ 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 {
let mut it = vec.iter().peekable();
while let Some((_, val)) = it.next() {
write!(f, "<{}>", val)?;
if it.peek().is_some() {
write!(f, " ")?;
write!(f, "{}", delim)?;
}
}
let num = vec.len();
Expand All @@ -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 {
Expand Down
16 changes: 14 additions & 2 deletions src/args/arg_builder/positional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -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,
Expand All @@ -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)?;
Expand Down

0 comments on commit dce6169

Please sign in to comment.