Skip to content

Commit

Permalink
test(parser): Verify global/default interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 28, 2022
1 parent 9962393 commit f082eb6
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/builder/global_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,40 @@ fn deeply_nested_discovery() {
let m = m.subcommand_matches("c").unwrap();
assert!(*m.get_one::<bool>("long-c").expect("defaulted by clap"));
}

#[test]
fn global_overrides_default() {
let cmd = Command::new("test")
.arg(
Arg::new("name")
.long("name")
.global(true)
.takes_value(true)
.default_value("from_default"),
)
.subcommand(Command::new("sub"));

let m = cmd.clone().try_get_matches_from(["test"]).unwrap();
assert_eq!(
m.get_one::<String>("name").unwrap().as_str(),
"from_default"
);

let m = cmd
.clone()
.try_get_matches_from(["test", "--name", "from_arg"])
.unwrap();
assert_eq!(m.get_one::<String>("name").unwrap().as_str(), "from_arg");

let m = cmd
.clone()
.try_get_matches_from(["test", "--name", "from_arg", "sub"])
.unwrap();
assert_eq!(m.get_one::<String>("name").unwrap().as_str(), "from_arg");

let m = cmd
.clone()
.try_get_matches_from(["test", "sub", "--name", "from_arg"])
.unwrap();
assert_eq!(m.get_one::<String>("name").unwrap().as_str(), "from_arg");
}

0 comments on commit f082eb6

Please sign in to comment.