feat(subcommands): subcommands can optionally negate parent requirements

Closes #123
This commit is contained in:
Kevin K 2015-05-17 14:29:57 -04:00
parent ab4ec609cc
commit 4a4229f500

View file

@ -109,6 +109,7 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
needs_short_help: bool,
needs_short_version: bool,
needs_subcmd_help: bool,
subcmds_neg_reqs: bool,
required: HashSet<&'ar str>,
short_list: HashSet<char>,
long_list: HashSet<&'ar str>,
@ -156,6 +157,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
blacklist: HashSet::new(),
bin_name: None,
groups: HashMap::new(),
subcmds_neg_reqs: false
}
}
@ -209,6 +211,25 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
self
}
/// Allows subcommands to override all requirements of the parent (this command). For example
/// if you had a subcommand or even top level application which had a required arguments that
/// is only required if no subcommand is used.
///
/// **NOTE:** This defaults to false (using subcommand does *not* negate requirements)
///
/// # Example
///
/// ```no_run
/// # use clap::App;
/// # let app = App::new("myprog")
/// .subcommands_negate_reqs(true)
/// # .get_matches();
/// ```
pub fn subcommands_negate_reqs(mut self, n: bool) -> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
self.subcmds_neg_reqs = n;
self
}
/// Sets a string of the version number to be displayed when displaying version or help
/// information.
///
@ -1602,7 +1623,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
self.validate_blacklist(matches);
self.validate_num_args(matches);
if !self.required.is_empty() {
if !self.required.is_empty() && !self.subcmds_neg_reqs {
if self.validate_required(&matches) {
self.report_error(format!("The following required arguments were not \
supplied:{}",