Skip to content

Commit

Permalink
fix(help): Disallow too many value names (unstable)
Browse files Browse the repository at this point in the history
I can't think of a case for this or a way to render it.

Fixes clap-rs#2695
  • Loading branch information
epage committed May 6, 2022
1 parent 70c29e3 commit 089f96e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
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
31 changes: 31 additions & 0 deletions tests/builder/help.rs
Expand Up @@ -2578,6 +2578,37 @@ OPTIONS:
);
}

#[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

0 comments on commit 089f96e

Please sign in to comment.