feat: expose is_multiple & get_args API for ArgGroup (#4336)

Fixes #4228
This commit is contained in:
Tony Gorez 2022-10-03 21:21:53 +01:00 committed by GitHub
parent 969d22e4e7
commit 7f98947c04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -188,6 +188,23 @@ impl ArgGroup {
self self
} }
/// Getters for all args. It will return a vector of `Id`
///
/// # Example
///
/// ```rust
/// # use clap::{ArgGroup};
/// let args: Vec<&str> = vec!["a1".into(), "a4".into()];
/// let grp = ArgGroup::new("program").args(&args);
///
/// for (pos, arg) in grp.get_args().enumerate() {
/// assert_eq!(*arg, args[pos]);
/// }
/// ```
pub fn get_args(&self) -> impl Iterator<Item = &Id> {
self.args.iter()
}
/// Allows more than one of the [`Arg`]s in this group to be used. (Default: `false`) /// Allows more than one of the [`Arg`]s in this group to be used. (Default: `false`)
/// ///
/// # Examples /// # Examples
@ -240,6 +257,23 @@ impl ArgGroup {
self self
} }
/// Return true if the group allows more than one of the arguments
/// in this group to be used. (Default: `false`)
///
/// # Example
///
/// ```rust
/// # use clap::{ArgGroup};
/// let mut group = ArgGroup::new("myprog")
/// .args(["f", "c"])
/// .multiple(true);
///
/// assert!(group.is_multiple());
/// ```
pub fn is_multiple(&mut self) -> bool {
self.multiple
}
/// Require an argument from the group to be present when parsing. /// Require an argument from the group to be present when parsing.
/// ///
/// This is unless conflicting with another argument. A required group will be displayed in /// This is unless conflicting with another argument. A required group will be displayed in
@ -538,4 +572,25 @@ mod test {
fn foo<T: Send + Sync>(_: T) {} fn foo<T: Send + Sync>(_: T) {}
foo(ArgGroup::new("test")) foo(ArgGroup::new("test"))
} }
#[test]
fn arg_group_expose_is_multiple_helper() {
let args: Vec<Id> = vec!["a1".into(), "a4".into()];
let mut grp_multiple = ArgGroup::new("test_multiple").args(&args).multiple(true);
assert!(grp_multiple.is_multiple());
let mut grp_not_multiple = ArgGroup::new("test_multiple").args(&args).multiple(false);
assert!(!grp_not_multiple.is_multiple());
}
#[test]
fn arg_group_expose_get_args_helper() {
let args: Vec<Id> = vec!["a1".into(), "a4".into()];
let grp = ArgGroup::new("program").args(&args);
for (pos, arg) in grp.get_args().enumerate() {
assert_eq!(*arg, args[pos]);
}
}
} }