Skip to content

Commit

Permalink
Fix issue 1571
Browse files Browse the repository at this point in the history
  • Loading branch information
ldm0 committed Aug 16, 2021
1 parent 0035ae0 commit 1641c71
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
60 changes: 23 additions & 37 deletions src/build/arg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{
error::Error,
ffi::OsStr,
fmt::{self, Display, Formatter},
str,
iter, str,
sync::{Arc, Mutex},
};
#[cfg(feature = "env")]
Expand Down Expand Up @@ -4852,77 +4852,63 @@ pub fn display_arg_val<F, T, E>(arg: &Arg, mut write: F) -> Result<(), E>
where
F: FnMut(&str, bool) -> Result<T, E>,
{
let mult = arg.is_set(ArgSettings::MultipleValues);
let mult_val = arg.is_set(ArgSettings::MultipleValues);
let mult_occ = arg.is_set(ArgSettings::MultipleOccurrences);
let delim = if arg.is_set(ArgSettings::RequireDelimiter) {
arg.val_delim.expect(INTERNAL_ERROR_MSG)
} else {
' '
};
if !arg.val_names.is_empty() {
// If have val_name.
match (arg.val_names.len(), arg.num_vals) {
(1, Some(num)) => {
(1, Some(num_vals)) => {
// If single value name with multiple num_of_vals, display all
// the values with the single value name.
let arg_name = format!("<{}>", arg.val_names.get(0).unwrap());
let mut it = (0..num).peekable();
while it.next().is_some() {
let mut it = iter::repeat(arg_name).take(num_vals).peekable();
while let Some(arg_name) = it.next() {
write(&arg_name, true)?;
if it.peek().is_some() {
write(&delim.to_string(), false)?;
}
}
if mult && num == 1 {
write("...", true)?;
}
}
_ => {
(num_val_names, _) => {
// If multiple value names, display them sequentially(ignore num of vals).
let mut it = arg.val_names.iter().peekable();
while let Some(val) = it.next() {
write(&format!("<{}>", val), true)?;
if it.peek().is_some() {
write(&delim.to_string(), false)?;
}
}
let num = arg.val_names.len();
if mult && num == 1 {
if num_val_names == 1 && mult_val {
write("...", true)?;
}
}
}
} else if let Some(num) = arg.num_vals {
} else if let Some(num_vals) = arg.num_vals {
// If number_of_values is sepcified, display the value multiple times.
let arg_name = format!("<{}>", arg.name);
let mut it = (0..num).peekable();
while it.next().is_some() {
write(&arg_name, true)?;
let mut it = iter::repeat(&arg_name).take(num_vals).peekable();
while let Some(arg_name) = it.next() {
write(arg_name, true)?;
if it.peek().is_some() {
write(&delim.to_string(), false)?;
}
}
if mult && num == 1 {
write("...", true)?;
}
} else if arg.is_positional() {
// value of positional argument
if !arg.val_names.is_empty() {
write(
&arg.val_names
.iter()
.map(|n| format!("<{}>", n))
.collect::<Vec<_>>()
.join(&delim.to_string()),
true,
)?;
} else {
write(&format!("<{}>", arg.name), true)?;
if matches!(arg.num_vals, Some(x) if x > 1)
|| (matches!(arg.num_vals, None) && (mult || mult_occ))
{
write("...", true)?;
}
// Value of positional argument with no num_vals and val_names.
write(&format!("<{}>", arg.name), true)?;

if mult_val || mult_occ {
write("...", true)?;
}
} else {
// value of flag argument
// value of flag argument with no num_vals and val_names.
write(&format!("<{}>", arg.name), true)?;
if mult {
if mult_val {
write("...", true)?;
}
}
Expand Down
32 changes: 30 additions & 2 deletions tests/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ FLAGS:
-V, --version Print version information
OPTIONS:
-o, --option <option>... tests options
-O, --opt <opt> tests options";
-o, --option <option> tests options
-O, --opt <opt> tests options";

static RIPGREP_USAGE: &str = "ripgrep 0.5
Expand Down Expand Up @@ -1247,6 +1247,34 @@ fn issue_760() {
assert!(utils::compare_output(app, "ctest --help", ISSUE_760, false));
}

#[test]
fn issue_1571() {
let app = App::new("hello").arg(
Arg::new("name")
.long("package")
.short('p')
.number_of_values(1)
.takes_value(true)
.multiple_values(true),
);
assert!(utils::compare_output(
app,
"hello --help",
"hello
USAGE:
hello [OPTIONS]
FLAGS:
-h, --help Print help information
-V, --version Print version information
OPTIONS:
-p, --package <name> ",
false
));
}

#[test]
fn ripgrep_usage() {
let app = App::new("ripgrep").version("0.5").override_usage(
Expand Down

0 comments on commit 1641c71

Please sign in to comment.