Merge pull request #1067 from kbknapp/issue-1052

Issue 1052
This commit is contained in:
Kevin K 2017-10-12 19:26:38 -04:00 committed by GitHub
commit caeb133bb5
4 changed files with 55 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)?;

View file

@ -6,6 +6,20 @@ include!("../clap-test.rs");
use clap::{App, AppSettings, SubCommand, ErrorKind, Arg};
static REQUIRE_DELIM_HELP: &'static str = "test 1.3
Kevin K.
tests stuff
USAGE:
test --fake <some>:<val>
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
-f, --fake <some>:<val> some help";
static HELP: &'static str = "clap-test v1.4.8
Kevin K. <kbknapp@gmail.com>
tests clap library
@ -1028,3 +1042,14 @@ fn override_help() {
assert!(m.is_ok());
assert!(m.unwrap().is_present("help"));
}
#[test]
fn issue_1052_require_delim_help() {
let app = App::new("test")
.author("Kevin K.")
.about("tests stuff")
.version("1.3")
.arg(Arg::from_usage("-f, --fake <some> <val> 'some help'").require_delimiter(true).value_delimiter(":"));
assert!(test::compare_output(app, "test --help", REQUIRE_DELIM_HELP, false));
}