-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #283 - kbknapp:issue-279, r=kbknapp
Issue 279 Allows accessing values by group name, and checking presence of any arg in a group in matches.
- Loading branch information
Showing
4 changed files
with
201 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,3 @@ mod app; | |
mod args; | ||
mod usageparser; | ||
mod fmt; | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
extern crate clap; | ||
|
||
use clap::{App, ArgGroup, ClapErrorType}; | ||
|
||
#[test] | ||
fn required_group_missing_arg() { | ||
let result = App::new("group") | ||
.args_from_usage("-f, --flag 'some flag' | ||
-c, --color 'some other flag'") | ||
.arg_group(ArgGroup::with_name("req") | ||
.add_all(&["flag", "color"]) | ||
.required(true)) | ||
.get_matches_from_safe(vec![""]); | ||
assert!(result.is_err()); | ||
let err = result.err().unwrap(); | ||
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument); | ||
} | ||
|
||
#[test] | ||
fn group_single_value() { | ||
let m = App::new("group") | ||
.args_from_usage("-f, --flag 'some flag' | ||
-c, --color [color] 'some option'") | ||
.arg_group(ArgGroup::with_name("grp") | ||
.add_all(&["flag", "color"])) | ||
.get_matches_from(vec!["", "-c", "blue"]); | ||
assert!(m.is_present("grp")); | ||
assert_eq!(m.value_of("grp").unwrap(), "blue"); | ||
let m = App::new("group") | ||
.args_from_usage("-f, --flag 'some flag' | ||
-c, --color [color] 'some option'") | ||
.arg_group(ArgGroup::with_name("grp") | ||
.add_all(&["flag", "color"])) | ||
.get_matches_from(vec!["", "-f"]); | ||
assert!(m.is_present("grp")); | ||
assert!(m.value_of("grp").is_none()); | ||
let m = App::new("group") | ||
.args_from_usage("-f, --flag 'some flag' | ||
-c, --color [color] 'some option'") | ||
.arg_group(ArgGroup::with_name("grp") | ||
.add_all(&["flag", "color"])) | ||
.get_matches_from(vec![""]); | ||
assert!(!m.is_present("grp")); | ||
assert!(m.value_of("grp").is_none()); | ||
} | ||
|
||
#[test] | ||
fn group_multi_value_single_arg() { | ||
let m = App::new("group") | ||
.args_from_usage("-f, --flag 'some flag' | ||
-c, --color [color]... 'some option'") | ||
.arg_group(ArgGroup::with_name("grp") | ||
.add_all(&["flag", "color"])) | ||
.get_matches_from(vec!["", "-c", "blue", "red", "green"]); | ||
assert!(m.is_present("grp")); | ||
assert_eq!(m.values_of("grp").unwrap(), &["blue", "red", "green"]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters