Struct pretty_exec::clap::ArgGroup
[−]pub struct ArgGroup<'help> { /* private fields */ }Expand description
Family of related arguments.
By placing arguments in a logical group, you can create easier requirement and exclusion rules instead of having to list each argument individually, or when you want a rule to apply “any but not all” arguments.
For instance, you can make an entire ArgGroup required. If ArgGroup::multiple(true) is
set, this means that at least one argument from that group must be present. If
ArgGroup::multiple(false) is set (the default), one and only one must be present.
You can also do things such as name an entire ArgGroup as a conflict or requirement for
another argument, meaning any of the arguments that belong to that group will cause a failure
if present, or must be present respectively.
Perhaps the most common use of ArgGroups is to require one and only one argument to be
present out of a given set. Imagine that you had multiple arguments, and you want one of them
to be required, but making all of them required isn’t feasible because perhaps they conflict
with each other. For example, lets say that you were building an application where one could
set a given version number by supplying a string with an option argument, i.e.
--set-ver v1.2.3, you also wanted to support automatically using a previous version number
and simply incrementing one of the three numbers. So you create three flags --major,
--minor, and --patch. All of these arguments shouldn’t be used at one time but you want to
specify that at least one of them is used. For this, you can create a group.
Finally, you may use ArgGroups to pull a value from a group of arguments when you don’t care
exactly which argument was actually used at runtime.
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.
let result = Command::new("cmd")
.arg(arg!(--"set-ver" <ver> "set the version manually").required(false))
.arg(arg!(--major "auto increase major"))
.arg(arg!(--minor "auto increase minor"))
.arg(arg!(--patch "auto increase patch"))
.group(ArgGroup::new("vers")
.args(&["set-ver", "major", "minor", "patch"])
.required(true))
.try_get_matches_from(vec!["cmd", "--major", "--patch"]);
// Because we used two args in the group it's an error
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);This next example shows a passing parse of the same scenario
let result = Command::new("cmd")
.arg(arg!(--"set-ver" <ver> "set the version manually").required(false))
.arg(arg!(--major "auto increase major"))
.arg(arg!(--minor "auto increase minor"))
.arg(arg!(--patch "auto increase patch"))
.group(ArgGroup::new("vers")
.args(&["set-ver", "major", "minor","patch"])
.required(true))
.try_get_matches_from(vec!["cmd", "--major"]);
assert!(result.is_ok());
let matches = result.unwrap();
// We may not know which of the args was used, so we can test for the group...
assert!(matches.contains_id("vers"));
// we could also alternatively check each arg individually (not shown here)Implementations
impl<'help> ArgGroup<'help>
impl<'help> ArgGroup<'help>
pub fn new<S>(n: S) -> ArgGroup<'help> where
S: Into<&'help str>,
pub fn new<S>(n: S) -> ArgGroup<'help> where
S: Into<&'help str>,
Create a ArgGroup using 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
ArgGroup::new("config")pub fn name<S>(self, n: S) -> ArgGroup<'help> where
S: Into<&'help str>,
pub fn name<S>(self, n: S) -> ArgGroup<'help> where
S: Into<&'help str>,
Deprecated, replaced with ArgGroup::id
pub fn arg<T>(self, arg_id: T) -> ArgGroup<'help> where
T: Key,
pub fn arg<T>(self, arg_id: T) -> ArgGroup<'help> where
T: Key,
Adds an argument to this group by name
Examples
let m = Command::new("myprog")
.arg(Arg::new("flag")
.short('f'))
.arg(Arg::new("color")
.short('c'))
.group(ArgGroup::new("req_flags")
.arg("flag")
.arg("color"))
.get_matches_from(vec!["myprog", "-f"]);
// maybe we don't know which of the two flags was used...
assert!(m.contains_id("req_flags"));
// but we can also check individually if needed
assert!(m.contains_id("flag"));pub fn args<T>(self, ns: &[T]) -> ArgGroup<'help> where
T: Key,
pub fn args<T>(self, ns: &[T]) -> ArgGroup<'help> where
T: Key,
Adds multiple arguments to this group by name
Examples
let m = Command::new("myprog")
.arg(Arg::new("flag")
.short('f'))
.arg(Arg::new("color")
.short('c'))
.group(ArgGroup::new("req_flags")
.args(&["flag", "color"]))
.get_matches_from(vec!["myprog", "-f"]);
// maybe we don't know which of the two flags was used...
assert!(m.contains_id("req_flags"));
// but we can also check individually if needed
assert!(m.contains_id("flag"));pub fn multiple(self, yes: bool) -> ArgGroup<'help>
pub fn multiple(self, yes: bool) -> ArgGroup<'help>
Allows more than one of the Args in this group to be used. (Default: false)
Examples
Notice in this example we use both the -f and -c flags which are both part of the
group
let m = Command::new("myprog")
.arg(Arg::new("flag")
.short('f'))
.arg(Arg::new("color")
.short('c'))
.group(ArgGroup::new("req_flags")
.args(&["flag", "color"])
.multiple(true))
.get_matches_from(vec!["myprog", "-f", "-c"]);
// maybe we don't know which of the two flags was used...
assert!(m.contains_id("req_flags"));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.
let result = Command::new("myprog")
.arg(Arg::new("flag")
.short('f'))
.arg(Arg::new("color")
.short('c'))
.group(ArgGroup::new("req_flags")
.args(&["flag", "color"]))
.try_get_matches_from(vec!["myprog", "-f", "-c"]);
// Because we used both args in the group it's an error
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);pub fn required(self, yes: bool) -> ArgGroup<'help>
pub fn required(self, yes: bool) -> ArgGroup<'help>
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
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::multiple is set to false which when combined with
ArgGroup::required(true) states, “One and only one arg must be used from this group.
Use of more than one arg is an error.” Vice setting ArgGroup::multiple(true) which
states, ’At least one arg from this group must be used. Using multiple is OK.“
Examples
let result = Command::new("myprog")
.arg(Arg::new("flag")
.short('f'))
.arg(Arg::new("color")
.short('c'))
.group(ArgGroup::new("req_flags")
.args(&["flag", "color"])
.required(true))
.try_get_matches_from(vec!["myprog"]);
// Because we didn't use any of the args in the group, it's an error
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);pub fn requires<T>(self, id: T) -> ArgGroup<'help> where
T: Key,
pub fn requires<T>(self, id: T) -> ArgGroup<'help> where
T: Key,
Specify 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
let result = Command::new("myprog")
.arg(Arg::new("flag")
.short('f'))
.arg(Arg::new("color")
.short('c'))
.arg(Arg::new("debug")
.short('d'))
.group(ArgGroup::new("req_flags")
.args(&["flag", "color"])
.requires("debug"))
.try_get_matches_from(vec!["myprog", "-c"]);
// because we used an arg from the group, and the group requires "-d" to be used, it's an
// error
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);pub fn requires_all(self, ns: &[&'help str]) -> ArgGroup<'help>
pub fn requires_all(self, ns: &[&'help str]) -> ArgGroup<'help>
Specify 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
let result = Command::new("myprog")
.arg(Arg::new("flag")
.short('f'))
.arg(Arg::new("color")
.short('c'))
.arg(Arg::new("debug")
.short('d'))
.arg(Arg::new("verb")
.short('v'))
.group(ArgGroup::new("req_flags")
.args(&["flag", "color"])
.requires_all(&["debug", "verb"]))
.try_get_matches_from(vec!["myprog", "-c", "-d"]);
// 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!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);pub fn conflicts_with<T>(self, id: T) -> ArgGroup<'help> where
T: Key,
pub fn conflicts_with<T>(self, id: T) -> ArgGroup<'help> where
T: Key,
Specify 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
let result = Command::new("myprog")
.arg(Arg::new("flag")
.short('f'))
.arg(Arg::new("color")
.short('c'))
.arg(Arg::new("debug")
.short('d'))
.group(ArgGroup::new("req_flags")
.args(&["flag", "color"])
.conflicts_with("debug"))
.try_get_matches_from(vec!["myprog", "-c", "-d"]);
// because we used an arg from the group, and the group conflicts with "-d", it's an error
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);pub fn conflicts_with_all(self, ns: &[&'help str]) -> ArgGroup<'help>
pub fn conflicts_with_all(self, ns: &[&'help str]) -> ArgGroup<'help>
Specify 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
let result = Command::new("myprog")
.arg(Arg::new("flag")
.short('f'))
.arg(Arg::new("color")
.short('c'))
.arg(Arg::new("debug")
.short('d'))
.arg(Arg::new("verb")
.short('v'))
.group(ArgGroup::new("req_flags")
.args(&["flag", "color"])
.conflicts_with_all(&["debug", "verb"]))
.try_get_matches_from(vec!["myprog", "-c", "-v"]);
// because we used an arg from the group, and the group conflicts with either "-v" or "-d"
// it's an error
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);Trait Implementations
impl<'help> Eq for ArgGroup<'help>
impl<'help> StructuralEq for ArgGroup<'help>
impl<'help> StructuralPartialEq for ArgGroup<'help>
Auto Trait Implementations
impl<'help> RefUnwindSafe for ArgGroup<'help>
impl<'help> Send for ArgGroup<'help>
impl<'help> Sync for ArgGroup<'help>
impl<'help> Unpin for ArgGroup<'help>
impl<'help> UnwindSafe for ArgGroup<'help>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key and return true if they are equal.
impl<X> Pipe for X
impl<X> Pipe for X
fn pipe<Return, Function>(self, f: Function) -> Return where
Function: FnOnce(Self) -> Return,
fn pipe<Return, Function>(self, f: Function) -> Return where
Function: FnOnce(Self) -> Return,
Apply f to self. Read more
fn pipe_ref<'a, Return, Function>(&'a self, f: Function) -> Return where
Function: FnOnce(&'a Self) -> Return,
fn pipe_ref<'a, Return, Function>(&'a self, f: Function) -> Return where
Function: FnOnce(&'a Self) -> Return,
Apply f to &self. Read more
fn pipe_mut<'a, Return, Function>(&'a mut self, f: Function) -> Return where
Function: FnOnce(&'a mut Self) -> Return,
fn pipe_mut<'a, Return, Function>(&'a mut self, f: Function) -> Return where
Function: FnOnce(&'a mut Self) -> Return,
Apply f to &mut self. Read more
fn pipe_as_ref<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: AsRef<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
fn pipe_as_ref<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: AsRef<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
Apply f to &self where f takes a single parameter of type Param
and Self implements trait AsRef<Param>. Read more
fn pipe_as_mut<'a, Param, Return, Function>(&'a mut self, f: Function) -> Return where
Self: AsMut<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
fn pipe_as_mut<'a, Param, Return, Function>(&'a mut self, f: Function) -> Return where
Self: AsMut<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
Apply f to &mut self where f takes a single parameter of type Param
and Self implements trait AsMut<Param>. Read more
fn pipe_deref<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: Deref<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
fn pipe_deref<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: Deref<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
Apply f to &self where f takes a single parameter of type Param
and Self implements trait Deref<Target = Param>. Read more
fn pipe_deref_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function
) -> Return where
Self: DerefMut<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
fn pipe_deref_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function
) -> Return where
Self: DerefMut<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
Apply f to &mut self where f takes a single parameter of type Param
and Self implements trait [DerefMut<Target = Param>]. Read more
fn pipe_borrow<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: Borrow<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
fn pipe_borrow<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: Borrow<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
Apply f to &self where f takes a single parameter of type Param
and Self implements trait Borrow<Param>. Read more
fn pipe_borrow_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function
) -> Return where
Self: BorrowMut<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
fn pipe_borrow_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function
) -> Return where
Self: BorrowMut<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
Apply f to &mut self where f takes a single parameter of type Param
and Self implements trait BorrowMut<Param>. Read more