Skip to content

Commit 7f98947

Browse files
authored
feat: expose is_multiple & get_args API for ArgGroup (#4336)
Fixes #4228
1 parent 969d22e commit 7f98947

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/builder/arg_group.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,23 @@ impl ArgGroup {
188188
self
189189
}
190190

191+
/// Getters for all args. It will return a vector of `Id`
192+
///
193+
/// # Example
194+
///
195+
/// ```rust
196+
/// # use clap::{ArgGroup};
197+
/// let args: Vec<&str> = vec!["a1".into(), "a4".into()];
198+
/// let grp = ArgGroup::new("program").args(&args);
199+
///
200+
/// for (pos, arg) in grp.get_args().enumerate() {
201+
/// assert_eq!(*arg, args[pos]);
202+
/// }
203+
/// ```
204+
pub fn get_args(&self) -> impl Iterator<Item = &Id> {
205+
self.args.iter()
206+
}
207+
191208
/// Allows more than one of the [`Arg`]s in this group to be used. (Default: `false`)
192209
///
193210
/// # Examples
@@ -240,6 +257,23 @@ impl ArgGroup {
240257
self
241258
}
242259

260+
/// Return true if the group allows more than one of the arguments
261+
/// in this group to be used. (Default: `false`)
262+
///
263+
/// # Example
264+
///
265+
/// ```rust
266+
/// # use clap::{ArgGroup};
267+
/// let mut group = ArgGroup::new("myprog")
268+
/// .args(["f", "c"])
269+
/// .multiple(true);
270+
///
271+
/// assert!(group.is_multiple());
272+
/// ```
273+
pub fn is_multiple(&mut self) -> bool {
274+
self.multiple
275+
}
276+
243277
/// Require an argument from the group to be present when parsing.
244278
///
245279
/// This is unless conflicting with another argument. A required group will be displayed in
@@ -538,4 +572,25 @@ mod test {
538572
fn foo<T: Send + Sync>(_: T) {}
539573
foo(ArgGroup::new("test"))
540574
}
575+
576+
#[test]
577+
fn arg_group_expose_is_multiple_helper() {
578+
let args: Vec<Id> = vec!["a1".into(), "a4".into()];
579+
580+
let mut grp_multiple = ArgGroup::new("test_multiple").args(&args).multiple(true);
581+
assert!(grp_multiple.is_multiple());
582+
583+
let mut grp_not_multiple = ArgGroup::new("test_multiple").args(&args).multiple(false);
584+
assert!(!grp_not_multiple.is_multiple());
585+
}
586+
587+
#[test]
588+
fn arg_group_expose_get_args_helper() {
589+
let args: Vec<Id> = vec!["a1".into(), "a4".into()];
590+
let grp = ArgGroup::new("program").args(&args);
591+
592+
for (pos, arg) in grp.get_args().enumerate() {
593+
assert_eq!(*arg, args[pos]);
594+
}
595+
}
541596
}

0 commit comments

Comments
 (0)