Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose is_multiple & get_args API for ArgGroup #4336

Merged
merged 6 commits into from Oct 3, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/builder/arg_group.rs
Expand Up @@ -188,6 +188,21 @@ impl ArgGroup {
self
}

/// Getters for all args. It will return a vector of `Id`
///
/// Example
tony-go marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```rust
/// # use clap::{ArgGroup};
/// let group = ArgGroup::new("myprog")
/// .args(["f", "c"]);
///
/// assert_eq!(group.get_args(), ["f", "c"]);
/// ```
pub fn get_args(self) -> Vec<Id> {
self.args
}

epage marked this conversation as resolved.
Show resolved Hide resolved
/// Allows more than one of the [`Arg`]s in this group to be used. (Default: `false`)
///
/// # Examples
Expand Down Expand Up @@ -240,6 +255,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
tony-go marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```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 +570,23 @@ 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);

assert_eq!(grp.get_args(), args);
}
}