Skip to content

Commit

Permalink
feat(Groups): one can now specify groups which require AT LEAST one o…
Browse files Browse the repository at this point in the history
…f the args

Unlike the previous ArgGroups, which made all args in the group mutually exclusive, one can now use
`ArgGroup::multiple(true)` which allows using more than one arg in the group (i.e. they're not all
mutually exclusive). When combined with `ArgGroup::required(true)` this effectively says, "At least
one of the args must be used, and using morethan one is also OK."

Closes #533
  • Loading branch information
kbknapp committed Jun 23, 2016
1 parent ce724d8 commit 33689ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/app/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ macro_rules! _handle_group_reqs{
if found {
vec_remove_all!($me.required, &grp.args);
debugln!("Adding args from group to blacklist...{:?}", grp.args);
$me.blacklist.extend(&grp.args);
vec_remove!($me.blacklist, &$arg.name());
if !grp.multiple {
$me.blacklist.extend(&grp.args);
vec_remove!($me.blacklist, &$arg.name());
}
}
}
})
Expand Down
25 changes: 25 additions & 0 deletions src/args/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub struct ArgGroup<'a> {
pub requires: Option<Vec<&'a str>>,
#[doc(hidden)]
pub conflicts: Option<Vec<&'a str>>,
#[doc(hidden)]
pub multiple: bool,
}

impl<'a> ArgGroup<'a> {
Expand All @@ -81,6 +83,7 @@ impl<'a> ArgGroup<'a> {
args: vec![],
requires: None,
conflicts: None,
multiple: false,
}
}

Expand Down Expand Up @@ -140,6 +143,26 @@ impl<'a> ArgGroup<'a> {
self
}

/// Allows more than one of the ['Arg']s in this group to be used. (Default: `false`)
///
/// # Examples
///
/// ```rust
/// # use clap::{ArgGroup, Arg};
/// let cfg_arg = Arg::with_name("config");
/// let in_arg = Arg::with_name("input");
/// // ...
/// ArgGroup::with_name("files")
/// .args(&["config", "input"])
/// .multiple(true)
/// # ;
/// ```
/// ['Arg']: ./struct.Arg.html
pub fn multiple(mut self, m: bool) -> Self {
self.multiple = m;
self
}

/// Sets the group as required or not. A required group will be displayed in the usage string
/// of the application in the format `[arg|arg2|arg3]`. A required `ArgGroup` simply states
/// that one, and only one argument from this group *must* be present at runtime (unless
Expand Down Expand Up @@ -310,6 +333,7 @@ impl<'a, 'z> From<&'z ArgGroup<'a>> for ArgGroup<'a> {
args: g.args.clone(),
requires: g.requires.clone(),
conflicts: g.conflicts.clone(),
multiple: g.multiple,
}
}
}
Expand Down Expand Up @@ -507,6 +531,7 @@ impl<'a> Clone for ArgGroup<'a> {
args: self.args.clone(),
requires: self.requires.clone(),
conflicts: self.conflicts.clone(),
multiple: self.multiple,
}
}
}

0 comments on commit 33689ac

Please sign in to comment.