Struct ArgGroup
pub struct ArgGroup { /* private fields */ }
Specifies a logical group of arguments
You can use this for
- applying validation to an entire group, like
ArgGroup::multiple - validate relationships between an argument and a group, like conflicts or requirements
- check which argument in a group was specified on the command-line
For visually grouping arguments in help, see instead
[Arg::help_heading][crate::Arg::help_heading].
Examples
The following example demonstrates using an ArgGroup to ensure that one, and only one, of
the arguments from the specified group is present at runtime.
# use clap_builder as clap;
# use ;
let result = new
.arg
.arg
.arg
.arg
.group
.try_get_matches_from;
// Because we used two args in the group it's an error
assert!;
let err = result.unwrap_err;
assert_eq!;
This next example shows a passing parse of the same scenario
# use clap_builder as clap;
# use ;
let result = new
.arg
.arg
.arg
.arg
.group
.try_get_matches_from;
assert!;
let matches = result.unwrap;
// We may not know which of the args was used, so we can test for the group...
assert!;
// We can also ask the group which arg was used
assert_eq!;
// we could also alternatively check each arg individually (not shown here)
Implementations
impl ArgGroup
fn new(id: impl Into<Id>) -> SelfCreate a
ArgGroupusing a unique name.The name will be used to get values from the group or refer to the group inside of conflict and requirement rules.
Examples
# use clap_builder as clap; # use ; new # ;fn id(self, id: impl Into<Id>) -> SelfSets the group name.
Examples
# use clap_builder as clap; # use ; default.id # ;fn arg(self, arg_id: impl IntoResettable<Id>) -> SelfAdds an argument to this group by name
Examples
# use clap_builder as clap; # use ; let m = new .arg .arg .group .get_matches_from; // maybe we don't know which of the two flags was used... assert!; // but we can also check individually if needed assert!;fn args(self, ns: impl IntoIterator<Item = impl Into<Id>>) -> SelfAdds multiple arguments to this group by name
Examples
# use clap_builder as clap; # use ; let m = new .arg .arg .group .get_matches_from; // maybe we don't know which of the two flags was used... assert!; // but we can also check individually if needed assert!;fn get_args(&self) -> impl Iterator<Item = &Id>Getters for all args. It will return a vector of
IdExample
# use clap_builder as clap; # use ; let args: = vec!; let grp = new.args; for in grp.get_args.enumeratefn multiple(self, yes: bool) -> SelfAllows more than one of the
Args in this group to be used. (Default:false)Examples
Notice in this example we use both the
-fand-cflags which are both part of the group# use clap_builder as clap; # use ; let m = new .arg .arg .group .get_matches_from; // maybe we don't know which of the two flags was used... assert!;In this next example, we show the default behavior (i.e.
multiple(false)) which will throw an error if more than one of the args in the group was used.# use clap_builder as clap; # use ; let result = new .arg .arg .group .try_get_matches_from; // Because we used both args in the group it's an error assert!; let err = result.unwrap_err; assert_eq!;fn is_multiple(&mut self) -> boolReturn true if the group allows more than one of the arguments in this group to be used. (Default:
false)Example
# use clap_builder as clap; # use ; let mut group = new .args .multiple; assert!;fn required(self, yes: bool) -> SelfRequire an argument from the group to be present when parsing.
This is unless conflicting with another argument. A required group will be displayed in the usage string of the application in the format
<arg|arg2|arg3>.NOTE: This setting only applies to the current
Command/Subcommands, and not globally.NOTE: By default,
ArgGroup::multipleis set tofalsewhich when combined withArgGroup::required(true)states, "One and only one arg must be used from this group. Use of more than one arg is an error." Vice settingArgGroup::multiple(true)which states, 'At least one arg from this group must be used. Using multiple is OK."Examples
# use clap_builder as clap; # use ; let result = new .arg .arg .group .try_get_matches_from; // Because we didn't use any of the args in the group, it's an error assert!; let err = result.unwrap_err; assert_eq!;fn requires(self, id: impl IntoResettable<Id>) -> SelfSpecify an argument or group that must be present when this group is.
This is not to be confused with a required group. Requirement rules function just like argument requirement rules, you can name other arguments or groups that must be present when any one of the arguments from this group is used.
NOTE: The name provided may be an argument or group name
Examples
# use clap_builder as clap; # use ; let result = new .arg .arg .arg .group .try_get_matches_from; // because we used an arg from the group, and the group requires "-d" to be used, it's an // error assert!; let err = result.unwrap_err; assert_eq!;fn requires_all(self, ns: impl IntoIterator<Item = impl Into<Id>>) -> SelfSpecify arguments or groups that must be present when this group is.
This is not to be confused with a required group. Requirement rules function just like argument requirement rules, you can name other arguments or groups that must be present when one of the arguments from this group is used.
NOTE: The names provided may be an argument or group name
Examples
# use clap_builder as clap; # use ; let result = new .arg .arg .arg .arg .group .try_get_matches_from; // because we used an arg from the group, and the group requires "-d" and "-v" to be used, // yet we only used "-d" it's an error assert!; let err = result.unwrap_err; assert_eq!;fn conflicts_with(self, id: impl IntoResettable<Id>) -> SelfSpecify an argument or group that must not be present when this group is.
Exclusion (aka conflict) rules function just like argument exclusion rules, you can name other arguments or groups that must not be present when one of the arguments from this group are used.
NOTE: The name provided may be an argument, or group name
Examples
# use clap_builder as clap; # use ; let result = new .arg .arg .arg .group .try_get_matches_from; // because we used an arg from the group, and the group conflicts with "-d", it's an error assert!; let err = result.unwrap_err; assert_eq!;fn conflicts_with_all(self, ns: impl IntoIterator<Item = impl Into<Id>>) -> SelfSpecify arguments or groups that must not be present when this group is.
Exclusion rules function just like argument exclusion rules, you can name other arguments or groups that must not be present when one of the arguments from this group are used.
NOTE: The names provided may be an argument, or group name
Examples
# use clap_builder as clap; # use ; let result = new .arg .arg .arg .arg .group .try_get_matches_from; // because we used an arg from the group, and the group conflicts with either "-v" or "-d" // it's an error assert!; let err = result.unwrap_err; assert_eq!;
impl ArgGroup
fn get_id(&self) -> &IdGet the name of the group
fn is_required_set(&self) -> boolReports whether
ArgGroup::requiredis set
Trait Implementations
impl Clone for ArgGroup
fn clone(&self) -> ArgGroup
impl Debug for ArgGroup
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Default for ArgGroup
fn default() -> ArgGroup
impl Eq for ArgGroup
impl From<&ArgGroup> for ArgGroup
fn from(g: &ArgGroup) -> Self
impl PartialEq for ArgGroup
fn eq(&self, other: &ArgGroup) -> bool
impl StructuralPartialEq for ArgGroup
Auto Trait Implementations
impl Freeze for ArgGroup
impl RefUnwindSafe for ArgGroup
impl Send for ArgGroup
impl Sync for ArgGroup
impl Unpin for ArgGroup
impl UnsafeUnpin for ArgGroup
impl UnwindSafe for ArgGroup
Blanket Implementations
impl<T> Any for ArgGroup
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for ArgGroup
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for ArgGroup
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for ArgGroup
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for ArgGroup
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for ArgGroup
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for ArgGroup
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for ArgGroup
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for ArgGroup
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>