Improve debug assert for required group and default value interaction

This commit is contained in:
Pavan Kumar Sunkara 2021-10-25 18:48:03 +01:00
parent b21c15f8b6
commit bf91a1a35a
No known key found for this signature in database
GPG key ID: 7149B807A81DE336
3 changed files with 15 additions and 13 deletions

View file

@ -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
)
}
}
}

View file

@ -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

View file

@ -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};