Skip to content

Commit

Permalink
feat(subcommands): subcommands can optionally negate parent requirements
Browse files Browse the repository at this point in the history
Closes #123
  • Loading branch information
kbknapp committed May 17, 2015
1 parent ab4ec60 commit 4a4229f
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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:{}",
Expand Down

0 comments on commit 4a4229f

Please sign in to comment.