Skip to content

Commit

Permalink
fix: add debug assertion for missing args in subcommand ArgGroup
Browse files Browse the repository at this point in the history
In debug mode, assert that all arguments in a subcommand's required
ArgGroup exist when printing the subcommand's help message. The previous
behavior was an expectation failure with the generic INTERNAL_ERROR_MSG.

See-also: 7ad123e (introduced relevant assertion)
  • Loading branch information
janikrabe committed Feb 4, 2019
1 parent 3294d18 commit 2699d9e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ where
}

#[inline]
fn app_debug_asserts(&mut self) -> bool {
fn app_debug_asserts(&self) -> bool {
assert!(self.verify_positionals());
let should_err = self.groups.iter().all(|g| {
g.args.iter().all(|arg| {
Expand Down Expand Up @@ -512,7 +512,7 @@ where
pub fn unset(&mut self, s: AS) { self.settings.unset(s) }

#[cfg_attr(feature = "lints", allow(block_in_if_condition_stmt))]
pub fn verify_positionals(&mut self) -> bool {
pub fn verify_positionals(&self) -> bool {
// Because you must wait until all arguments have been supplied, this is the first chance
// to make assertions on positional argument indexes
//
Expand Down Expand Up @@ -1362,6 +1362,8 @@ where
}

pub fn args_in_group(&self, group: &str) -> Vec<String> {
debug_assert!(self.app_debug_asserts());

let mut g_vec = vec![];
let mut args = vec![];

Expand Down
16 changes: 15 additions & 1 deletion tests/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate regex;

include!("../clap-test.rs");

use clap::{App, Arg, ArgGroup, ErrorKind};
use clap::{App, Arg, ArgGroup, ErrorKind, SubCommand};

static REQ_GROUP_USAGE: &'static str = "error: The following required arguments were not provided:
<base|--delete>
Expand Down Expand Up @@ -53,6 +53,20 @@ fn non_existing_arg() {
.get_matches_from_safe(vec![""]);
}

#[test]
#[should_panic(expected = "The group 'c' contains the arg 'd' that doesn't actually exist.")]
fn non_existing_arg_in_subcommand_help() {
let _ = App::new("a")
.subcommand(
SubCommand::with_name("b")
.group(
ArgGroup::with_name("c")
.args(&["d"])
.required(true),
)
).get_matches_from_safe(vec!["a", "help", "b"]);
}

#[test]
fn group_single_value() {
let res = App::new("group")
Expand Down

0 comments on commit 2699d9e

Please sign in to comment.