Skip to content

Commit

Permalink
feat: expose is_multiple & get_args API for ArgGroup (#4336)
Browse files Browse the repository at this point in the history
Fixes #4228
  • Loading branch information
tony-go authored Oct 3, 2022
1 parent 969d22e commit 7f98947
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/builder/arg_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,23 @@ impl ArgGroup {
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`)
///
/// # Examples
Expand Down Expand Up @@ -240,6 +257,23 @@ impl ArgGroup {
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.
///
/// This is unless conflicting with another argument. A required group will be displayed in
Expand Down Expand Up @@ -538,4 +572,25 @@ mod test {
fn foo<T: Send + Sync>(_: T) {}
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]);
}
}
}

0 comments on commit 7f98947

Please sign in to comment.