Skip to content

Commit 9435b2a

Browse files
committed
tests: adds tests to make sure args are preferred over matching subcommands when values are possible
1 parent 03455b7 commit 9435b2a

File tree

2 files changed

+25
-53
lines changed

2 files changed

+25
-53
lines changed

tests/multiple_values.rs

-53
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,6 @@ fn option_long() {
2525
assert_eq!(m.values_of("option").unwrap().collect::<Vec<_>>(), ["val1", "val2", "val3"]);
2626
}
2727

28-
#[test]
29-
fn with_subcmd() {
30-
let m = App::new("multiple_values")
31-
.arg(Arg::with_name("option")
32-
.long("option")
33-
.help("multiple options")
34-
.takes_value(true)
35-
.multiple(true))
36-
.subcommand(SubCommand::with_name("foo"))
37-
.get_matches_from_safe(vec![
38-
"",
39-
"--option", "val1",
40-
"val2", "foo"
41-
]);
42-
43-
assert!(m.is_ok());
44-
let m = m.unwrap();
45-
46-
assert!(m.is_present("option"));
47-
assert_eq!(m.occurrences_of("option"), 1);
48-
assert_eq!(m.values_of("option").unwrap().collect::<Vec<_>>(), ["val1", "val2"]);
49-
assert_eq!(m.subcommand_name(), Some("foo"));
50-
}
51-
5228
#[test]
5329
fn option_short() {
5430
let m = App::new("multiple_values")
@@ -985,35 +961,6 @@ fn low_index_positional() {
985961
assert_eq!(m.value_of("target").unwrap(), "target");
986962
}
987963

988-
#[test]
989-
fn low_index_positional_with_subcmd() {
990-
let m = App::new("lip")
991-
.arg(Arg::with_name("files")
992-
.index(1)
993-
.required(true)
994-
.multiple(true))
995-
.arg(Arg::with_name("target")
996-
.index(2)
997-
.required(true))
998-
.subcommand(SubCommand::with_name("test").arg(Arg::with_name("other")))
999-
.get_matches_from_safe(vec![
1000-
"lip",
1001-
"file1", "file2",
1002-
"file3", "target",
1003-
"test"
1004-
]);
1005-
1006-
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
1007-
let m = m.unwrap();
1008-
1009-
assert!(m.is_present("files"));
1010-
assert_eq!(m.occurrences_of("files"), 3);
1011-
assert!(m.is_present("target"));
1012-
assert_eq!(m.occurrences_of("target"), 1);
1013-
assert_eq!(m.values_of("files").unwrap().collect::<Vec<_>>(), ["file1", "file2", "file3"]);
1014-
assert_eq!(m.value_of("target").unwrap(), "target");
1015-
}
1016-
1017964
#[test]
1018965
fn low_index_positional_in_subcmd() {
1019966
let m = App::new("lip")

tests/subcommands.rs

+25
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,28 @@ fn invisible_aliases_help_output() {
164164
.alias("invisible"));
165165
assert!(test::compare_output(app, "clap-test --help", INVISIBLE_ALIAS_HELP, false));
166166
}
167+
168+
#[test]
169+
fn issue_1031_args_with_same_name() {
170+
let res = App::new("prog")
171+
.arg(Arg::from_usage("--ui-path=<PATH>"))
172+
.subcommand(SubCommand::with_name("signer"))
173+
.get_matches_from_safe(vec!["prog", "--ui-path", "signer"]);
174+
175+
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
176+
let m = res.unwrap();
177+
assert_eq!(m.value_of("ui-path"), Some("signer"));
178+
}
179+
180+
#[test]
181+
fn issue_1031_args_with_same_name_no_more_vals() {
182+
let res = App::new("prog")
183+
.arg(Arg::from_usage("--ui-path=<PATH>"))
184+
.subcommand(SubCommand::with_name("signer"))
185+
.get_matches_from_safe(vec!["prog", "--ui-path", "value", "signer"]);
186+
187+
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
188+
let m = res.unwrap();
189+
assert_eq!(m.value_of("ui-path"), Some("value"));
190+
assert_eq!(m.subcommand_name(), Some("signer"));
191+
}

0 commit comments

Comments
 (0)