1 // Std 2 use std::ops::BitOr; 3 #[cfg(feature = "yaml")] 4 use std::str::FromStr; 5 6 // Third party 7 use bitflags::bitflags; 8 9 #[doc(hidden)] 10 #[derive(Debug, Copy, Clone, PartialEq, Eq)] 11 pub struct AppFlags(Flags); 12 13 impl Default for AppFlags { default() -> Self14 fn default() -> Self { 15 AppFlags(Flags::COLOR_AUTO) 16 } 17 } 18 19 /// Application level settings, which affect how [`App`] operates 20 /// 21 /// **NOTE:** When these settings are used, they apply only to current command, and are *not* 22 /// propagated down or up through child or parent subcommands 23 /// 24 /// [`App`]: crate::App 25 #[derive(Debug, PartialEq, Copy, Clone)] 26 #[non_exhaustive] 27 pub enum AppSettings { 28 /// Try not to fail on parse errors, like missing option values. 29 /// 30 /// **Note:** Make sure you apply it as `global_setting` if you want this setting 31 /// to be propagated to subcommands and sub-subcommands! 32 /// 33 /// ```rust 34 /// # use clap::{App, arg, AppSettings}; 35 /// let app = App::new("app") 36 /// .global_setting(AppSettings::IgnoreErrors) 37 /// .arg(arg!(-c --config <FILE> "Sets a custom config file").required(false)) 38 /// .arg(arg!(-x --stuff <FILE> "Sets a custom stuff file").required(false)) 39 /// .arg(arg!(f: -f "Flag")); 40 /// 41 /// let r = app.try_get_matches_from(vec!["app", "-c", "file", "-f", "-x"]); 42 /// 43 /// assert!(r.is_ok(), "unexpected error: {:?}", r); 44 /// let m = r.unwrap(); 45 /// assert_eq!(m.value_of("config"), Some("file")); 46 /// assert!(m.is_present("f")); 47 /// assert_eq!(m.value_of("stuff"), None); 48 /// ``` 49 IgnoreErrors, 50 51 /// Display the message "Press \[ENTER\]/\[RETURN\] to continue..." and wait for user before 52 /// exiting 53 /// 54 /// This is most useful when writing an application which is run from a GUI shortcut, or on 55 /// Windows where a user tries to open the binary by double-clicking instead of using the 56 /// command line. 57 /// 58 /// # Examples 59 /// 60 /// ```rust 61 /// # use clap::{App, Arg, AppSettings}; 62 /// App::new("myprog") 63 /// .global_setting(AppSettings::WaitOnError); 64 /// ``` 65 WaitOnError, 66 67 /// Specifies that leading hyphens are allowed in all argument *values* (e.g. `-10`). 68 /// 69 /// Otherwise they will be parsed as another flag or option. See also 70 /// [`AppSettings::AllowNegativeNumbers`]. 71 /// 72 /// **NOTE:** Use this setting with caution as it silences certain circumstances which would 73 /// otherwise be an error (such as accidentally forgetting to specify a value for leading 74 /// option). It is preferred to set this on a per argument basis, via [`Arg::allow_hyphen_values`]. 75 /// 76 /// # Examples 77 /// 78 /// ```rust 79 /// # use clap::{Arg, App, AppSettings}; 80 /// // Imagine you needed to represent negative numbers as well, such as -10 81 /// let m = App::new("nums") 82 /// .setting(AppSettings::AllowHyphenValues) 83 /// .arg(Arg::new("neg")) 84 /// .get_matches_from(vec![ 85 /// "nums", "-20" 86 /// ]); 87 /// 88 /// assert_eq!(m.value_of("neg"), Some("-20")); 89 /// # ; 90 /// ``` 91 /// [`Arg::allow_hyphen_values`]: crate::Arg::allow_hyphen_values() 92 AllowHyphenValues, 93 94 /// Allows negative numbers to pass as values. 95 /// 96 /// This is similar to [`AppSettings::AllowHyphenValues`] except that it only allows numbers, 97 /// all other undefined leading hyphens will fail to parse. 98 /// 99 /// # Examples 100 /// 101 /// ```rust 102 /// # use clap::{App, Arg, AppSettings}; 103 /// let res = App::new("myprog") 104 /// .global_setting(AppSettings::AllowNegativeNumbers) 105 /// .arg(Arg::new("num")) 106 /// .try_get_matches_from(vec![ 107 /// "myprog", "-20" 108 /// ]); 109 /// assert!(res.is_ok()); 110 /// let m = res.unwrap(); 111 /// assert_eq!(m.value_of("num").unwrap(), "-20"); 112 /// ``` 113 AllowNegativeNumbers, 114 115 /// Specifies that all arguments override themselves. 116 /// 117 /// This is the equivalent to saying the `foo` arg using [`Arg::overrides_with("foo")`] for all 118 /// defined arguments. 119 /// 120 /// [`Arg::overrides_with("foo")`]: crate::Arg::overrides_with() 121 AllArgsOverrideSelf, 122 123 /// Allows one to implement two styles of CLIs where positionals can be used out of order. 124 /// 125 /// The first example is a CLI where the second to last positional argument is optional, but 126 /// the final positional argument is required. Such as `$ prog [optional] <required>` where one 127 /// of the two following usages is allowed: 128 /// 129 /// * `$ prog [optional] <required>` 130 /// * `$ prog <required>` 131 /// 132 /// This would otherwise not be allowed. This is useful when `[optional]` has a default value. 133 /// 134 /// **Note:** when using this style of "missing positionals" the final positional *must* be 135 /// [required] if `--` will not be used to skip to the final positional argument. 136 /// 137 /// **Note:** This style also only allows a single positional argument to be "skipped" without 138 /// the use of `--`. To skip more than one, see the second example. 139 /// 140 /// The second example is when one wants to skip multiple optional positional arguments, and use 141 /// of the `--` operator is OK (but not required if all arguments will be specified anyways). 142 /// 143 /// For example, imagine a CLI which has three positional arguments `[foo] [bar] [baz]...` where 144 /// `baz` accepts multiple values (similar to man `ARGS...` style training arguments). 145 /// 146 /// With this setting the following invocations are posisble: 147 /// 148 /// * `$ prog foo bar baz1 baz2 baz3` 149 /// * `$ prog foo -- baz1 baz2 baz3` 150 /// * `$ prog -- baz1 baz2 baz3` 151 /// 152 /// # Examples 153 /// 154 /// Style number one from above: 155 /// 156 /// ```rust 157 /// # use clap::{App, Arg, AppSettings}; 158 /// // Assume there is an external subcommand named "subcmd" 159 /// let m = App::new("myprog") 160 /// .setting(AppSettings::AllowMissingPositional) 161 /// .arg(Arg::new("arg1")) 162 /// .arg(Arg::new("arg2") 163 /// .required(true)) 164 /// .get_matches_from(vec![ 165 /// "prog", "other" 166 /// ]); 167 /// 168 /// assert_eq!(m.value_of("arg1"), None); 169 /// assert_eq!(m.value_of("arg2"), Some("other")); 170 /// ``` 171 /// 172 /// Now the same example, but using a default value for the first optional positional argument 173 /// 174 /// ```rust 175 /// # use clap::{App, Arg, AppSettings}; 176 /// // Assume there is an external subcommand named "subcmd" 177 /// let m = App::new("myprog") 178 /// .setting(AppSettings::AllowMissingPositional) 179 /// .arg(Arg::new("arg1") 180 /// .default_value("something")) 181 /// .arg(Arg::new("arg2") 182 /// .required(true)) 183 /// .get_matches_from(vec![ 184 /// "prog", "other" 185 /// ]); 186 /// 187 /// assert_eq!(m.value_of("arg1"), Some("something")); 188 /// assert_eq!(m.value_of("arg2"), Some("other")); 189 /// ``` 190 /// 191 /// Style number two from above: 192 /// 193 /// ```rust 194 /// # use clap::{App, Arg, AppSettings}; 195 /// // Assume there is an external subcommand named "subcmd" 196 /// let m = App::new("myprog") 197 /// .setting(AppSettings::AllowMissingPositional) 198 /// .arg(Arg::new("foo")) 199 /// .arg(Arg::new("bar")) 200 /// .arg(Arg::new("baz").takes_value(true).multiple_values(true)) 201 /// .get_matches_from(vec![ 202 /// "prog", "foo", "bar", "baz1", "baz2", "baz3" 203 /// ]); 204 /// 205 /// assert_eq!(m.value_of("foo"), Some("foo")); 206 /// assert_eq!(m.value_of("bar"), Some("bar")); 207 /// assert_eq!(m.values_of("baz").unwrap().collect::<Vec<_>>(), &["baz1", "baz2", "baz3"]); 208 /// ``` 209 /// 210 /// Now nofice if we don't specify `foo` or `baz` but use the `--` operator. 211 /// 212 /// ```rust 213 /// # use clap::{App, Arg, AppSettings}; 214 /// // Assume there is an external subcommand named "subcmd" 215 /// let m = App::new("myprog") 216 /// .setting(AppSettings::AllowMissingPositional) 217 /// .arg(Arg::new("foo")) 218 /// .arg(Arg::new("bar")) 219 /// .arg(Arg::new("baz").takes_value(true).multiple_values(true)) 220 /// .get_matches_from(vec![ 221 /// "prog", "--", "baz1", "baz2", "baz3" 222 /// ]); 223 /// 224 /// assert_eq!(m.value_of("foo"), None); 225 /// assert_eq!(m.value_of("bar"), None); 226 /// assert_eq!(m.values_of("baz").unwrap().collect::<Vec<_>>(), &["baz1", "baz2", "baz3"]); 227 /// ``` 228 /// 229 /// [required]: crate::Arg::required() 230 AllowMissingPositional, 231 232 /// Specifies that the final positional argument is a "VarArg" and that `clap` should not 233 /// attempt to parse any further args. 234 /// 235 /// The values of the trailing positional argument will contain all args from itself on. 236 /// 237 /// **NOTE:** The final positional argument **must** have [`Arg::multiple_values(true)`] or the usage 238 /// string equivalent. 239 /// 240 /// # Examples 241 /// 242 /// ```rust 243 /// # use clap::{App, arg, AppSettings}; 244 /// let m = App::new("myprog") 245 /// .setting(AppSettings::TrailingVarArg) 246 /// .arg(arg!(<cmd> ... "commands to run")) 247 /// .get_matches_from(vec!["myprog", "arg1", "-r", "val1"]); 248 /// 249 /// let trail: Vec<&str> = m.values_of("cmd").unwrap().collect(); 250 /// assert_eq!(trail, ["arg1", "-r", "val1"]); 251 /// ``` 252 /// [`Arg::multiple_values(true)`]: crate::Arg::multiple_values() 253 TrailingVarArg, 254 255 /// Disables the automatic delimiting of values when `--` or [`AppSettings::TrailingVarArg`] 256 /// was used. 257 /// 258 /// **NOTE:** The same thing can be done manually by setting the final positional argument to 259 /// [`Arg::use_delimiter(false)`]. Using this setting is safer, because it's easier to locate 260 /// when making changes. 261 /// 262 /// # Examples 263 /// 264 /// ```no_run 265 /// # use clap::{App, Arg, AppSettings}; 266 /// App::new("myprog") 267 /// .setting(AppSettings::DontDelimitTrailingValues) 268 /// .get_matches(); 269 /// ``` 270 /// 271 /// [`Arg::use_delimiter(false)`]: crate::Arg::use_delimiter() 272 DontDelimitTrailingValues, 273 274 /// Allow partial matches of long arguments or their [aliases]. 275 /// 276 /// For example, to match an argument named `--test`, one could use `--t`, `--te`, `--tes`, and 277 /// `--test`. 278 /// 279 /// **NOTE:** The match *must not* be ambiguous at all in order to succeed. i.e. to match 280 /// `--te` to `--test` there could not also be another argument or alias `--temp` because both 281 /// start with `--te` 282 /// 283 /// [aliases]: crate::App::aliases() 284 InferLongArgs, 285 286 /// Allow partial matches of [subcommand] names and their [aliases]. 287 /// 288 /// For example, to match a subcommand named `test`, one could use `t`, `te`, `tes`, and 289 /// `test`. 290 /// 291 /// **NOTE:** The match *must not* be ambiguous at all in order to succeed. i.e. to match `te` 292 /// to `test` there could not also be a subcommand or alias `temp` because both start with `te` 293 /// 294 /// **CAUTION:** This setting can interfere with [positional/free arguments], take care when 295 /// designing CLIs which allow inferred subcommands and have potential positional/free 296 /// arguments whose values could start with the same characters as subcommands. If this is the 297 /// case, it's recommended to use settings such as [`AppSettings::ArgsNegateSubcommands`] in 298 /// conjunction with this setting. 299 /// 300 /// # Examples 301 /// 302 /// ```no_run 303 /// # use clap::{App, Arg, AppSettings}; 304 /// let m = App::new("prog") 305 /// .global_setting(AppSettings::InferSubcommands) 306 /// .subcommand(App::new("test")) 307 /// .get_matches_from(vec![ 308 /// "prog", "te" 309 /// ]); 310 /// assert_eq!(m.subcommand_name(), Some("test")); 311 /// ``` 312 /// 313 /// [subcommand]: crate::App::subcommand() 314 /// [positional/free arguments]: crate::Arg::index() 315 /// [aliases]: crate::App::aliases() 316 InferSubcommands, 317 318 /// If no [`subcommand`] is present at runtime, error and exit gracefully. 319 /// 320 /// # Examples 321 /// 322 /// ```rust 323 /// # use clap::{App, AppSettings, ErrorKind}; 324 /// let err = App::new("myprog") 325 /// .setting(AppSettings::SubcommandRequired) 326 /// .subcommand(App::new("test")) 327 /// .try_get_matches_from(vec![ 328 /// "myprog", 329 /// ]); 330 /// assert!(err.is_err()); 331 /// assert_eq!(err.unwrap_err().kind, ErrorKind::MissingSubcommand); 332 /// # ; 333 /// ``` 334 /// 335 /// [`subcommand`]: crate::App::subcommand() 336 SubcommandRequired, 337 338 /// Display help if no [`subcommands`] are present at runtime and exit gracefully (i.e. an 339 /// empty run such as `$ myprog`). 340 /// 341 /// **NOTE:** This should *not* be used with [`AppSettings::SubcommandRequired`] as they do 342 /// nearly same thing; this prints the help text, and the other prints an error. 343 /// 344 /// **NOTE:** If the user specifies arguments at runtime, but no subcommand the help text will 345 /// still be displayed and exit. If this is *not* the desired result, consider using 346 /// [`AppSettings::ArgRequiredElseHelp`] instead. 347 /// 348 /// # Examples 349 /// 350 /// ```rust 351 /// # use clap::{App, Arg, AppSettings}; 352 /// App::new("myprog") 353 /// .setting(AppSettings::SubcommandRequiredElseHelp); 354 /// ``` 355 /// 356 /// [`subcommands`]: crate::App::subcommand() 357 SubcommandRequiredElseHelp, 358 359 /// Assume unexpected positional arguments are a [`subcommand`]. 360 /// 361 /// **NOTE:** Use this setting with caution, 362 /// as a truly unexpected argument (i.e. one that is *NOT* an external subcommand) 363 /// will **not** cause an error and instead be treated as a potential subcommand. 364 /// One should check for such cases manually and inform the user appropriately. 365 /// 366 /// # Examples 367 /// 368 /// ```rust 369 /// # use clap::{App, AppSettings}; 370 /// // Assume there is an external subcommand named "subcmd" 371 /// let m = App::new("myprog") 372 /// .setting(AppSettings::AllowExternalSubcommands) 373 /// .get_matches_from(vec![ 374 /// "myprog", "subcmd", "--option", "value", "-fff", "--flag" 375 /// ]); 376 /// 377 /// // All trailing arguments will be stored under the subcommand's sub-matches using an empty 378 /// // string argument name 379 /// match m.subcommand() { 380 /// Some((external, ext_m)) => { 381 /// let ext_args: Vec<&str> = ext_m.values_of("").unwrap().collect(); 382 /// assert_eq!(external, "subcmd"); 383 /// assert_eq!(ext_args, ["--option", "value", "-fff", "--flag"]); 384 /// }, 385 /// _ => {}, 386 /// } 387 /// ``` 388 /// 389 /// [`subcommand`]: crate::App::subcommand() 390 /// [`ArgMatches`]: crate::ArgMatches 391 /// [`ErrorKind::UnknownArgument`]: crate::ErrorKind::UnknownArgument 392 AllowExternalSubcommands, 393 394 /// Strip directory path from argv\[0\] and use as an argument. 395 /// 396 /// A "multicall" executable is a single executable 397 /// that contains a variety of applets, 398 /// and decides which applet to run based on the name of the file. 399 /// The executable can be called from different names by creating hard links 400 /// or symbolic links to it. 401 /// 402 /// This is desirable when it is convenient to store code 403 /// for many programs in the same file, 404 /// such as deduplicating code across multiple programs 405 /// without loading a shared library at runtime. 406 /// 407 /// Multicall can't be used with [`NoBinaryName`] since they interpret 408 /// the command name in incompatible ways. 409 /// 410 /// # Examples 411 /// 412 /// `hostname` is an example of a multicall executable. 413 /// Both `hostname` and `dnsdomainname` are provided by the same executable 414 /// and which behaviour to use is based on the executable file name. 415 /// 416 /// This is desirable when the executable has a primary purpose 417 /// but there is other related functionality that would be convenient to provide 418 /// and it is convenient for the code to implement it to be in the same executable. 419 /// 420 /// The name of the app is essentially unused 421 /// and may be the same as the name of a subcommand. 422 /// 423 /// The names of the immediate subcommands of the App 424 /// are matched against the basename of the first argument, 425 /// which is conventionally the path of the executable. 426 /// 427 /// This does not allow the subcommand to be passed as the first non-path argument. 428 /// 429 /// ```rust 430 /// # use clap::{App, AppSettings, ErrorKind}; 431 /// let mut app = App::new("hostname") 432 /// .setting(AppSettings::Multicall) 433 /// .subcommand(App::new("hostname")) 434 /// .subcommand(App::new("dnsdomainname")); 435 /// let m = app.try_get_matches_from_mut(&["/usr/bin/hostname", "dnsdomainname"]); 436 /// assert!(m.is_err()); 437 /// assert_eq!(m.unwrap_err().kind, ErrorKind::UnknownArgument); 438 /// let m = app.get_matches_from(&["/usr/bin/dnsdomainname"]); 439 /// assert_eq!(m.subcommand_name(), Some("dnsdomainname")); 440 /// ``` 441 /// 442 /// Busybox is another common example of a multicall executable 443 /// with a subcommmand for each applet that can be run directly, 444 /// e.g. with the `cat` applet being run by running `busybox cat`, 445 /// or with `cat` as a link to the `busybox` binary. 446 /// 447 /// This is desirable when the launcher program has additional options 448 /// or it is useful to run the applet without installing a symlink 449 /// e.g. to test the applet without installing it 450 /// or there may already be a command of that name installed. 451 /// 452 /// To make an applet usable as both a multicall link and a subcommand 453 /// the subcommands must be defined both in the top-level App 454 /// and as subcommands of the "main" applet. 455 /// 456 /// ```rust 457 /// # use clap::{App, AppSettings}; 458 /// fn applet_commands() -> [App<'static>; 2] { 459 /// [App::new("true"), App::new("false")] 460 /// } 461 /// let mut app = App::new("busybox") 462 /// .setting(AppSettings::Multicall) 463 /// .subcommand( 464 /// App::new("busybox") 465 /// .subcommand_value_name("APPLET") 466 /// .subcommand_help_heading("APPLETS") 467 /// .subcommands(applet_commands()), 468 /// ) 469 /// .subcommands(applet_commands()); 470 /// // When called from the executable's canonical name 471 /// // its applets can be matched as subcommands. 472 /// let m = app.try_get_matches_from_mut(&["/usr/bin/busybox", "true"]).unwrap(); 473 /// assert_eq!(m.subcommand_name(), Some("busybox")); 474 /// assert_eq!(m.subcommand().unwrap().1.subcommand_name(), Some("true")); 475 /// // When called from a link named after an applet that applet is matched. 476 /// let m = app.get_matches_from(&["/usr/bin/true"]); 477 /// assert_eq!(m.subcommand_name(), Some("true")); 478 /// ``` 479 /// 480 /// **NOTE:** Applets are slightly semantically different from subcommands, 481 /// so it's recommended to use [`App::subcommand_help_heading`] and 482 /// [`App::subcommand_value_name`] to change the descriptive text as above. 483 /// 484 /// [`NoBinaryName`]: crate::AppSettings::NoBinaryName 485 /// [`App::subcommand_value_name`]: crate::App::subcommand_value_name 486 /// [`App::subcommand_help_heading`]: crate::App::subcommand_help_heading 487 #[cfg(feature = "unstable-multicall")] 488 Multicall, 489 490 /// Specifies that external subcommands that are invalid UTF-8 should *not* be treated as an error. 491 /// 492 /// **NOTE:** Using external subcommand argument values with invalid UTF-8 requires using 493 /// [`ArgMatches::values_of_os`] or [`ArgMatches::values_of_lossy`] for those particular 494 /// arguments which may contain invalid UTF-8 values 495 /// 496 /// **NOTE:** Setting this requires [`AppSettings::AllowExternalSubcommands`] 497 /// 498 /// # Platform Specific 499 /// 500 /// Non Windows systems only 501 /// 502 /// # Examples 503 /// 504 #[cfg_attr(not(unix), doc = " ```ignore")] 505 #[cfg_attr(unix, doc = " ```")] 506 /// # use clap::{App, AppSettings}; 507 /// // Assume there is an external subcommand named "subcmd" 508 /// let m = App::new("myprog") 509 /// .setting(AppSettings::AllowInvalidUtf8ForExternalSubcommands) 510 /// .setting(AppSettings::AllowExternalSubcommands) 511 /// .get_matches_from(vec![ 512 /// "myprog", "subcmd", "--option", "value", "-fff", "--flag" 513 /// ]); 514 /// 515 /// // All trailing arguments will be stored under the subcommand's sub-matches using an empty 516 /// // string argument name 517 /// match m.subcommand() { 518 /// Some((external, ext_m)) => { 519 /// let ext_args: Vec<&std::ffi::OsStr> = ext_m.values_of_os("").unwrap().collect(); 520 /// assert_eq!(external, "subcmd"); 521 /// assert_eq!(ext_args, ["--option", "value", "-fff", "--flag"]); 522 /// }, 523 /// _ => {}, 524 /// } 525 /// ``` 526 /// 527 /// [`ArgMatches::values_of_os`]: crate::ArgMatches::values_of_os() 528 /// [`ArgMatches::values_of_lossy`]: crate::ArgMatches::values_of_lossy() 529 /// [`subcommands`]: crate::App::subcommand() 530 AllowInvalidUtf8ForExternalSubcommands, 531 532 /// Specifies that the help subcommand should print the long help message (`--help`). 533 /// 534 /// **NOTE:** This setting is useless if [`AppSettings::DisableHelpSubcommand`] or [`AppSettings::NoAutoHelp`] is set, 535 /// or if the app contains no subcommands at all. 536 /// 537 /// # Examples 538 /// 539 /// ```rust 540 /// # use clap::{App, Arg, AppSettings}; 541 /// App::new("myprog") 542 /// .global_setting(AppSettings::UseLongFormatForHelpSubcommand) 543 /// .subcommand(App::new("test") 544 /// .arg(Arg::new("foo") 545 /// .help("short form about message") 546 /// .long_help("long form about message") 547 /// ) 548 /// ) 549 /// .get_matches(); 550 /// ``` 551 /// [long format]: crate::App::long_about 552 UseLongFormatForHelpSubcommand, 553 554 /// Allows [`subcommands`] to override all requirements of the parent command. 555 /// 556 /// For example, if you had a subcommand or top level application with a required argument 557 /// that is only required as long as there is no subcommand present, 558 /// using this setting would allow you to set those arguments to [`Arg::required(true)`] 559 /// and yet receive no error so long as the user uses a valid subcommand instead. 560 /// 561 /// **NOTE:** This defaults to false (using subcommand does *not* negate requirements) 562 /// 563 /// # Examples 564 /// 565 /// This first example shows that it is an error to not use a required argument 566 /// 567 /// ```rust 568 /// # use clap::{App, Arg, AppSettings, ErrorKind}; 569 /// let err = App::new("myprog") 570 /// .setting(AppSettings::SubcommandsNegateReqs) 571 /// .arg(Arg::new("opt").required(true)) 572 /// .subcommand(App::new("test")) 573 /// .try_get_matches_from(vec![ 574 /// "myprog" 575 /// ]); 576 /// assert!(err.is_err()); 577 /// assert_eq!(err.unwrap_err().kind, ErrorKind::MissingRequiredArgument); 578 /// # ; 579 /// ``` 580 /// 581 /// This next example shows that it is no longer error to not use a required argument if a 582 /// valid subcommand is used. 583 /// 584 /// ```rust 585 /// # use clap::{App, Arg, AppSettings, ErrorKind}; 586 /// let noerr = App::new("myprog") 587 /// .setting(AppSettings::SubcommandsNegateReqs) 588 /// .arg(Arg::new("opt").required(true)) 589 /// .subcommand(App::new("test")) 590 /// .try_get_matches_from(vec![ 591 /// "myprog", "test" 592 /// ]); 593 /// assert!(noerr.is_ok()); 594 /// # ; 595 /// ``` 596 /// 597 /// [`Arg::required(true)`]: crate::Arg::required() 598 /// [`subcommands`]: crate::App::subcommand() 599 SubcommandsNegateReqs, 600 601 /// Specifies that use of an argument prevents the use of [`subcommands`]. 602 /// 603 /// By default `clap` allows arguments between subcommands such 604 /// as `<cmd> [cmd_args] <subcmd> [subcmd_args] <subsubcmd> [subsubcmd_args]`. 605 /// 606 /// This setting disables that functionality and says that arguments can 607 /// only follow the *final* subcommand. For instance using this setting 608 /// makes only the following invocations possible: 609 /// 610 /// * `<cmd> <subcmd> <subsubcmd> [subsubcmd_args]` 611 /// * `<cmd> <subcmd> [subcmd_args]` 612 /// * `<cmd> [cmd_args]` 613 /// 614 /// # Examples 615 /// 616 /// ```rust 617 /// # use clap::{App, AppSettings}; 618 /// App::new("myprog") 619 /// .setting(AppSettings::ArgsNegateSubcommands); 620 /// ``` 621 /// 622 /// [`subcommands`]: crate::App::subcommand() 623 ArgsNegateSubcommands, 624 625 /// Prevent subcommands from being consumed as an arguments value. 626 /// 627 /// By default, if an option taking multiple values is followed by a subcommand, the 628 /// subcommand will be parsed as another value. 629 /// 630 /// ```text 631 /// app --foo val1 val2 subcommand 632 /// --------- ---------- 633 /// values another value 634 /// ``` 635 /// 636 /// This setting instructs the parser to stop when encountering a subcommand instead of 637 /// greedily consuming arguments. 638 /// 639 /// ```text 640 /// app --foo val1 val2 subcommand 641 /// --------- ---------- 642 /// values subcommand 643 /// ``` 644 /// 645 /// **Note:** Make sure you apply it as `global_setting` if you want this setting 646 /// to be propagated to subcommands and sub-subcommands! 647 /// 648 /// # Examples 649 /// 650 /// ```rust 651 /// # use clap::{App, AppSettings, Arg}; 652 /// let app = App::new("app").subcommand(App::new("sub")).arg( 653 /// Arg::new("arg") 654 /// .long("arg") 655 /// .multiple_values(true) 656 /// .takes_value(true), 657 /// ); 658 /// 659 /// let matches = app 660 /// .clone() 661 /// .try_get_matches_from(&["app", "--arg", "1", "2", "3", "sub"]) 662 /// .unwrap(); 663 /// 664 /// assert_eq!( 665 /// matches.values_of("arg").unwrap().collect::<Vec<_>>(), 666 /// &["1", "2", "3", "sub"] 667 /// ); 668 /// assert!(matches.subcommand_matches("sub").is_none()); 669 /// 670 /// let matches = app 671 /// .setting(AppSettings::SubcommandPrecedenceOverArg) 672 /// .try_get_matches_from(&["app", "--arg", "1", "2", "3", "sub"]) 673 /// .unwrap(); 674 /// 675 /// assert_eq!( 676 /// matches.values_of("arg").unwrap().collect::<Vec<_>>(), 677 /// &["1", "2", "3"] 678 /// ); 679 /// assert!(matches.subcommand_matches("sub").is_some()); 680 /// ``` 681 SubcommandPrecedenceOverArg, 682 683 /// Exit gracefully if no arguments are present (e.g. `$ myprog`). 684 /// 685 /// **NOTE:** [`subcommands`] count as arguments 686 /// 687 /// **NOTE:** Setting [`Arg::default_value`] effectively disables this option as it will 688 /// ensure that some argument is always present. 689 /// 690 /// # Examples 691 /// 692 /// ```rust 693 /// # use clap::{App, AppSettings}; 694 /// App::new("myprog") 695 /// .setting(AppSettings::ArgRequiredElseHelp); 696 /// ``` 697 /// 698 /// [`subcommands`]: crate::App::subcommand() 699 /// [`Arg::default_value`]: crate::Arg::default_value() 700 ArgRequiredElseHelp, 701 702 /// Displays the arguments and [`subcommands`] in the help message in the order that they were 703 /// declared in, and not alphabetically which is the default. 704 /// 705 /// To override the declaration order, see [`Arg::display_order`] and [`App::display_order`]. 706 /// 707 /// # Examples 708 /// 709 /// ```no_run 710 /// # use clap::{App, Arg, AppSettings}; 711 /// App::new("myprog") 712 /// .global_setting(AppSettings::DeriveDisplayOrder) 713 /// .get_matches(); 714 /// ``` 715 /// 716 /// [`subcommands`]: crate::App::subcommand() 717 /// [`Arg::display_order`]: crate::Arg::display_order 718 /// [`App::display_order`]: crate::App::display_order 719 DeriveDisplayOrder, 720 721 /// Disables the automatic collapsing of positional args into `[ARGS]` inside the usage string. 722 /// 723 /// # Examples 724 /// 725 /// ```no_run 726 /// # use clap::{App, Arg, AppSettings}; 727 /// App::new("myprog") 728 /// .global_setting(AppSettings::DontCollapseArgsInUsage) 729 /// .get_matches(); 730 /// ``` 731 DontCollapseArgsInUsage, 732 733 /// Places the help string for all arguments on the line after the argument. 734 /// 735 /// # Examples 736 /// 737 /// ```no_run 738 /// # use clap::{App, Arg, AppSettings}; 739 /// App::new("myprog") 740 /// .global_setting(AppSettings::NextLineHelp) 741 /// .get_matches(); 742 /// ``` 743 NextLineHelp, 744 745 /// Disables colorized help messages. 746 /// 747 /// # Examples 748 /// 749 /// ```no_run 750 /// # use clap::{App, AppSettings}; 751 /// App::new("myprog") 752 /// .setting(AppSettings::DisableColoredHelp) 753 /// .get_matches(); 754 /// ``` 755 DisableColoredHelp, 756 757 /// Disables `-h` and `--help` flag. 758 /// 759 /// # Examples 760 /// 761 /// ```rust 762 /// # use clap::{App, AppSettings, ErrorKind}; 763 /// let res = App::new("myprog") 764 /// .setting(AppSettings::DisableHelpFlag) 765 /// .try_get_matches_from(vec![ 766 /// "myprog", "-h" 767 /// ]); 768 /// assert!(res.is_err()); 769 /// assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument); 770 /// ``` 771 DisableHelpFlag, 772 773 /// Disables the `help` [`subcommand`]. 774 /// 775 /// # Examples 776 /// 777 /// ```rust 778 /// # use clap::{App, AppSettings, ErrorKind, }; 779 /// let res = App::new("myprog") 780 /// .setting(AppSettings::DisableHelpSubcommand) 781 /// // Normally, creating a subcommand causes a `help` subcommand to automatically 782 /// // be generated as well 783 /// .subcommand(App::new("test")) 784 /// .try_get_matches_from(vec![ 785 /// "myprog", "help" 786 /// ]); 787 /// assert!(res.is_err()); 788 /// assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument); 789 /// ``` 790 /// 791 /// [`subcommand`]: crate::App::subcommand() 792 DisableHelpSubcommand, 793 794 /// Disables `-V` and `--version` flag. 795 /// 796 /// # Examples 797 /// 798 /// ```rust 799 /// # use clap::{App, AppSettings, ErrorKind}; 800 /// let res = App::new("myprog") 801 /// .setting(AppSettings::DisableVersionFlag) 802 /// .try_get_matches_from(vec![ 803 /// "myprog", "-V" 804 /// ]); 805 /// assert!(res.is_err()); 806 /// assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument); 807 /// ``` 808 DisableVersionFlag, 809 810 /// Specifies to use the version of the current command for all [`subcommands`]. 811 /// 812 /// Defaults to `false`; subcommands have independent version strings from their parents. 813 /// 814 /// **Note:** Make sure you apply it as `global_setting` if you want this setting 815 /// to be propagated to subcommands and sub-subcommands! 816 /// 817 /// # Examples 818 /// 819 /// ```no_run 820 /// # use clap::{App, Arg, AppSettings}; 821 /// App::new("myprog") 822 /// .version("v1.1") 823 /// .global_setting(AppSettings::PropagateVersion) 824 /// .subcommand(App::new("test")) 825 /// .get_matches(); 826 /// // running `$ myprog test --version` will display 827 /// // "myprog-test v1.1" 828 /// ``` 829 /// 830 /// [`subcommands`]: crate::App::subcommand() 831 PropagateVersion, 832 833 /// Specifies that this [`subcommand`] should be hidden from help messages 834 /// 835 /// # Examples 836 /// 837 /// ```rust 838 /// # use clap::{App, Arg, AppSettings, }; 839 /// App::new("myprog") 840 /// .subcommand(App::new("test") 841 /// .setting(AppSettings::Hidden)) 842 /// # ; 843 /// ``` 844 /// 845 /// [`subcommand`]: crate::App::subcommand() 846 Hidden, 847 848 /// Tells `clap` *not* to print possible values when displaying help information. 849 /// 850 /// This can be useful if there are many values, or they are explained elsewhere. 851 /// 852 /// To set this per argument, see 853 /// [`Arg::hide_possible_values`][crate::Arg::hide_possible_values]. 854 HidePossibleValues, 855 856 /// Panic if help descriptions are omitted. 857 /// 858 /// **NOTE:** When deriving [`Parser`][crate::Parser], you could instead check this at 859 /// compile-time with `#![deny(missing_docs)]` 860 /// 861 /// # Examples 862 /// 863 /// ```rust 864 /// # use clap::{App, Arg, AppSettings}; 865 /// App::new("myprog") 866 /// .global_setting(AppSettings::HelpExpected) 867 /// .arg( 868 /// Arg::new("foo").help("It does foo stuff") 869 /// // As required via AppSettings::HelpExpected, a help message was supplied 870 /// ) 871 /// # .get_matches(); 872 /// ``` 873 /// 874 /// # Panics 875 /// 876 /// ```rust,no_run 877 /// # use clap::{App, Arg, AppSettings}; 878 /// App::new("myapp") 879 /// .global_setting(AppSettings::HelpExpected) 880 /// .arg( 881 /// Arg::new("foo") 882 /// // Someone forgot to put .about("...") here 883 /// // Since the setting AppSettings::HelpExpected is activated, this will lead to 884 /// // a panic (if you are in debug mode) 885 /// ) 886 /// # .get_matches(); 887 ///``` 888 HelpExpected, 889 890 /// Specifies that the parser should not assume the first argument passed is the binary name. 891 /// 892 /// This is normally the case when using a "daemon" style mode, or an interactive CLI where 893 /// one would not normally type the binary or program name for each command. 894 /// 895 /// # Examples 896 /// 897 /// ```rust 898 /// # use clap::{App, arg, AppSettings}; 899 /// let m = App::new("myprog") 900 /// .setting(AppSettings::NoBinaryName) 901 /// .arg(arg!(<cmd> ... "commands to run")) 902 /// .get_matches_from(vec!["command", "set"]); 903 /// 904 /// let cmds: Vec<&str> = m.values_of("cmd").unwrap().collect(); 905 /// assert_eq!(cmds, ["command", "set"]); 906 /// ``` 907 /// [`try_get_matches_from_mut`]: crate::App::try_get_matches_from_mut() 908 NoBinaryName, 909 910 /// Treat the auto-generated `-h, --help` flags like any other flag, and *not* print the help 911 /// message. 912 /// 913 /// This allows one to handle printing of the help message manually. 914 /// 915 /// ```rust 916 /// # use clap::{App, AppSettings}; 917 /// let result = App::new("myprog") 918 /// .setting(AppSettings::NoAutoHelp) 919 /// .try_get_matches_from("myprog --help".split(" ")); 920 /// 921 /// // Normally, if `--help` is used clap prints the help message and returns an 922 /// // ErrorKind::DisplayHelp 923 /// // 924 /// // However, `--help` was treated like a normal flag 925 /// 926 /// assert!(result.is_ok()); 927 /// assert!(result.unwrap().is_present("help")); 928 /// ``` 929 NoAutoHelp, 930 931 /// Treat the auto-generated `-V, --version` flags like any other flag, and 932 /// *not* print the version message. 933 /// 934 /// This allows one to handle printing of the version message manually. 935 /// 936 /// ```rust 937 /// # use clap::{App, AppSettings}; 938 /// let result = App::new("myprog") 939 /// .version("3.0") 940 /// .setting(AppSettings::NoAutoVersion) 941 /// .try_get_matches_from("myprog --version".split(" ")); 942 /// 943 /// // Normally, if `--version` is used clap prints the version message and returns an 944 /// // ErrorKind::DisplayVersion 945 /// // 946 /// // However, `--version` was treated like a normal flag 947 /// 948 /// assert!(result.is_ok()); 949 /// assert!(result.unwrap().is_present("version")); 950 /// ``` 951 NoAutoVersion, 952 953 /// Deprecated, replaced with [`AppSettings::AllowHyphenValues`] 954 #[deprecated( 955 since = "3.0.0", 956 note = "Replaced with `AppSettings::AllowHyphenValues`" 957 )] 958 AllowLeadingHyphen, 959 960 /// Deprecated, this is now the default, see [`AppSettings::AllowInvalidUtf8ForExternalSubcommands`] and [`ArgSettings::AllowInvalidUtf8`][crate::ArgSettings::AllowInvalidUtf8] for the opposite. 961 #[deprecated( 962 since = "3.0.0", 963 note = "This is now the default see `AppSettings::AllowInvalidUtf8ForExternalSubcommands` and `ArgSettings::AllowInvalidUtf8` for the opposite." 964 )] 965 StrictUtf8, 966 967 /// Deprecated, this is now the default 968 #[deprecated(since = "3.0.0", note = "This is now the default")] 969 UnifiedHelpMessage, 970 971 /// Deprecated, this is now the default 972 #[deprecated(since = "3.0.0", note = "This is now the default")] 973 ColoredHelp, 974 975 /// Deprecated, see [`App::color`][crate::App::color] 976 #[deprecated(since = "3.0.0", note = "Replaced with `App::color`")] 977 ColorAuto, 978 979 /// Deprecated, replaced with [`App::color`][crate::App::color] 980 #[deprecated(since = "3.0.0", note = "Replaced with `App::color`")] 981 ColorAlways, 982 983 /// Deprecated, replaced with [`App::color`][crate::App::color] 984 #[deprecated(since = "3.0.0", note = "Replaced with `App::color`")] 985 ColorNever, 986 987 /// Deprecated, replaced with [`AppSettings::DisableHelpFlag`] 988 #[deprecated(since = "3.0.0", note = "Replaced with `AppSettings::DisableHelpFlag`")] 989 DisableHelpFlags, 990 991 /// Deprecated, replaced with [`AppSettings::DisableVersionFlag`] 992 #[deprecated( 993 since = "3.0.0", 994 note = "Replaced with `AppSettings::DisableVersionFlag`" 995 )] 996 DisableVersion, 997 998 /// Deprecated, replaced with [`AppSettings::PropagateVersion`] 999 #[deprecated( 1000 since = "3.0.0", 1001 note = "Replaced with `AppSettings::PropagateVersion`" 1002 )] 1003 GlobalVersion, 1004 1005 /// Deprecated, replaced with [`AppSettings::HidePossibleValues`] 1006 #[deprecated( 1007 since = "3.0.0", 1008 note = "Replaced with AppSettings::HidePossibleValues" 1009 )] 1010 HidePossibleValuesInHelp, 1011 1012 /// Deprecated, this is now the default 1013 #[deprecated(since = "3.0.0", note = "This is now the default")] 1014 UnifiedHelp, 1015 1016 /// If the app is already built, used for caching. 1017 #[doc(hidden)] 1018 Built, 1019 1020 /// If the app's bin name is already built, used for caching. 1021 #[doc(hidden)] 1022 BinNameBuilt, 1023 } 1024 1025 bitflags! { 1026 struct Flags: u64 { 1027 const SC_NEGATE_REQS = 1; 1028 const SC_REQUIRED = 1 << 1; 1029 const ARG_REQUIRED_ELSE_HELP = 1 << 2; 1030 const PROPAGATE_VERSION = 1 << 3; 1031 const DISABLE_VERSION_FOR_SC = 1 << 4; 1032 const WAIT_ON_ERROR = 1 << 6; 1033 const SC_REQUIRED_ELSE_HELP = 1 << 7; 1034 const NO_AUTO_HELP = 1 << 8; 1035 const NO_AUTO_VERSION = 1 << 9; 1036 const DISABLE_VERSION_FLAG = 1 << 10; 1037 const HIDDEN = 1 << 11; 1038 const TRAILING_VARARG = 1 << 12; 1039 const NO_BIN_NAME = 1 << 13; 1040 const ALLOW_UNK_SC = 1 << 14; 1041 const SC_UTF8_NONE = 1 << 15; 1042 const LEADING_HYPHEN = 1 << 16; 1043 const NO_POS_VALUES = 1 << 17; 1044 const NEXT_LINE_HELP = 1 << 18; 1045 const DERIVE_DISP_ORDER = 1 << 19; 1046 const DISABLE_COLORED_HELP = 1 << 20; 1047 const COLOR_ALWAYS = 1 << 21; 1048 const COLOR_AUTO = 1 << 22; 1049 const COLOR_NEVER = 1 << 23; 1050 const DONT_DELIM_TRAIL = 1 << 24; 1051 const ALLOW_NEG_NUMS = 1 << 25; 1052 const DISABLE_HELP_SC = 1 << 27; 1053 const DONT_COLLAPSE_ARGS = 1 << 28; 1054 const ARGS_NEGATE_SCS = 1 << 29; 1055 const PROPAGATE_VALS_DOWN = 1 << 30; 1056 const ALLOW_MISSING_POS = 1 << 31; 1057 const TRAILING_VALUES = 1 << 32; 1058 const BUILT = 1 << 33; 1059 const BIN_NAME_BUILT = 1 << 34; 1060 const VALID_ARG_FOUND = 1 << 35; 1061 const INFER_SUBCOMMANDS = 1 << 36; 1062 const CONTAINS_LAST = 1 << 37; 1063 const ARGS_OVERRIDE_SELF = 1 << 38; 1064 const HELP_REQUIRED = 1 << 39; 1065 const SUBCOMMAND_PRECEDENCE_OVER_ARG = 1 << 40; 1066 const DISABLE_HELP_FLAG = 1 << 41; 1067 const USE_LONG_FORMAT_FOR_HELP_SC = 1 << 42; 1068 const INFER_LONG_ARGS = 1 << 43; 1069 const IGNORE_ERRORS = 1 << 44; 1070 #[cfg(feature = "unstable-multicall")] 1071 const MULTICALL = 1 << 45; 1072 const NO_OP = 0; 1073 } 1074 } 1075 1076 impl_settings! { AppSettings, AppFlags, 1077 ArgRequiredElseHelp 1078 => Flags::ARG_REQUIRED_ELSE_HELP, 1079 SubcommandPrecedenceOverArg 1080 => Flags::SUBCOMMAND_PRECEDENCE_OVER_ARG, 1081 ArgsNegateSubcommands 1082 => Flags::ARGS_NEGATE_SCS, 1083 AllowExternalSubcommands 1084 => Flags::ALLOW_UNK_SC, 1085 StrictUtf8 1086 => Flags::NO_OP, 1087 AllowInvalidUtf8ForExternalSubcommands 1088 => Flags::SC_UTF8_NONE, 1089 AllowHyphenValues 1090 => Flags::LEADING_HYPHEN, 1091 AllowLeadingHyphen 1092 => Flags::LEADING_HYPHEN, 1093 AllowNegativeNumbers 1094 => Flags::ALLOW_NEG_NUMS, 1095 AllowMissingPositional 1096 => Flags::ALLOW_MISSING_POS, 1097 UnifiedHelpMessage 1098 => Flags::NO_OP, 1099 ColoredHelp 1100 => Flags::NO_OP, 1101 ColorAlways 1102 => Flags::COLOR_ALWAYS, 1103 ColorAuto 1104 => Flags::COLOR_AUTO, 1105 ColorNever 1106 => Flags::COLOR_NEVER, 1107 DontDelimitTrailingValues 1108 => Flags::DONT_DELIM_TRAIL, 1109 DontCollapseArgsInUsage 1110 => Flags::DONT_COLLAPSE_ARGS, 1111 DeriveDisplayOrder 1112 => Flags::DERIVE_DISP_ORDER, 1113 DisableColoredHelp 1114 => Flags::DISABLE_COLORED_HELP, 1115 DisableHelpSubcommand 1116 => Flags::DISABLE_HELP_SC, 1117 DisableHelpFlag 1118 => Flags::DISABLE_HELP_FLAG, 1119 DisableHelpFlags 1120 => Flags::DISABLE_HELP_FLAG, 1121 DisableVersionFlag 1122 => Flags::DISABLE_VERSION_FLAG, 1123 DisableVersion 1124 => Flags::DISABLE_VERSION_FLAG, 1125 PropagateVersion 1126 => Flags::PROPAGATE_VERSION, 1127 GlobalVersion 1128 => Flags::PROPAGATE_VERSION, 1129 HidePossibleValues 1130 => Flags::NO_POS_VALUES, 1131 HidePossibleValuesInHelp 1132 => Flags::NO_POS_VALUES, 1133 HelpExpected 1134 => Flags::HELP_REQUIRED, 1135 Hidden 1136 => Flags::HIDDEN, 1137 #[cfg(feature = "unstable-multicall")] 1138 Multicall 1139 => Flags::MULTICALL, 1140 NoAutoHelp 1141 => Flags::NO_AUTO_HELP, 1142 NoAutoVersion 1143 => Flags::NO_AUTO_VERSION, 1144 NoBinaryName 1145 => Flags::NO_BIN_NAME, 1146 SubcommandsNegateReqs 1147 => Flags::SC_NEGATE_REQS, 1148 SubcommandRequired 1149 => Flags::SC_REQUIRED, 1150 SubcommandRequiredElseHelp 1151 => Flags::SC_REQUIRED_ELSE_HELP, 1152 UseLongFormatForHelpSubcommand 1153 => Flags::USE_LONG_FORMAT_FOR_HELP_SC, 1154 TrailingVarArg 1155 => Flags::TRAILING_VARARG, 1156 UnifiedHelp => Flags::NO_OP, 1157 NextLineHelp 1158 => Flags::NEXT_LINE_HELP, 1159 IgnoreErrors 1160 => Flags::IGNORE_ERRORS, 1161 WaitOnError 1162 => Flags::WAIT_ON_ERROR, 1163 Built 1164 => Flags::BUILT, 1165 BinNameBuilt 1166 => Flags::BIN_NAME_BUILT, 1167 InferSubcommands 1168 => Flags::INFER_SUBCOMMANDS, 1169 AllArgsOverrideSelf 1170 => Flags::ARGS_OVERRIDE_SELF, 1171 InferLongArgs 1172 => Flags::INFER_LONG_ARGS 1173 } 1174 1175 /// Deprecated in [Issue #3087](https://github.com/clap-rs/clap/issues/3087), maybe [`clap::Parser`][crate::Parser] would fit your use case? 1176 #[cfg(feature = "yaml")] 1177 impl FromStr for AppSettings { 1178 type Err = String; from_str(s: &str) -> Result<Self, <Self as FromStr>::Err>1179 fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> { 1180 #[allow(deprecated)] 1181 #[allow(unreachable_patterns)] 1182 match &*s.to_ascii_lowercase() { 1183 "argrequiredelsehelp" => Ok(AppSettings::ArgRequiredElseHelp), 1184 "subcommandprecedenceoverarg" => Ok(AppSettings::SubcommandPrecedenceOverArg), 1185 "argsnegatesubcommands" => Ok(AppSettings::ArgsNegateSubcommands), 1186 "allowexternalsubcommands" => Ok(AppSettings::AllowExternalSubcommands), 1187 "strictutf8" => Ok(AppSettings::StrictUtf8), 1188 "allowinvalidutf8forexternalsubcommands" => { 1189 Ok(AppSettings::AllowInvalidUtf8ForExternalSubcommands) 1190 } 1191 "allowhyphenvalues" => Ok(AppSettings::AllowHyphenValues), 1192 "allowleadinghyphen" => Ok(AppSettings::AllowLeadingHyphen), 1193 "allownegativenumbers" => Ok(AppSettings::AllowNegativeNumbers), 1194 "allowmissingpositional" => Ok(AppSettings::AllowMissingPositional), 1195 "unifiedhelpmessage" => Ok(AppSettings::UnifiedHelpMessage), 1196 "coloredhelp" => Ok(AppSettings::ColoredHelp), 1197 "coloralways" => Ok(AppSettings::ColorAlways), 1198 "colorauto" => Ok(AppSettings::ColorAuto), 1199 "colornever" => Ok(AppSettings::ColorNever), 1200 "dontdelimittrailingvalues" => Ok(AppSettings::DontDelimitTrailingValues), 1201 "dontcollapseargsinusage" => Ok(AppSettings::DontCollapseArgsInUsage), 1202 "derivedisplayorder" => Ok(AppSettings::DeriveDisplayOrder), 1203 "disablecoloredhelp" => Ok(AppSettings::DisableColoredHelp), 1204 "disablehelpsubcommand" => Ok(AppSettings::DisableHelpSubcommand), 1205 "disablehelpflag" => Ok(AppSettings::DisableHelpFlag), 1206 "disablehelpflags" => Ok(AppSettings::DisableHelpFlags), 1207 "disableversionflag" => Ok(AppSettings::DisableVersionFlag), 1208 "disableversion" => Ok(AppSettings::DisableVersion), 1209 "propagateversion" => Ok(AppSettings::PropagateVersion), 1210 "propagateversion" => Ok(AppSettings::GlobalVersion), 1211 "hidepossiblevalues" => Ok(AppSettings::HidePossibleValues), 1212 "hidepossiblevaluesinhelp" => Ok(AppSettings::HidePossibleValuesInHelp), 1213 "helpexpected" => Ok(AppSettings::HelpExpected), 1214 "hidden" => Ok(AppSettings::Hidden), 1215 "noautohelp" => Ok(AppSettings::NoAutoHelp), 1216 "noautoversion" => Ok(AppSettings::NoAutoVersion), 1217 "nobinaryname" => Ok(AppSettings::NoBinaryName), 1218 "subcommandsnegatereqs" => Ok(AppSettings::SubcommandsNegateReqs), 1219 "subcommandrequired" => Ok(AppSettings::SubcommandRequired), 1220 "subcommandrequiredelsehelp" => Ok(AppSettings::SubcommandRequiredElseHelp), 1221 "uselongformatforhelpsubcommand" => Ok(AppSettings::UseLongFormatForHelpSubcommand), 1222 "trailingvararg" => Ok(AppSettings::TrailingVarArg), 1223 "unifiedhelp" => Ok(AppSettings::UnifiedHelp), 1224 "nextlinehelp" => Ok(AppSettings::NextLineHelp), 1225 "ignoreerrors" => Ok(AppSettings::IgnoreErrors), 1226 "waitonerror" => Ok(AppSettings::WaitOnError), 1227 "built" => Ok(AppSettings::Built), 1228 "binnamebuilt" => Ok(AppSettings::BinNameBuilt), 1229 "infersubcommands" => Ok(AppSettings::InferSubcommands), 1230 "allargsoverrideself" => Ok(AppSettings::AllArgsOverrideSelf), 1231 "inferlongargs" => Ok(AppSettings::InferLongArgs), 1232 _ => Err(format!("unknown AppSetting: `{}`", s)), 1233 } 1234 } 1235 } 1236 1237 #[cfg(test)] 1238 mod test { 1239 #[allow(clippy::cognitive_complexity)] 1240 #[test] 1241 #[cfg(feature = "yaml")] app_settings_fromstr()1242 fn app_settings_fromstr() { 1243 use super::AppSettings; 1244 1245 assert_eq!( 1246 "disablehelpflag".parse::<AppSettings>().unwrap(), 1247 AppSettings::DisableHelpFlag 1248 ); 1249 assert_eq!( 1250 "argsnegatesubcommands".parse::<AppSettings>().unwrap(), 1251 AppSettings::ArgsNegateSubcommands 1252 ); 1253 assert_eq!( 1254 "argrequiredelsehelp".parse::<AppSettings>().unwrap(), 1255 AppSettings::ArgRequiredElseHelp 1256 ); 1257 assert_eq!( 1258 "subcommandprecedenceoverarg" 1259 .parse::<AppSettings>() 1260 .unwrap(), 1261 AppSettings::SubcommandPrecedenceOverArg 1262 ); 1263 assert_eq!( 1264 "allowexternalsubcommands".parse::<AppSettings>().unwrap(), 1265 AppSettings::AllowExternalSubcommands 1266 ); 1267 assert_eq!( 1268 "allowinvalidutf8forexternalsubcommands" 1269 .parse::<AppSettings>() 1270 .unwrap(), 1271 AppSettings::AllowInvalidUtf8ForExternalSubcommands 1272 ); 1273 assert_eq!( 1274 "allowhyphenvalues".parse::<AppSettings>().unwrap(), 1275 AppSettings::AllowHyphenValues 1276 ); 1277 assert_eq!( 1278 "allownegativenumbers".parse::<AppSettings>().unwrap(), 1279 AppSettings::AllowNegativeNumbers 1280 ); 1281 assert_eq!( 1282 "disablehelpsubcommand".parse::<AppSettings>().unwrap(), 1283 AppSettings::DisableHelpSubcommand 1284 ); 1285 assert_eq!( 1286 "disableversionflag".parse::<AppSettings>().unwrap(), 1287 AppSettings::DisableVersionFlag 1288 ); 1289 assert_eq!( 1290 "dontcollapseargsinusage".parse::<AppSettings>().unwrap(), 1291 AppSettings::DontCollapseArgsInUsage 1292 ); 1293 assert_eq!( 1294 "dontdelimittrailingvalues".parse::<AppSettings>().unwrap(), 1295 AppSettings::DontDelimitTrailingValues 1296 ); 1297 assert_eq!( 1298 "derivedisplayorder".parse::<AppSettings>().unwrap(), 1299 AppSettings::DeriveDisplayOrder 1300 ); 1301 assert_eq!( 1302 "disablecoloredhelp".parse::<AppSettings>().unwrap(), 1303 AppSettings::DisableColoredHelp 1304 ); 1305 assert_eq!( 1306 "propagateversion".parse::<AppSettings>().unwrap(), 1307 AppSettings::PropagateVersion 1308 ); 1309 assert_eq!( 1310 "hidden".parse::<AppSettings>().unwrap(), 1311 AppSettings::Hidden 1312 ); 1313 assert_eq!( 1314 "hidepossiblevalues".parse::<AppSettings>().unwrap(), 1315 AppSettings::HidePossibleValues 1316 ); 1317 assert_eq!( 1318 "helpexpected".parse::<AppSettings>().unwrap(), 1319 AppSettings::HelpExpected 1320 ); 1321 assert_eq!( 1322 "nobinaryname".parse::<AppSettings>().unwrap(), 1323 AppSettings::NoBinaryName 1324 ); 1325 assert_eq!( 1326 "nextlinehelp".parse::<AppSettings>().unwrap(), 1327 AppSettings::NextLineHelp 1328 ); 1329 assert_eq!( 1330 "subcommandsnegatereqs".parse::<AppSettings>().unwrap(), 1331 AppSettings::SubcommandsNegateReqs 1332 ); 1333 assert_eq!( 1334 "subcommandrequired".parse::<AppSettings>().unwrap(), 1335 AppSettings::SubcommandRequired 1336 ); 1337 assert_eq!( 1338 "subcommandrequiredelsehelp".parse::<AppSettings>().unwrap(), 1339 AppSettings::SubcommandRequiredElseHelp 1340 ); 1341 assert_eq!( 1342 "uselongformatforhelpsubcommand" 1343 .parse::<AppSettings>() 1344 .unwrap(), 1345 AppSettings::UseLongFormatForHelpSubcommand 1346 ); 1347 assert_eq!( 1348 "trailingvararg".parse::<AppSettings>().unwrap(), 1349 AppSettings::TrailingVarArg 1350 ); 1351 assert_eq!( 1352 "waitonerror".parse::<AppSettings>().unwrap(), 1353 AppSettings::WaitOnError 1354 ); 1355 assert_eq!("built".parse::<AppSettings>().unwrap(), AppSettings::Built); 1356 assert_eq!( 1357 "binnamebuilt".parse::<AppSettings>().unwrap(), 1358 AppSettings::BinNameBuilt 1359 ); 1360 assert_eq!( 1361 "infersubcommands".parse::<AppSettings>().unwrap(), 1362 AppSettings::InferSubcommands 1363 ); 1364 assert!("hahahaha".parse::<AppSettings>().is_err()); 1365 } 1366 } 1367