2018-06-12 15:42:03 +00:00
|
|
|
// Std
|
2015-04-27 02:03:11 +00:00
|
|
|
use std::fmt::{Debug, Formatter, Result};
|
|
|
|
|
2018-06-12 15:42:03 +00:00
|
|
|
// Third Party
|
2015-09-01 03:14:00 +00:00
|
|
|
#[cfg(feature = "yaml")]
|
2018-06-22 11:32:13 +00:00
|
|
|
use yaml_rust;
|
2015-09-01 03:14:00 +00:00
|
|
|
|
2016-06-23 14:49:25 +00:00
|
|
|
/// `ArgGroup`s are a family of related [arguments] and way for you to express, "Any of these
|
2016-01-26 19:02:10 +00:00
|
|
|
/// 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.
|
2015-04-28 02:52:50 +00:00
|
|
|
///
|
2017-01-30 02:12:54 +00:00
|
|
|
/// 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.
|
2015-05-01 18:44:20 +00:00
|
|
|
///
|
2016-06-23 14:49:25 +00:00
|
|
|
/// You can also do things such as name an entire `ArgGroup` as a [conflict] or [requirement] for
|
2016-01-26 19:02:10 +00:00
|
|
|
/// another argument, meaning any of the arguments that belong to that group will cause a failure
|
|
|
|
/// if present, or must present respectively.
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2015-05-01 18:44:20 +00:00
|
|
|
/// Perhaps the most common use of `ArgGroup`s 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
|
2015-05-01 18:38:27 +00:00
|
|
|
/// specify that *at least one* of them is used. For this, you can create a group.
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2016-01-26 19:02:10 +00:00
|
|
|
/// Finally, you may use `ArgGroup`s to pull a value from a group of arguments when you don't care
|
2016-06-24 10:23:08 +00:00
|
|
|
/// exactly which argument was actually used at runtime.
|
2016-01-26 19:02:10 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-04-28 02:52:50 +00:00
|
|
|
///
|
2016-01-26 19:02:10 +00:00
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2016-06-23 14:49:25 +00:00
|
|
|
/// # use clap::{App, ArgGroup, ErrorKind};
|
|
|
|
/// let result = App::new("app")
|
|
|
|
/// .args_from_usage(
|
|
|
|
/// "--set-ver [ver] 'set the version manually'
|
|
|
|
/// --major 'auto increase major'
|
|
|
|
/// --minor 'auto increase minor'
|
|
|
|
/// --patch 'auto increase patch'")
|
|
|
|
/// .group(ArgGroup::with_name("vers")
|
|
|
|
/// .args(&["set-ver", "major", "minor","patch"])
|
|
|
|
/// .required(true))
|
|
|
|
/// .get_matches_from_safe(vec!["app", "--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
|
2016-08-26 10:40:38 +00:00
|
|
|
///
|
2016-06-23 14:49:25 +00:00
|
|
|
/// ```rust
|
2015-04-27 02:03:11 +00:00
|
|
|
/// # use clap::{App, ArgGroup};
|
2016-06-23 14:49:25 +00:00
|
|
|
/// let result = App::new("app")
|
2016-01-26 19:02:10 +00:00
|
|
|
/// .args_from_usage(
|
|
|
|
/// "--set-ver [ver] 'set the version manually'
|
|
|
|
/// --major 'auto increase major'
|
|
|
|
/// --minor 'auto increase minor'
|
|
|
|
/// --patch 'auto increase patch'")
|
|
|
|
/// .group(ArgGroup::with_name("vers")
|
|
|
|
/// .args(&["set-ver", "major", "minor","patch"])
|
|
|
|
/// .required(true))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .get_matches_from_safe(vec!["app", "--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.is_present("vers"));
|
|
|
|
/// // we could also alternatively check each arg individually (not shown here)
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```
|
2016-06-23 14:49:25 +00:00
|
|
|
/// [`ArgGroup::multiple(true)`]: ./struct.ArgGroup.html#method.multiple
|
|
|
|
/// [arguments]: ./struct.Arg.html
|
|
|
|
/// [conflict]: ./struct.Arg.html#method.conflicts_with
|
|
|
|
/// [requirement]: ./struct.Arg.html#method.requires
|
2016-02-04 06:10:37 +00:00
|
|
|
#[derive(Default)]
|
2016-01-11 08:59:56 +00:00
|
|
|
pub struct ArgGroup<'a> {
|
2018-01-25 04:05:05 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub name: &'a str,
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub args: Vec<&'a str>,
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub required: bool,
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub requires: Option<Vec<&'a str>>,
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub conflicts: Option<Vec<&'a str>>,
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub multiple: bool,
|
2015-04-27 02:03:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
impl<'a> ArgGroup<'a> {
|
2016-01-26 19:02:10 +00:00
|
|
|
/// Creates a new instance of `ArgGroup` using a unique string name. The name will be used to
|
|
|
|
/// get values from the group or refer to the group inside of conflict and requirement rules.
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```rust
|
2015-04-27 02:03:11 +00:00
|
|
|
/// # use clap::{App, ArgGroup};
|
2015-11-01 14:02:37 +00:00
|
|
|
/// ArgGroup::with_name("config")
|
2016-01-26 19:02:10 +00:00
|
|
|
/// # ;
|
|
|
|
/// ```
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn with_name(n: &'a str) -> Self {
|
2015-04-27 02:03:11 +00:00
|
|
|
ArgGroup {
|
|
|
|
name: n,
|
|
|
|
required: false,
|
2015-09-04 18:05:13 +00:00
|
|
|
args: vec![],
|
2015-04-27 02:03:11 +00:00
|
|
|
requires: None,
|
2015-10-28 14:23:59 +00:00
|
|
|
conflicts: None,
|
2016-06-23 14:01:19 +00:00
|
|
|
multiple: false,
|
2015-04-27 02:03:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 14:02:37 +00:00
|
|
|
/// Creates a new instance of `ArgGroup` from a .yml (YAML) file.
|
2015-09-01 03:14:00 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-01 03:14:00 +00:00
|
|
|
///
|
|
|
|
/// ```ignore
|
2016-10-05 09:28:59 +00:00
|
|
|
/// # #[macro_use]
|
|
|
|
/// # extern crate clap;
|
2015-09-01 03:14:00 +00:00
|
|
|
/// # use clap::ArgGroup;
|
2016-10-05 09:28:59 +00:00
|
|
|
/// # fn main() {
|
2015-09-01 03:14:00 +00:00
|
|
|
/// let yml = load_yaml!("group.yml");
|
|
|
|
/// let ag = ArgGroup::from_yaml(yml);
|
2016-10-05 09:28:59 +00:00
|
|
|
/// # }
|
2015-09-01 03:14:00 +00:00
|
|
|
/// ```
|
|
|
|
#[cfg(feature = "yaml")]
|
2018-06-22 11:32:13 +00:00
|
|
|
pub fn from_yaml(y: &'a yaml_rust::Yaml) -> ArgGroup<'a> { ArgGroup::from(y.as_hash().unwrap()) }
|
2015-09-01 03:14:00 +00:00
|
|
|
|
2016-05-14 20:25:00 +00:00
|
|
|
/// Adds an [argument] to this group by name
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```rust
|
2016-06-23 14:49:25 +00:00
|
|
|
/// # use clap::{App, Arg, ArgGroup};
|
|
|
|
/// let m = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("flag")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('f'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("color")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('c'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .group(ArgGroup::with_name("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.is_present("req_flags"));
|
|
|
|
/// // but we can also check individually if needed
|
|
|
|
/// assert!(m.is_present("flag"));
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```
|
2016-05-14 20:25:00 +00:00
|
|
|
/// [argument]: ./struct.Arg.html
|
2017-03-10 03:32:32 +00:00
|
|
|
#[cfg_attr(feature = "lints", allow(should_assert_eq))]
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn arg(mut self, n: &'a str) -> Self {
|
2017-11-28 12:30:06 +00:00
|
|
|
assert!(
|
|
|
|
self.name != n,
|
|
|
|
"ArgGroup '{}' can not have same name as arg inside it",
|
|
|
|
&*self.name
|
|
|
|
);
|
2015-09-04 18:05:13 +00:00
|
|
|
self.args.push(n);
|
2015-04-27 02:03:11 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-05-14 20:25:00 +00:00
|
|
|
/// Adds multiple [arguments] to this group by name
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```rust
|
2016-06-23 14:49:25 +00:00
|
|
|
/// # use clap::{App, Arg, ArgGroup};
|
|
|
|
/// let m = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("flag")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('f'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("color")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('c'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .group(ArgGroup::with_name("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.is_present("req_flags"));
|
|
|
|
/// // but we can also check individually if needed
|
|
|
|
/// assert!(m.is_present("flag"));
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```
|
2016-05-14 20:25:00 +00:00
|
|
|
/// [arguments]: ./struct.Arg.html
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn args(mut self, ns: &[&'a str]) -> Self {
|
2015-04-27 02:03:11 +00:00
|
|
|
for n in ns {
|
2016-01-11 08:59:56 +00:00
|
|
|
self = self.arg(n);
|
2015-04-27 02:03:11 +00:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-06-23 14:01:19 +00:00
|
|
|
/// Allows more than one of the ['Arg']s in this group to be used. (Default: `false`)
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2016-06-23 14:49:25 +00:00
|
|
|
/// Notice in this example we use *both* the `-f` and `-c` flags which are both part of the
|
|
|
|
/// group
|
|
|
|
///
|
2016-06-23 14:01:19 +00:00
|
|
|
/// ```rust
|
2016-06-23 14:49:25 +00:00
|
|
|
/// # use clap::{App, Arg, ArgGroup};
|
|
|
|
/// let m = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("flag")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('f'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("color")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('c'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .group(ArgGroup::with_name("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.is_present("req_flags"));
|
|
|
|
/// ```
|
2016-06-24 10:23:08 +00:00
|
|
|
/// In this next example, we show the default behavior (i.e. `multiple(false)) which will throw
|
2016-06-23 14:49:25 +00:00
|
|
|
/// an error if more than one of the args in the group was used.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use clap::{App, Arg, ArgGroup, ErrorKind};
|
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("flag")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('f'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("color")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('c'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .group(ArgGroup::with_name("req_flags")
|
|
|
|
/// .args(&["flag", "color"]))
|
|
|
|
/// .get_matches_from_safe(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);
|
2016-06-23 14:01:19 +00:00
|
|
|
/// ```
|
|
|
|
/// ['Arg']: ./struct.Arg.html
|
|
|
|
pub fn multiple(mut self, m: bool) -> Self {
|
|
|
|
self.multiple = m;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-01-26 19:02:10 +00:00
|
|
|
/// Sets the group as required or not. A required group will be displayed in the usage string
|
2016-06-23 14:49:25 +00:00
|
|
|
/// of the application in the format `<arg|arg2|arg3>`. A required `ArgGroup` simply states
|
|
|
|
/// that one argument from this group *must* be present at runtime (unless
|
2015-04-27 02:03:11 +00:00
|
|
|
/// conflicting with another argument).
|
|
|
|
///
|
2016-05-14 20:25:00 +00:00
|
|
|
/// **NOTE:** This setting only applies to the current [`App`] / [`SubCommand`], and not
|
|
|
|
/// globally.
|
2016-03-10 01:16:50 +00:00
|
|
|
///
|
2016-06-23 14:49:25 +00:00
|
|
|
/// **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."
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```rust
|
2016-06-23 14:49:25 +00:00
|
|
|
/// # use clap::{App, Arg, ArgGroup, ErrorKind};
|
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("flag")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('f'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("color")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('c'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .group(ArgGroup::with_name("req_flags")
|
|
|
|
/// .args(&["flag", "color"])
|
|
|
|
/// .required(true))
|
|
|
|
/// .get_matches_from_safe(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);
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```
|
2016-05-14 20:25:00 +00:00
|
|
|
/// [`App`]: ./struct.App.html
|
|
|
|
/// [`SubCommand`]: ./struct.SubCommand.html
|
2016-06-23 14:49:25 +00:00
|
|
|
/// [`ArgGroup::multiple`]: ./struct.ArgGroup.html#method.multiple
|
2015-10-28 14:23:59 +00:00
|
|
|
pub fn required(mut self, r: bool) -> Self {
|
2015-04-27 02:03:11 +00:00
|
|
|
self.required = r;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-06-23 14:49:25 +00:00
|
|
|
/// Sets the requirement rules of this group. 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.
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
|
|
|
/// **NOTE:** The name provided may be an argument, or group name
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```rust
|
2016-06-23 14:49:25 +00:00
|
|
|
/// # use clap::{App, Arg, ArgGroup, ErrorKind};
|
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("flag")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('f'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("color")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('c'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("debug")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('d'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .group(ArgGroup::with_name("req_flags")
|
|
|
|
/// .args(&["flag", "color"])
|
|
|
|
/// .requires("debug"))
|
|
|
|
/// .get_matches_from_safe(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);
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```
|
2016-06-23 14:49:25 +00:00
|
|
|
/// [required group]: ./struct.ArgGroup.html#method.required
|
|
|
|
/// [argument requirement rules]: ./struct.Arg.html#method.requires
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn requires(mut self, n: &'a str) -> Self {
|
2015-04-27 02:03:11 +00:00
|
|
|
if let Some(ref mut reqs) = self.requires {
|
2015-09-04 18:05:13 +00:00
|
|
|
reqs.push(n);
|
2015-04-27 02:03:11 +00:00
|
|
|
} else {
|
2015-09-04 18:05:13 +00:00
|
|
|
self.requires = Some(vec![n]);
|
2015-04-27 02:03:11 +00:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-06-23 14:49:25 +00:00
|
|
|
/// Sets the requirement rules of this group. 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.
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
|
|
|
/// **NOTE:** The names provided may be an argument, or group name
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```rust
|
2016-06-23 14:49:25 +00:00
|
|
|
/// # use clap::{App, Arg, ArgGroup, ErrorKind};
|
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("flag")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('f'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("color")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('c'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("debug")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('d'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("verb")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('v'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .group(ArgGroup::with_name("req_flags")
|
|
|
|
/// .args(&["flag", "color"])
|
|
|
|
/// .requires_all(&["debug", "verb"]))
|
|
|
|
/// .get_matches_from_safe(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);
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```
|
2016-06-23 14:49:25 +00:00
|
|
|
/// [required group]: ./struct.ArgGroup.html#method.required
|
|
|
|
/// [argument requirement rules]: ./struct.Arg.html#method.requires_all
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn requires_all(mut self, ns: &[&'a str]) -> Self {
|
2015-04-27 02:03:11 +00:00
|
|
|
for n in ns {
|
|
|
|
self = self.requires(n);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-01-26 19:02:10 +00:00
|
|
|
/// Sets the exclusion rules of this group. Exclusion (aka conflict) rules function just like
|
2016-06-23 14:49:25 +00:00
|
|
|
/// [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.
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
|
|
|
/// **NOTE:** The name provided may be an argument, or group name
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```rust
|
2016-06-23 14:49:25 +00:00
|
|
|
/// # use clap::{App, Arg, ArgGroup, ErrorKind};
|
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("flag")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('f'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("color")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('c'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("debug")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('d'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .group(ArgGroup::with_name("req_flags")
|
|
|
|
/// .args(&["flag", "color"])
|
|
|
|
/// .conflicts_with("debug"))
|
|
|
|
/// .get_matches_from_safe(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);
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```
|
2016-06-23 14:49:25 +00:00
|
|
|
/// [argument exclusion rules]: ./struct.Arg.html#method.conflicts_with
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn conflicts_with(mut self, n: &'a str) -> Self {
|
2015-04-27 02:03:11 +00:00
|
|
|
if let Some(ref mut confs) = self.conflicts {
|
2015-09-04 18:05:13 +00:00
|
|
|
confs.push(n);
|
2015-04-27 02:03:11 +00:00
|
|
|
} else {
|
2015-09-04 18:05:13 +00:00
|
|
|
self.conflicts = Some(vec![n]);
|
2015-04-27 02:03:11 +00:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-06-23 14:49:25 +00:00
|
|
|
/// Sets the exclusion rules of this group. 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.
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
|
|
|
/// **NOTE:** The names provided may be an argument, or group name
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-04-27 02:03:11 +00:00
|
|
|
///
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```rust
|
2016-06-23 14:49:25 +00:00
|
|
|
/// # use clap::{App, Arg, ArgGroup, ErrorKind};
|
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("flag")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('f'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("color")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('c'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("debug")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('d'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .arg(Arg::with_name("verb")
|
2018-07-23 19:09:42 +00:00
|
|
|
/// .short('v'))
|
2016-06-23 14:49:25 +00:00
|
|
|
/// .group(ArgGroup::with_name("req_flags")
|
|
|
|
/// .args(&["flag", "color"])
|
|
|
|
/// .conflicts_with_all(&["debug", "verb"]))
|
|
|
|
/// .get_matches_from_safe(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);
|
2016-01-26 19:02:10 +00:00
|
|
|
/// ```
|
2016-06-23 14:49:25 +00:00
|
|
|
/// [argument exclusion rules]: ./struct.Arg.html#method.conflicts_with_all
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn conflicts_with_all(mut self, ns: &[&'a str]) -> Self {
|
2015-04-27 02:03:11 +00:00
|
|
|
for n in ns {
|
|
|
|
self = self.conflicts_with(n);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
impl<'a> Debug for ArgGroup<'a> {
|
2015-10-28 14:23:59 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
2017-11-28 12:30:06 +00:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{{\n\
|
|
|
|
\tname: {:?},\n\
|
|
|
|
\targs: {:?},\n\
|
|
|
|
\trequired: {:?},\n\
|
|
|
|
\trequires: {:?},\n\
|
|
|
|
\tconflicts: {:?},\n\
|
|
|
|
}}",
|
2018-01-25 04:05:05 +00:00
|
|
|
self.name, self.args, self.required, self.requires, self.conflicts
|
2017-11-28 12:30:06 +00:00
|
|
|
)
|
2015-04-27 02:03:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 17:58:00 +00:00
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
impl<'a, 'z> From<&'z ArgGroup<'a>> for ArgGroup<'a> {
|
|
|
|
fn from(g: &'z ArgGroup<'a>) -> Self {
|
|
|
|
ArgGroup {
|
2016-02-14 06:10:44 +00:00
|
|
|
name: g.name,
|
2016-01-11 08:59:56 +00:00
|
|
|
required: g.required,
|
|
|
|
args: g.args.clone(),
|
|
|
|
requires: g.requires.clone(),
|
|
|
|
conflicts: g.conflicts.clone(),
|
2016-06-23 14:01:19 +00:00
|
|
|
multiple: g.multiple,
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 06:10:37 +00:00
|
|
|
#[cfg(feature = "yaml")]
|
2018-06-22 11:32:13 +00:00
|
|
|
impl<'a> From<&'a yaml_rust::yaml::Hash> for ArgGroup<'a> {
|
|
|
|
fn from(b: &'a yaml_rust::yaml::Hash) -> Self {
|
2016-02-04 06:10:37 +00:00
|
|
|
// We WANT this to panic on error...so expect() is good.
|
|
|
|
let mut a = ArgGroup::default();
|
|
|
|
let group_settings = if b.len() == 1 {
|
|
|
|
let name_yml = b.keys().nth(0).expect("failed to get name");
|
2017-11-28 12:30:06 +00:00
|
|
|
let name_str = name_yml
|
|
|
|
.as_str()
|
|
|
|
.expect("failed to convert arg YAML name to str");
|
2016-02-04 06:10:37 +00:00
|
|
|
a.name = name_str;
|
2016-05-06 21:35:53 +00:00
|
|
|
b.get(name_yml)
|
2016-09-05 21:03:45 +00:00
|
|
|
.expect("failed to get name_str")
|
|
|
|
.as_hash()
|
|
|
|
.expect("failed to convert to a hash")
|
2016-02-04 06:10:37 +00:00
|
|
|
} else {
|
|
|
|
b
|
|
|
|
};
|
|
|
|
|
2017-06-09 14:50:25 +00:00
|
|
|
for (k, v) in group_settings {
|
2016-02-04 06:10:37 +00:00
|
|
|
a = match k.as_str().unwrap() {
|
|
|
|
"required" => a.required(v.as_bool().unwrap()),
|
2017-02-03 20:46:25 +00:00
|
|
|
"multiple" => a.multiple(v.as_bool().unwrap()),
|
2016-09-05 21:03:45 +00:00
|
|
|
"args" => yaml_vec_or_str!(v, a, arg),
|
2016-02-04 06:10:37 +00:00
|
|
|
"arg" => {
|
|
|
|
if let Some(ys) = v.as_str() {
|
|
|
|
a = a.arg(ys);
|
|
|
|
}
|
|
|
|
a
|
|
|
|
}
|
2016-09-05 21:03:45 +00:00
|
|
|
"requires" => yaml_vec_or_str!(v, a, requires),
|
|
|
|
"conflicts_with" => yaml_vec_or_str!(v, a, conflicts_with),
|
2016-02-04 06:10:37 +00:00
|
|
|
"name" => {
|
|
|
|
if let Some(ys) = v.as_str() {
|
|
|
|
a.name = ys;
|
|
|
|
}
|
|
|
|
a
|
|
|
|
}
|
2017-11-28 12:30:06 +00:00
|
|
|
s => panic!(
|
|
|
|
"Unknown ArgGroup setting '{}' in YAML file for \
|
|
|
|
ArgGroup '{}'",
|
2018-01-25 04:05:05 +00:00
|
|
|
s, a.name
|
2017-11-28 12:30:06 +00:00
|
|
|
),
|
2016-02-04 06:10:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:58:00 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::ArgGroup;
|
2016-02-04 06:10:37 +00:00
|
|
|
#[cfg(feature = "yaml")]
|
|
|
|
use yaml_rust::YamlLoader;
|
2015-09-04 17:58:00 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn groups() {
|
|
|
|
let g = ArgGroup::with_name("test")
|
2016-09-05 21:03:45 +00:00
|
|
|
.arg("a1")
|
|
|
|
.arg("a4")
|
|
|
|
.args(&["a2", "a3"])
|
|
|
|
.required(true)
|
|
|
|
.conflicts_with("c1")
|
|
|
|
.conflicts_with_all(&["c2", "c3"])
|
|
|
|
.conflicts_with("c4")
|
|
|
|
.requires("r1")
|
|
|
|
.requires_all(&["r2", "r3"])
|
|
|
|
.requires("r4");
|
2015-09-04 17:58:00 +00:00
|
|
|
|
2016-01-22 04:18:52 +00:00
|
|
|
let args = vec!["a1", "a4", "a2", "a3"];
|
2015-09-07 01:07:46 +00:00
|
|
|
let reqs = vec!["r1", "r2", "r3", "r4"];
|
2015-09-04 18:05:13 +00:00
|
|
|
let confs = vec!["c1", "c2", "c3", "c4"];
|
2015-09-04 17:58:00 +00:00
|
|
|
|
|
|
|
assert_eq!(g.args, args);
|
2016-02-04 06:13:46 +00:00
|
|
|
assert_eq!(g.requires, Some(reqs));
|
|
|
|
assert_eq!(g.conflicts, Some(confs));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_debug() {
|
|
|
|
let g = ArgGroup::with_name("test")
|
2016-09-05 21:03:45 +00:00
|
|
|
.arg("a1")
|
|
|
|
.arg("a4")
|
|
|
|
.args(&["a2", "a3"])
|
|
|
|
.required(true)
|
|
|
|
.conflicts_with("c1")
|
|
|
|
.conflicts_with_all(&["c2", "c3"])
|
|
|
|
.conflicts_with("c4")
|
|
|
|
.requires("r1")
|
|
|
|
.requires_all(&["r2", "r3"])
|
|
|
|
.requires("r4");
|
2016-02-04 06:13:46 +00:00
|
|
|
|
|
|
|
let args = vec!["a1", "a4", "a2", "a3"];
|
|
|
|
let reqs = vec!["r1", "r2", "r3", "r4"];
|
|
|
|
let confs = vec!["c1", "c2", "c3", "c4"];
|
|
|
|
|
2017-11-28 12:30:06 +00:00
|
|
|
let debug_str = format!(
|
|
|
|
"{{\n\
|
|
|
|
\tname: \"test\",\n\
|
|
|
|
\targs: {:?},\n\
|
|
|
|
\trequired: {:?},\n\
|
|
|
|
\trequires: {:?},\n\
|
|
|
|
\tconflicts: {:?},\n\
|
|
|
|
}}",
|
|
|
|
args,
|
|
|
|
true,
|
|
|
|
Some(reqs),
|
|
|
|
Some(confs)
|
|
|
|
);
|
2016-02-04 06:13:46 +00:00
|
|
|
assert_eq!(&*format!("{:?}", g), &*debug_str);
|
|
|
|
}
|
2015-09-04 17:58:00 +00:00
|
|
|
|
2016-02-04 06:13:46 +00:00
|
|
|
#[test]
|
|
|
|
fn test_from() {
|
|
|
|
let g = ArgGroup::with_name("test")
|
2016-09-05 21:03:45 +00:00
|
|
|
.arg("a1")
|
|
|
|
.arg("a4")
|
|
|
|
.args(&["a2", "a3"])
|
|
|
|
.required(true)
|
|
|
|
.conflicts_with("c1")
|
|
|
|
.conflicts_with_all(&["c2", "c3"])
|
|
|
|
.conflicts_with("c4")
|
|
|
|
.requires("r1")
|
|
|
|
.requires_all(&["r2", "r3"])
|
|
|
|
.requires("r4");
|
2016-02-04 06:13:46 +00:00
|
|
|
|
|
|
|
let args = vec!["a1", "a4", "a2", "a3"];
|
|
|
|
let reqs = vec!["r1", "r2", "r3", "r4"];
|
|
|
|
let confs = vec!["c1", "c2", "c3", "c4"];
|
|
|
|
|
|
|
|
let g2 = ArgGroup::from(&g);
|
|
|
|
assert_eq!(g2.args, args);
|
|
|
|
assert_eq!(g2.requires, Some(reqs));
|
|
|
|
assert_eq!(g2.conflicts, Some(confs));
|
|
|
|
}
|
|
|
|
|
2017-11-28 12:30:06 +00:00
|
|
|
#[cfg(feature = "yaml")]
|
2016-02-04 06:13:46 +00:00
|
|
|
#[cfg_attr(feature = "yaml", test)]
|
|
|
|
fn test_yaml() {
|
2016-05-06 21:35:53 +00:00
|
|
|
let g_yaml = "name: test
|
2016-02-04 06:13:46 +00:00
|
|
|
args:
|
|
|
|
- a1
|
|
|
|
- a4
|
|
|
|
- a2
|
|
|
|
- a3
|
|
|
|
conflicts_with:
|
|
|
|
- c1
|
|
|
|
- c2
|
|
|
|
- c3
|
|
|
|
- c4
|
|
|
|
requires:
|
|
|
|
- r1
|
|
|
|
- r2
|
|
|
|
- r3
|
|
|
|
- r4";
|
|
|
|
let yml = &YamlLoader::load_from_str(g_yaml).expect("failed to load YAML file")[0];
|
|
|
|
let g = ArgGroup::from_yaml(yml);
|
|
|
|
let args = vec!["a1", "a4", "a2", "a3"];
|
|
|
|
let reqs = vec!["r1", "r2", "r3", "r4"];
|
|
|
|
let confs = vec!["c1", "c2", "c3", "c4"];
|
|
|
|
assert_eq!(g.args, args);
|
|
|
|
assert_eq!(g.requires, Some(reqs));
|
|
|
|
assert_eq!(g.conflicts, Some(confs));
|
2015-09-04 17:58:00 +00:00
|
|
|
}
|
2015-09-07 01:07:46 +00:00
|
|
|
}
|
2016-03-15 02:09:17 +00:00
|
|
|
|
|
|
|
impl<'a> Clone for ArgGroup<'a> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ArgGroup {
|
|
|
|
name: self.name,
|
|
|
|
required: self.required,
|
|
|
|
args: self.args.clone(),
|
|
|
|
requires: self.requires.clone(),
|
|
|
|
conflicts: self.conflicts.clone(),
|
2016-06-23 14:01:19 +00:00
|
|
|
multiple: self.multiple,
|
2016-03-15 02:09:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|