Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(help): Define value name / number of values mismatch relationships #3700

Merged
merged 2 commits into from May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/build/arg.rs
Expand Up @@ -5287,7 +5287,10 @@ where
write(&delim, false)?;
}
}
if (num_val_names == 1 && mult_val) || (arg.is_positional() && mult_occ) {
if (num_val_names == 1 && mult_val)
|| (arg.is_positional() && mult_occ)
|| num_val_names < arg.num_vals.unwrap_or(0)
{
write("...", true)?;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/build/debug_asserts.rs
Expand Up @@ -636,6 +636,18 @@ fn assert_arg(arg: &Arg) {
);
}

#[cfg(feature = "unstable-v4")]
{
let num_vals = arg.get_num_vals().unwrap_or(usize::MAX);
let num_val_names = arg.get_value_names().unwrap_or(&[]).len();
if num_vals < num_val_names {
panic!(
"Argument {}: Too many value names ({}) compared to number_of_values ({})",
arg.name, num_val_names, num_vals
);
}
}

assert_arg_flags(arg);

assert_defaults(arg, "default_value", arg.default_vals.iter().copied());
Expand Down
57 changes: 57 additions & 0 deletions tests/builder/help.rs
Expand Up @@ -2552,6 +2552,63 @@ OPTIONS:
);
}

#[test]
fn too_few_value_names_is_dotted() {
let cmd = Command::new("test").arg(
Arg::new("foo")
.long("foo")
.required(true)
.takes_value(true)
.number_of_values(3)
.value_names(&["one", "two"]),
);
utils::assert_output(
cmd,
"test --help",
"test

USAGE:
test --foo <one> <two>...

OPTIONS:
--foo <one> <two>...
-h, --help Print help information
",
false,
);
}

#[test]
#[cfg(not(feature = "unstable-v4"))]
fn too_many_value_names_panics() {
Command::new("test")
.arg(
Arg::new("foo")
.long("foo")
.required(true)
.takes_value(true)
.number_of_values(1)
.value_names(&["one", "two"]),
)
.debug_assert()
}

#[test]
#[cfg(feature = "unstable-v4")]
#[should_panic = "Argument foo: Too many value names (2) compared to number_of_values (1)"]
fn too_many_value_names_panics() {
Command::new("test")
.arg(
Arg::new("foo")
.long("foo")
.required(true)
.takes_value(true)
.number_of_values(1)
.value_names(&["one", "two"]),
)
.debug_assert()
}

#[test]
fn disabled_help_flag() {
let res = Command::new("foo")
Expand Down