mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 14:54:15 +00:00
Improve debug assert for required group and default value interaction
This commit is contained in:
parent
b21c15f8b6
commit
bf91a1a35a
3 changed files with 15 additions and 13 deletions
|
@ -252,6 +252,19 @@ pub(crate) fn assert_app(app: &App) {
|
|||
group.name,
|
||||
);
|
||||
|
||||
// Required groups should have at least one arg without default values
|
||||
if group.required && !group.args.is_empty() {
|
||||
assert!(
|
||||
group.args.iter().any(|arg| {
|
||||
app.args
|
||||
.args()
|
||||
.any(|x| x.id == *arg && x.default_vals.is_empty())
|
||||
}),
|
||||
"Argument group '{}' is required but all of it's arguments have a default value.",
|
||||
group.name
|
||||
)
|
||||
}
|
||||
|
||||
for arg in &group.args {
|
||||
// Args listed inside groups should exist
|
||||
assert!(
|
||||
|
@ -260,18 +273,6 @@ pub(crate) fn assert_app(app: &App) {
|
|||
group.name,
|
||||
arg
|
||||
);
|
||||
|
||||
// Required groups shouldn't have args with default values
|
||||
if group.required {
|
||||
assert!(
|
||||
app.args
|
||||
.args ()
|
||||
.any(|x| x.id == *arg && x.default_vals.is_empty()),
|
||||
"Argument group '{}' is required but contains argument '{:?}' which has a default value.",
|
||||
group.name,
|
||||
arg
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2254,6 +2254,7 @@ impl<'help> App<'help> {
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Get the name of the program (argument 1 of env::args()) and determine the
|
||||
// actual file
|
||||
// that was used to execute the program. This is because a program called
|
||||
|
|
|
@ -591,7 +591,7 @@ fn issue_1050_num_vals_and_defaults() {
|
|||
|
||||
#[cfg(debug_assertions)]
|
||||
#[test]
|
||||
#[should_panic = "Argument group 'group' is required but contains argument 'arg' which has a default value."]
|
||||
#[should_panic = "Argument group 'group' is required but all of it's arguments have a default value."]
|
||||
fn required_groups_with_default_values() {
|
||||
use clap::{App, Arg, ArgGroup};
|
||||
|
||||
|
|
Loading…
Reference in a new issue