Pavan Kumar Sunkara | b7f76d8 | 2020-02-04 08:10:53 | [diff] [blame] | 1 | mod utils; |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 2 | |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 3 | use clap::{App, Arg, ArgGroup, ErrorKind}; |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 4 | |
Oleksii Filonenko | 1e39967 | 2019-10-02 13:27:19 | [diff] [blame] | 5 | static REQUIRE_EQUALS: &str = "error: The following required arguments were not provided: |
Kevin K | c84416f | 2017-03-17 01:45:45 | [diff] [blame] | 6 | --opt=<FILE> |
| 7 | |
| 8 | USAGE: |
| 9 | clap-test --opt=<FILE> |
| 10 | |
| 11 | For more information try --help"; |
| 12 | |
Oleksii Filonenko | 1e39967 | 2019-10-02 13:27:19 | [diff] [blame] | 13 | static MISSING_REQ: &str = "error: The following required arguments were not provided: |
Kevin K | f967235 | 2017-01-03 04:05:23 | [diff] [blame] | 14 | <positional2> |
| 15 | --long-option-2 <option2> |
| 16 | |
| 17 | USAGE: |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 18 | clap-test <positional2> --long-option-2 <option2> -F |
Kevin K | f967235 | 2017-01-03 04:05:23 | [diff] [blame] | 19 | |
| 20 | For more information try --help"; |
| 21 | |
danieleades | af45420 | 2020-01-11 18:15:46 | [diff] [blame] | 22 | static COND_REQ_IN_USAGE: &str = "error: The following required arguments were not provided: |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 23 | --output <output> |
| 24 | |
| 25 | USAGE: |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 26 | test --target <target> --input <input> --output <output> |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 27 | |
| 28 | For more information try --help"; |
| 29 | |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 30 | #[test] |
| 31 | fn flag_required() { |
| 32 | let result = App::new("flag_required") |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 33 | .arg(Arg::from("-f, --flag 'some flag'").requires("color")) |
| 34 | .arg(Arg::from("-c, --color 'third flag'")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 35 | .try_get_matches_from(vec!["", "-f"]); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 36 | assert!(result.is_err()); |
| 37 | let err = result.err().unwrap(); |
Kevin K | 7fc18e6 | 2016-01-21 05:18:53 | [diff] [blame] | 38 | assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn flag_required_2() { |
| 43 | let m = App::new("flag_required") |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 44 | .arg(Arg::from("-f, --flag 'some flag'").requires("color")) |
| 45 | .arg(Arg::from("-c, --color 'third flag'")) |
Kevin K | f17e150 | 2016-01-25 20:56:37 | [diff] [blame] | 46 | .get_matches_from(vec!["", "-f", "-c"]); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 47 | assert!(m.is_present("color")); |
| 48 | assert!(m.is_present("flag")); |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn option_required() { |
| 53 | let result = App::new("option_required") |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 54 | .arg(Arg::from("-f [flag] 'some flag'").requires("c")) |
| 55 | .arg(Arg::from("-c [color] 'third flag'")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 56 | .try_get_matches_from(vec!["", "-f", "val"]); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 57 | assert!(result.is_err()); |
| 58 | let err = result.err().unwrap(); |
Kevin K | 7fc18e6 | 2016-01-21 05:18:53 | [diff] [blame] | 59 | assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn option_required_2() { |
| 64 | let m = App::new("option_required") |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 65 | .arg(Arg::from("-f [flag] 'some flag'").requires("c")) |
| 66 | .arg(Arg::from("-c [color] 'third flag'")) |
Kevin K | f17e150 | 2016-01-25 20:56:37 | [diff] [blame] | 67 | .get_matches_from(vec!["", "-f", "val", "-c", "other_val"]); |
| 68 | assert!(m.is_present("c")); |
| 69 | assert_eq!(m.value_of("c").unwrap(), "other_val"); |
| 70 | assert!(m.is_present("f")); |
| 71 | assert_eq!(m.value_of("f").unwrap(), "val"); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | #[test] |
| 75 | fn positional_required() { |
| 76 | let result = App::new("positional_required") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 77 | .arg(Arg::with_name("flag").index(1).required(true)) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 78 | .try_get_matches_from(vec![""]); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 79 | assert!(result.is_err()); |
| 80 | let err = result.err().unwrap(); |
Kevin K | 7fc18e6 | 2016-01-21 05:18:53 | [diff] [blame] | 81 | assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn positional_required_2() { |
| 86 | let m = App::new("positional_required") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 87 | .arg(Arg::with_name("flag").index(1).required(true)) |
Kevin K | f17e150 | 2016-01-25 20:56:37 | [diff] [blame] | 88 | .get_matches_from(vec!["", "someval"]); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 89 | assert!(m.is_present("flag")); |
| 90 | assert_eq!(m.value_of("flag").unwrap(), "someval"); |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn group_required() { |
| 95 | let result = App::new("group_required") |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 96 | .arg(Arg::from("-f, --flag 'some flag'")) |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 97 | .group( |
| 98 | ArgGroup::with_name("gr") |
| 99 | .required(true) |
| 100 | .arg("some") |
| 101 | .arg("other"), |
| 102 | ) |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 103 | .arg(Arg::from("--some 'some arg'")) |
| 104 | .arg(Arg::from("--other 'other arg'")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 105 | .try_get_matches_from(vec!["", "-f"]); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 106 | assert!(result.is_err()); |
| 107 | let err = result.err().unwrap(); |
Kevin K | 7fc18e6 | 2016-01-21 05:18:53 | [diff] [blame] | 108 | assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn group_required_2() { |
| 113 | let m = App::new("group_required") |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 114 | .arg(Arg::from("-f, --flag 'some flag'")) |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 115 | .group( |
| 116 | ArgGroup::with_name("gr") |
| 117 | .required(true) |
| 118 | .arg("some") |
| 119 | .arg("other"), |
| 120 | ) |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 121 | .arg(Arg::from("--some 'some arg'")) |
| 122 | .arg(Arg::from("--other 'other arg'")) |
Kevin K | f17e150 | 2016-01-25 20:56:37 | [diff] [blame] | 123 | .get_matches_from(vec!["", "-f", "--some"]); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 124 | assert!(m.is_present("some")); |
| 125 | assert!(!m.is_present("other")); |
| 126 | assert!(m.is_present("flag")); |
| 127 | } |
| 128 | |
| 129 | #[test] |
| 130 | fn group_required_3() { |
| 131 | let m = App::new("group_required") |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 132 | .arg(Arg::from("-f, --flag 'some flag'")) |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 133 | .group( |
| 134 | ArgGroup::with_name("gr") |
| 135 | .required(true) |
| 136 | .arg("some") |
| 137 | .arg("other"), |
| 138 | ) |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 139 | .arg(Arg::from("--some 'some arg'")) |
| 140 | .arg(Arg::from("--other 'other arg'")) |
Kevin K | f17e150 | 2016-01-25 20:56:37 | [diff] [blame] | 141 | .get_matches_from(vec!["", "-f", "--other"]); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 142 | assert!(!m.is_present("some")); |
| 143 | assert!(m.is_present("other")); |
| 144 | assert!(m.is_present("flag")); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn arg_require_group() { |
| 149 | let result = App::new("arg_require_group") |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 150 | .arg(Arg::from("-f, --flag 'some flag'").requires("gr")) |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 151 | .group(ArgGroup::with_name("gr").arg("some").arg("other")) |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 152 | .arg(Arg::from("--some 'some arg'")) |
| 153 | .arg(Arg::from("--other 'other arg'")) |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 154 | .try_get_matches_from(vec!["", "-f"]); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 155 | assert!(result.is_err()); |
| 156 | let err = result.err().unwrap(); |
Kevin K | 7fc18e6 | 2016-01-21 05:18:53 | [diff] [blame] | 157 | assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn arg_require_group_2() { |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 162 | let res = App::new("arg_require_group") |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 163 | .arg(Arg::from("-f, --flag 'some flag'").requires("gr")) |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 164 | .group(ArgGroup::with_name("gr").arg("some").arg("other")) |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 165 | .arg(Arg::from("--some 'some arg'")) |
| 166 | .arg(Arg::from("--other 'other arg'")) |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 167 | .try_get_matches_from(vec!["", "-f", "--some"]); |
| 168 | assert!(res.is_ok()); |
| 169 | let m = res.unwrap(); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 170 | assert!(m.is_present("some")); |
| 171 | assert!(!m.is_present("other")); |
| 172 | assert!(m.is_present("flag")); |
| 173 | } |
| 174 | |
| 175 | #[test] |
| 176 | fn arg_require_group_3() { |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 177 | let res = App::new("arg_require_group") |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 178 | .arg(Arg::from("-f, --flag 'some flag'").requires("gr")) |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 179 | .group(ArgGroup::with_name("gr").arg("some").arg("other")) |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 180 | .arg(Arg::from("--some 'some arg'")) |
| 181 | .arg(Arg::from("--other 'other arg'")) |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 182 | .try_get_matches_from(vec!["", "-f", "--other"]); |
| 183 | assert!(res.is_ok()); |
| 184 | let m = res.unwrap(); |
Alexander Kuvaev | 7a2375b | 2015-09-06 20:08:13 | [diff] [blame] | 185 | assert!(!m.is_present("some")); |
| 186 | assert!(m.is_present("other")); |
| 187 | assert!(m.is_present("flag")); |
Kevin K | f17e150 | 2016-01-25 20:56:37 | [diff] [blame] | 188 | } |
Kevin K | 9fdad2e | 2016-05-02 18:12:57 | [diff] [blame] | 189 | |
| 190 | // REQUIRED_UNLESS |
| 191 | |
| 192 | #[test] |
Kevin K | d20331b | 2016-11-20 14:48:16 | [diff] [blame] | 193 | fn issue_753() { |
| 194 | let m = App::new("test") |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 195 | .arg(Arg::from( |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 196 | "-l, --list 'List available interfaces (and stop there)'", |
| 197 | )) |
| 198 | .arg( |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 199 | Arg::from("-i, --iface=[INTERFACE] 'Ethernet interface for fetching NTP packets'") |
| 200 | .required_unless("list"), |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 201 | ) |
| 202 | .arg( |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 203 | Arg::from("-f, --file=[TESTFILE] 'Fetch NTP packets from pcap file'") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 204 | .conflicts_with("iface") |
| 205 | .required_unless("list"), |
| 206 | ) |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 207 | .arg(Arg::from("-s, --server=[SERVER_IP] 'NTP server IP address'").required_unless("list")) |
Corentin Henry | 4f602b7 | 2018-04-21 18:59:19 | [diff] [blame] | 208 | .arg(Arg::from("-p, --port=[SERVER_PORT] 'NTP server port'").default_value("123")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 209 | .try_get_matches_from(vec!["test", "--list"]); |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 210 | assert!(m.is_ok()); |
Kevin K | d20331b | 2016-11-20 14:48:16 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | #[test] |
Kevin K | 9fdad2e | 2016-05-02 18:12:57 | [diff] [blame] | 214 | fn required_unless() { |
| 215 | let res = App::new("unlesstest") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 216 | .arg( |
| 217 | Arg::with_name("cfg") |
| 218 | .required_unless("dbg") |
| 219 | .takes_value(true) |
| 220 | .long("config"), |
| 221 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 222 | .arg(Arg::with_name("dbg").long("debug")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 223 | .try_get_matches_from(vec!["unlesstest", "--debug"]); |
Kevin K | 9fdad2e | 2016-05-02 18:12:57 | [diff] [blame] | 224 | |
| 225 | assert!(res.is_ok()); |
| 226 | let m = res.unwrap(); |
| 227 | assert!(m.is_present("dbg")); |
| 228 | assert!(!m.is_present("cfg")); |
| 229 | } |
| 230 | |
| 231 | #[test] |
| 232 | fn required_unless_err() { |
| 233 | let res = App::new("unlesstest") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 234 | .arg( |
| 235 | Arg::with_name("cfg") |
| 236 | .required_unless("dbg") |
| 237 | .takes_value(true) |
| 238 | .long("config"), |
| 239 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 240 | .arg(Arg::with_name("dbg").long("debug")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 241 | .try_get_matches_from(vec!["unlesstest"]); |
Kevin K | 9fdad2e | 2016-05-02 18:12:57 | [diff] [blame] | 242 | |
| 243 | assert!(res.is_err()); |
| 244 | assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); |
| 245 | } |
| 246 | |
| 247 | // REQUIRED_UNLESS_ALL |
| 248 | |
| 249 | #[test] |
| 250 | fn required_unless_all() { |
| 251 | let res = App::new("unlessall") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 252 | .arg( |
| 253 | Arg::with_name("cfg") |
| 254 | .required_unless_all(&["dbg", "infile"]) |
| 255 | .takes_value(true) |
| 256 | .long("config"), |
| 257 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 258 | .arg(Arg::with_name("dbg").long("debug")) |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 259 | .arg(Arg::with_name("infile").short('i').takes_value(true)) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 260 | .try_get_matches_from(vec!["unlessall", "--debug", "-i", "file"]); |
Kevin K | 9fdad2e | 2016-05-02 18:12:57 | [diff] [blame] | 261 | |
| 262 | assert!(res.is_ok()); |
| 263 | let m = res.unwrap(); |
| 264 | assert!(m.is_present("dbg")); |
| 265 | assert!(m.is_present("infile")); |
| 266 | assert!(!m.is_present("cfg")); |
| 267 | } |
| 268 | |
| 269 | #[test] |
| 270 | fn required_unless_all_err() { |
| 271 | let res = App::new("unlessall") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 272 | .arg( |
| 273 | Arg::with_name("cfg") |
| 274 | .required_unless_all(&["dbg", "infile"]) |
| 275 | .takes_value(true) |
| 276 | .long("config"), |
| 277 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 278 | .arg(Arg::with_name("dbg").long("debug")) |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 279 | .arg(Arg::with_name("infile").short('i').takes_value(true)) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 280 | .try_get_matches_from(vec!["unlessall", "--debug"]); |
Kevin K | 9fdad2e | 2016-05-02 18:12:57 | [diff] [blame] | 281 | |
| 282 | assert!(res.is_err()); |
| 283 | assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); |
| 284 | } |
| 285 | |
| 286 | // REQUIRED_UNLESS_ONE |
| 287 | |
| 288 | #[test] |
| 289 | fn required_unless_one() { |
| 290 | let res = App::new("unlessone") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 291 | .arg( |
| 292 | Arg::with_name("cfg") |
| 293 | .required_unless_one(&["dbg", "infile"]) |
| 294 | .takes_value(true) |
| 295 | .long("config"), |
| 296 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 297 | .arg(Arg::with_name("dbg").long("debug")) |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 298 | .arg(Arg::with_name("infile").short('i').takes_value(true)) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 299 | .try_get_matches_from(vec!["unlessone", "--debug"]); |
Kevin K | 9fdad2e | 2016-05-02 18:12:57 | [diff] [blame] | 300 | |
| 301 | assert!(res.is_ok()); |
| 302 | let m = res.unwrap(); |
| 303 | assert!(m.is_present("dbg")); |
| 304 | assert!(!m.is_present("cfg")); |
| 305 | } |
| 306 | |
| 307 | #[test] |
Christopher Field | 1b99091 | 2016-07-23 17:43:22 | [diff] [blame] | 308 | fn required_unless_one_2() { |
| 309 | // This tests that the required_unless_one works when the second arg in the array is used |
| 310 | // instead of the first. |
| 311 | let res = App::new("unlessone") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 312 | .arg( |
| 313 | Arg::with_name("cfg") |
| 314 | .required_unless_one(&["dbg", "infile"]) |
| 315 | .takes_value(true) |
| 316 | .long("config"), |
| 317 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 318 | .arg(Arg::with_name("dbg").long("debug")) |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 319 | .arg(Arg::with_name("infile").short('i').takes_value(true)) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 320 | .try_get_matches_from(vec!["unlessone", "-i", "file"]); |
Christopher Field | 1b99091 | 2016-07-23 17:43:22 | [diff] [blame] | 321 | |
| 322 | assert!(res.is_ok()); |
| 323 | let m = res.unwrap(); |
| 324 | assert!(m.is_present("infile")); |
| 325 | assert!(!m.is_present("cfg")); |
| 326 | } |
| 327 | |
| 328 | #[test] |
Will Murphy | 63cc4bc | 2017-12-26 16:16:18 | [diff] [blame] | 329 | fn required_unless_one_works_with_short() { |
| 330 | // GitHub issue: https://github.com/kbknapp/clap-rs/issues/1135 |
Will Murphy | 63cc4bc | 2017-12-26 16:16:18 | [diff] [blame] | 331 | let res = App::new("unlessone") |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 332 | .arg(Arg::with_name("a").conflicts_with("b").short('a')) |
| 333 | .arg(Arg::with_name("b").short('b')) |
Will Murphy | 63cc4bc | 2017-12-26 16:16:18 | [diff] [blame] | 334 | .arg( |
| 335 | Arg::with_name("x") |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 336 | .short('x') |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 337 | .required_unless_one(&["a", "b"]), |
| 338 | ) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 339 | .try_get_matches_from(vec!["unlessone", "-a"]); |
Will Murphy | 63cc4bc | 2017-12-26 16:16:18 | [diff] [blame] | 340 | |
| 341 | assert!(res.is_ok()); |
| 342 | } |
| 343 | |
| 344 | #[test] |
Will Murphy | c759d20 | 2018-01-19 01:41:23 | [diff] [blame] | 345 | fn required_unless_one_works_with_short_err() { |
| 346 | let res = App::new("unlessone") |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 347 | .arg(Arg::with_name("a").conflicts_with("b").short('a')) |
| 348 | .arg(Arg::with_name("b").short('b')) |
Will Murphy | c759d20 | 2018-01-19 01:41:23 | [diff] [blame] | 349 | .arg( |
| 350 | Arg::with_name("x") |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 351 | .short('x') |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 352 | .required_unless_one(&["a", "b"]), |
| 353 | ) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 354 | .try_get_matches_from(vec!["unlessone"]); |
Will Murphy | c759d20 | 2018-01-19 01:41:23 | [diff] [blame] | 355 | |
| 356 | assert!(!res.is_ok()); |
| 357 | } |
| 358 | |
| 359 | #[test] |
Will Murphy | 74976a0 | 2018-01-19 01:30:12 | [diff] [blame] | 360 | fn required_unless_one_works_without() { |
Will Murphy | 74976a0 | 2018-01-19 01:30:12 | [diff] [blame] | 361 | let res = App::new("unlessone") |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 362 | .arg(Arg::with_name("a").conflicts_with("b").short('a')) |
| 363 | .arg(Arg::with_name("b").short('b')) |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 364 | .arg(Arg::with_name("x").required_unless_one(&["a", "b"])) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 365 | .try_get_matches_from(vec!["unlessone", "-a"]); |
Will Murphy | 74976a0 | 2018-01-19 01:30:12 | [diff] [blame] | 366 | |
| 367 | assert!(res.is_ok()); |
| 368 | } |
| 369 | |
| 370 | #[test] |
| 371 | fn required_unless_one_works_with_long() { |
| 372 | let res = App::new("unlessone") |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 373 | .arg(Arg::with_name("a").conflicts_with("b").short('a')) |
| 374 | .arg(Arg::with_name("b").short('b')) |
Will Murphy | 74976a0 | 2018-01-19 01:30:12 | [diff] [blame] | 375 | .arg( |
| 376 | Arg::with_name("x") |
| 377 | .long("x_is_the_option") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 378 | .required_unless_one(&["a", "b"]), |
| 379 | ) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 380 | .try_get_matches_from(vec!["unlessone", "-a"]); |
Will Murphy | 74976a0 | 2018-01-19 01:30:12 | [diff] [blame] | 381 | |
| 382 | assert!(res.is_ok()); |
| 383 | } |
| 384 | |
| 385 | #[test] |
Kevin K | 625cbbc | 2016-07-23 21:22:14 | [diff] [blame] | 386 | fn required_unless_one_1() { |
| 387 | let res = App::new("unlessone") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 388 | .arg( |
| 389 | Arg::with_name("cfg") |
| 390 | .required_unless_one(&["dbg", "infile"]) |
| 391 | .takes_value(true) |
| 392 | .long("config"), |
| 393 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 394 | .arg(Arg::with_name("dbg").long("debug")) |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 395 | .arg(Arg::with_name("infile").short('i').takes_value(true)) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 396 | .try_get_matches_from(vec!["unlessone", "--debug"]); |
Kevin K | 625cbbc | 2016-07-23 21:22:14 | [diff] [blame] | 397 | |
| 398 | assert!(res.is_ok()); |
| 399 | let m = res.unwrap(); |
| 400 | assert!(!m.is_present("infile")); |
| 401 | assert!(!m.is_present("cfg")); |
| 402 | assert!(m.is_present("dbg")); |
| 403 | } |
| 404 | |
| 405 | #[test] |
Kevin K | 9fdad2e | 2016-05-02 18:12:57 | [diff] [blame] | 406 | fn required_unless_one_err() { |
| 407 | let res = App::new("unlessone") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 408 | .arg( |
| 409 | Arg::with_name("cfg") |
| 410 | .required_unless_one(&["dbg", "infile"]) |
| 411 | .takes_value(true) |
| 412 | .long("config"), |
| 413 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 414 | .arg(Arg::with_name("dbg").long("debug")) |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 415 | .arg(Arg::with_name("infile").short('i').takes_value(true)) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 416 | .try_get_matches_from(vec!["unlessone"]); |
Kevin K | 9fdad2e | 2016-05-02 18:12:57 | [diff] [blame] | 417 | |
| 418 | assert!(res.is_err()); |
| 419 | assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); |
| 420 | } |
Kevin K | 852e581 | 2016-05-09 03:20:50 | [diff] [blame] | 421 | |
| 422 | #[test] |
| 423 | fn missing_required_output() { |
Pavan Kumar Sunkara | b7f76d8 | 2020-02-04 08:10:53 | [diff] [blame] | 424 | assert!(utils::compare_output( |
| 425 | utils::complex_app(), |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 426 | "clap-test -F", |
| 427 | MISSING_REQ, |
| 428 | true |
| 429 | )); |
Kevin K | 852e581 | 2016-05-09 03:20:50 | [diff] [blame] | 430 | } |
Kevin K | eca6091 | 2016-12-14 16:41:21 | [diff] [blame] | 431 | |
| 432 | // Conditional external requirements |
| 433 | |
| 434 | #[test] |
| 435 | fn requires_if_present_val() { |
| 436 | let res = App::new("unlessone") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 437 | .arg( |
| 438 | Arg::with_name("cfg") |
| 439 | .requires_if("my.cfg", "extra") |
| 440 | .takes_value(true) |
| 441 | .long("config"), |
| 442 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 443 | .arg(Arg::with_name("extra").long("extra")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 444 | .try_get_matches_from(vec!["unlessone", "--config=my.cfg"]); |
Kevin K | eca6091 | 2016-12-14 16:41:21 | [diff] [blame] | 445 | |
| 446 | assert!(res.is_err()); |
| 447 | assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); |
| 448 | } |
| 449 | |
| 450 | #[test] |
| 451 | fn requires_if_present_mult() { |
| 452 | let res = App::new("unlessone") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 453 | .arg( |
| 454 | Arg::with_name("cfg") |
| 455 | .requires_ifs(&[("my.cfg", "extra"), ("other.cfg", "other")]) |
| 456 | .takes_value(true) |
| 457 | .long("config"), |
| 458 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 459 | .arg(Arg::with_name("extra").long("extra")) |
| 460 | .arg(Arg::with_name("other").long("other")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 461 | .try_get_matches_from(vec!["unlessone", "--config=other.cfg"]); |
Kevin K | eca6091 | 2016-12-14 16:41:21 | [diff] [blame] | 462 | |
| 463 | assert!(res.is_err()); |
| 464 | assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); |
| 465 | } |
| 466 | |
| 467 | #[test] |
| 468 | fn requires_if_present_mult_pass() { |
| 469 | let res = App::new("unlessone") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 470 | .arg( |
| 471 | Arg::with_name("cfg") |
| 472 | .requires_ifs(&[("my.cfg", "extra"), ("other.cfg", "other")]) |
| 473 | .takes_value(true) |
| 474 | .long("config"), |
| 475 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 476 | .arg(Arg::with_name("extra").long("extra")) |
| 477 | .arg(Arg::with_name("other").long("other")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 478 | .try_get_matches_from(vec!["unlessone", "--config=some.cfg"]); |
Kevin K | eca6091 | 2016-12-14 16:41:21 | [diff] [blame] | 479 | |
| 480 | assert!(res.is_ok()); |
Kevin K | eca6091 | 2016-12-14 16:41:21 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | #[test] |
| 484 | fn requires_if_present_val_no_present_pass() { |
| 485 | let res = App::new("unlessone") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 486 | .arg( |
| 487 | Arg::with_name("cfg") |
| 488 | .requires_if("my.cfg", "extra") |
| 489 | .takes_value(true) |
| 490 | .long("config"), |
| 491 | ) |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 492 | .arg(Arg::with_name("extra").long("extra")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 493 | .try_get_matches_from(vec!["unlessone"]); |
Kevin K | eca6091 | 2016-12-14 16:41:21 | [diff] [blame] | 494 | |
| 495 | assert!(res.is_ok()); |
Kevin K | 60e1a3a | 2016-12-29 01:35:34 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // Conditionally required |
| 499 | |
| 500 | #[test] |
| 501 | fn required_if_val_present_pass() { |
| 502 | let res = App::new("ri") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 503 | .arg( |
| 504 | Arg::with_name("cfg") |
| 505 | .required_if("extra", "val") |
| 506 | .takes_value(true) |
| 507 | .long("config"), |
| 508 | ) |
| 509 | .arg(Arg::with_name("extra").takes_value(true).long("extra")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 510 | .try_get_matches_from(vec!["ri", "--extra", "val", "--config", "my.cfg"]); |
Kevin K | 60e1a3a | 2016-12-29 01:35:34 | [diff] [blame] | 511 | |
| 512 | assert!(res.is_ok()); |
Kevin K | 60e1a3a | 2016-12-29 01:35:34 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | #[test] |
| 516 | fn required_if_val_present_fail() { |
| 517 | let res = App::new("ri") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 518 | .arg( |
| 519 | Arg::with_name("cfg") |
| 520 | .required_if("extra", "val") |
| 521 | .takes_value(true) |
| 522 | .long("config"), |
| 523 | ) |
| 524 | .arg(Arg::with_name("extra").takes_value(true).long("extra")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 525 | .try_get_matches_from(vec!["ri", "--extra", "val"]); |
Kevin K | 60e1a3a | 2016-12-29 01:35:34 | [diff] [blame] | 526 | |
| 527 | assert!(res.is_err()); |
| 528 | assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); |
| 529 | } |
| 530 | |
| 531 | #[test] |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 532 | fn required_if_val_present_fail_error_output() { |
| 533 | let app = App::new("Test app") |
| 534 | .version("1.0") |
| 535 | .author("F0x06") |
| 536 | .about("Arg test") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 537 | .arg( |
| 538 | Arg::with_name("target") |
| 539 | .takes_value(true) |
| 540 | .required(true) |
| 541 | .possible_values(&["file", "stdout"]) |
| 542 | .long("target"), |
| 543 | ) |
| 544 | .arg( |
| 545 | Arg::with_name("input") |
| 546 | .takes_value(true) |
| 547 | .required(true) |
| 548 | .long("input"), |
| 549 | ) |
| 550 | .arg( |
| 551 | Arg::with_name("output") |
| 552 | .takes_value(true) |
| 553 | .required_if("target", "file") |
| 554 | .long("output"), |
| 555 | ); |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 556 | |
Pavan Kumar Sunkara | b7f76d8 | 2020-02-04 08:10:53 | [diff] [blame] | 557 | assert!(utils::compare_output( |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 558 | app, |
| 559 | "test --input somepath --target file", |
| 560 | COND_REQ_IN_USAGE, |
| 561 | true |
| 562 | )); |
Kevin K | 92919f5 | 2017-02-03 22:43:49 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | #[test] |
Kevin K | 60e1a3a | 2016-12-29 01:35:34 | [diff] [blame] | 566 | fn required_if_wrong_val() { |
| 567 | let res = App::new("ri") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 568 | .arg( |
| 569 | Arg::with_name("cfg") |
| 570 | .required_if("extra", "val") |
| 571 | .takes_value(true) |
| 572 | .long("config"), |
| 573 | ) |
| 574 | .arg(Arg::with_name("extra").takes_value(true).long("extra")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 575 | .try_get_matches_from(vec!["ri", "--extra", "other"]); |
Kevin K | 60e1a3a | 2016-12-29 01:35:34 | [diff] [blame] | 576 | |
| 577 | assert!(res.is_ok()); |
| 578 | } |
| 579 | |
| 580 | #[test] |
| 581 | fn required_ifs_val_present_pass() { |
| 582 | let res = App::new("ri") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 583 | .arg( |
| 584 | Arg::with_name("cfg") |
| 585 | .required_ifs(&[("extra", "val"), ("option", "spec")]) |
| 586 | .takes_value(true) |
| 587 | .long("config"), |
| 588 | ) |
| 589 | .arg(Arg::with_name("option").takes_value(true).long("option")) |
| 590 | .arg(Arg::with_name("extra").takes_value(true).long("extra")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 591 | .try_get_matches_from(vec!["ri", "--option", "spec", "--config", "my.cfg"]); |
Kevin K | 60e1a3a | 2016-12-29 01:35:34 | [diff] [blame] | 592 | |
| 593 | assert!(res.is_ok()); |
Kevin K | 60e1a3a | 2016-12-29 01:35:34 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | #[test] |
| 597 | fn required_ifs_val_present_fail() { |
| 598 | let res = App::new("ri") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 599 | .arg( |
| 600 | Arg::with_name("cfg") |
| 601 | .required_ifs(&[("extra", "val"), ("option", "spec")]) |
| 602 | .takes_value(true) |
| 603 | .long("config"), |
| 604 | ) |
| 605 | .arg(Arg::with_name("extra").takes_value(true).long("extra")) |
| 606 | .arg(Arg::with_name("option").takes_value(true).long("option")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 607 | .try_get_matches_from(vec!["ri", "--option", "spec"]); |
Kevin K | 60e1a3a | 2016-12-29 01:35:34 | [diff] [blame] | 608 | |
| 609 | assert!(res.is_err()); |
| 610 | assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); |
| 611 | } |
| 612 | |
| 613 | #[test] |
| 614 | fn required_ifs_wrong_val() { |
| 615 | let res = App::new("ri") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 616 | .arg( |
| 617 | Arg::with_name("cfg") |
| 618 | .required_ifs(&[("extra", "val"), ("option", "spec")]) |
| 619 | .takes_value(true) |
| 620 | .long("config"), |
| 621 | ) |
| 622 | .arg(Arg::with_name("extra").takes_value(true).long("extra")) |
| 623 | .arg(Arg::with_name("option").takes_value(true).long("option")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 624 | .try_get_matches_from(vec!["ri", "--option", "other"]); |
Kevin K | 60e1a3a | 2016-12-29 01:35:34 | [diff] [blame] | 625 | |
| 626 | assert!(res.is_ok()); |
| 627 | } |
| 628 | |
| 629 | #[test] |
| 630 | fn required_ifs_wrong_val_mult_fail() { |
| 631 | let res = App::new("ri") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 632 | .arg( |
| 633 | Arg::with_name("cfg") |
| 634 | .required_ifs(&[("extra", "val"), ("option", "spec")]) |
| 635 | .takes_value(true) |
| 636 | .long("config"), |
| 637 | ) |
| 638 | .arg(Arg::with_name("extra").takes_value(true).long("extra")) |
| 639 | .arg(Arg::with_name("option").takes_value(true).long("option")) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 640 | .try_get_matches_from(vec!["ri", "--extra", "other", "--option", "spec"]); |
Kevin K | 60e1a3a | 2016-12-29 01:35:34 | [diff] [blame] | 641 | |
| 642 | assert!(res.is_err()); |
| 643 | assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); |
Kevin K | 0efa411 | 2017-02-21 04:51:20 | [diff] [blame] | 644 | } |
Kevin K | c84416f | 2017-03-17 01:45:45 | [diff] [blame] | 645 | |
| 646 | #[test] |
| 647 | fn require_eq() { |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 648 | let app = App::new("clap-test").version("v1.4.8").arg( |
| 649 | Arg::with_name("opt") |
Kevin K | c84416f | 2017-03-17 01:45:45 | [diff] [blame] | 650 | .long("opt") |
Kevin K | 94872e0 | 2018-07-23 19:09:42 | [diff] [blame] | 651 | .short('o') |
Kevin K | c84416f | 2017-03-17 01:45:45 | [diff] [blame] | 652 | .required(true) |
| 653 | .require_equals(true) |
| 654 | .value_name("FILE") |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 655 | .help("some"), |
| 656 | ); |
Pavan Kumar Sunkara | b7f76d8 | 2020-02-04 08:10:53 | [diff] [blame] | 657 | assert!(utils::compare_output( |
| 658 | app, |
| 659 | "clap-test", |
| 660 | REQUIRE_EQUALS, |
| 661 | true |
| 662 | )); |
Kevin K | 1ab1027 | 2018-01-25 04:05:05 | [diff] [blame] | 663 | } |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 664 | |
Oleksii Filonenko | 1e39967 | 2019-10-02 13:27:19 | [diff] [blame] | 665 | static ISSUE_1158: &str = "error: The following required arguments were not provided: |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 666 | -x <X> |
| 667 | -y <Y> |
| 668 | -z <Z> |
| 669 | |
| 670 | USAGE: |
| 671 | example <ID> -x <X> -y <Y> -z <Z> |
| 672 | |
| 673 | For more information try --help"; |
| 674 | |
Kevin K | 4cc8599 | 2019-04-05 02:06:23 | [diff] [blame] | 675 | fn issue_1158_app() -> App<'static> { |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 676 | App::new("example") |
| 677 | .arg( |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 678 | Arg::from("-c, --config [FILE] 'Custom config file.'") |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 679 | .required_unless("ID") |
| 680 | .conflicts_with("ID"), |
| 681 | ) |
| 682 | .arg( |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 683 | Arg::from("[ID] 'ID'") |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 684 | .required_unless("config") |
| 685 | .conflicts_with("config") |
| 686 | .requires_all(&["x", "y", "z"]), |
| 687 | ) |
Kevin K | 0333380 | 2018-10-19 20:42:13 | [diff] [blame] | 688 | .arg(Arg::from("-x [X] 'X'")) |
| 689 | .arg(Arg::from("-y [Y] 'Y'")) |
| 690 | .arg(Arg::from("-z [Z] 'Z'")) |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | #[test] |
| 694 | fn issue_1158_conflicting_requirements() { |
| 695 | let app = issue_1158_app(); |
| 696 | |
Pavan Kumar Sunkara | b7f76d8 | 2020-02-04 08:10:53 | [diff] [blame] | 697 | assert!(utils::compare_output(app, "example id", ISSUE_1158, true)); |
Kevin K | 5a06a82 | 2018-07-25 18:46:28 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | #[test] |
| 701 | fn issue_1158_conflicting_requirements_rev() { |
| 702 | let res = issue_1158_app().try_get_matches_from(&["", "--config", "some.conf"]); |
| 703 | |
| 704 | assert!(res.is_ok()); |
| 705 | } |