mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
Merge #2072
2072: Fix YAML's default_value_if handling r=pksunkara a=bkaestner Co-authored-by: Benjamin Kästner <benjamin.kaestner@gmail.com>
This commit is contained in:
commit
16cf7685d7
3 changed files with 47 additions and 2 deletions
|
@ -65,7 +65,7 @@ macro_rules! yaml_vec_or_str {
|
|||
#[cfg(feature = "yaml")]
|
||||
macro_rules! yaml_opt_str {
|
||||
($v:expr) => {{
|
||||
if $v.is_null() {
|
||||
if !$v.is_null() {
|
||||
Some(
|
||||
$v.as_str()
|
||||
.unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)),
|
||||
|
|
2
tests/fixtures/app.yaml
vendored
2
tests/fixtures/app.yaml
vendored
|
@ -21,7 +21,7 @@ args:
|
|||
about: tests positionals with exclusions
|
||||
index: 2
|
||||
default_value_if:
|
||||
- [flag, Null, some]
|
||||
- [flag, null, some]
|
||||
- [positional, other, something]
|
||||
- flag:
|
||||
short: f
|
||||
|
|
|
@ -54,3 +54,48 @@ fn value_hint() {
|
|||
.unwrap();
|
||||
assert_eq!(arg.get_value_hint(), ValueHint::FilePath);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_value_if_not_triggered_by_argument() {
|
||||
let yml = load_yaml!("fixtures/app.yaml");
|
||||
let app = App::from(yml);
|
||||
|
||||
// Fixtures use "other" as value
|
||||
let matches = app.try_get_matches_from(vec!["prog", "wrong"]).unwrap();
|
||||
|
||||
assert!(matches.value_of("positional2").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_value_if_triggered_by_matching_argument() {
|
||||
let yml = load_yaml!("fixtures/app.yaml");
|
||||
let app = App::from(yml);
|
||||
|
||||
let matches = app.try_get_matches_from(vec!["prog", "other"]).unwrap();
|
||||
assert_eq!(matches.value_of("positional2").unwrap(), "something");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_value_if_triggered_by_flag() {
|
||||
let yml = load_yaml!("fixtures/app.yaml");
|
||||
let app = App::from(yml);
|
||||
|
||||
let matches = app
|
||||
.try_get_matches_from(vec!["prog", "--flag", "flagvalue"])
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(matches.value_of("positional2").unwrap(), "some");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_value_if_triggered_by_flag_and_argument() {
|
||||
let yml = load_yaml!("fixtures/app.yaml");
|
||||
let app = App::from(yml);
|
||||
|
||||
let matches = app
|
||||
.try_get_matches_from(vec!["prog", "--flag", "flagvalue", "other"])
|
||||
.unwrap();
|
||||
|
||||
// First condition triggers, therefore "some"
|
||||
assert_eq!(matches.value_of("positional2").unwrap(), "some");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue