clap/tests/double_require.rs
Ed Page 61c9e6265b fix(help)!: Merge OPTIONS / FLAGS default groups
For those that want the original behavior, you can usxe
`arg.help_heading(Some("FLAGS"))` on your flags.  Limitations:
- This will not give you a special sort order
- This will not get a `[FLAGS]` added to usage

For templates, we removed `{unified}` and `{flags}`.  To help people
catch these, a debug_assert was added.

I'm unsure but I think there might be a change in behavior in calcuating
when to show `[OPTION]` in usage.  The old code only looked at
`required` while flags looked only at arg groups.  We now look at both.

Ideally we'd add these in `_build` and remove special casing for
no-groups except in the sort order of groups.  I feel like thats best
left for later.

This also reduced the scope of `App`s public API.
`get_*_with_no_heading` seemed a bit specialized to be in the public
API.  #2853 looks at splitting it out into its own PR.

BREAKING CHANGE: Multiple
- `UnifiedHelpMessage` removed
- `{flags}` and `{unified}` are removed and will assert when present.
- `get_*_with_no_heading` removed

Fixes #2807
2021-10-13 11:42:10 -05:00

88 lines
2.1 KiB
Rust

use clap::{App, Arg, ErrorKind};
static HELP: &str = "prog
USAGE:
prog [OPTIONS]
OPTIONS:
-a
-b
-c
-h, --help Print help information
";
static ONLY_B_ERROR: &str = "error: The following required arguments were not provided:
-c
USAGE:
prog [OPTIONS] -c -b
For more information try --help
";
static ONLY_C_ERROR: &str = "error: The following required arguments were not provided:
-b
USAGE:
prog [OPTIONS] -b -c
For more information try --help
";
fn app() -> App<'static> {
App::new("prog")
.arg(
Arg::new("a")
.short('a')
.required_unless_present_any(&["b", "c"])
.conflicts_with_all(&["b", "c"]),
)
.arg(
Arg::new("b")
.short('b')
.required_unless_present("a")
.requires("c"),
)
.arg(
Arg::new("c")
.short('c')
.required_unless_present("a")
.requires("b"),
)
}
#[test]
fn valid_cases() {
let res = app().try_get_matches_from(vec!["", "-a"]);
assert!(res.is_ok());
let res = app().clone().try_get_matches_from(vec!["", "-b", "-c"]);
assert!(res.is_ok());
let res = app().try_get_matches_from(vec!["", "-c", "-b"]);
assert!(res.is_ok());
}
#[test]
fn help_text() {
let res = app().try_get_matches_from(vec!["prog", "--help"]);
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.kind, ErrorKind::DisplayHelp);
println!("{}", err.to_string());
assert_eq!(err.to_string(), HELP);
}
#[test]
fn no_duplicate_error() {
let res = app().try_get_matches_from(vec!["", "-b"]);
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
assert_eq!(err.to_string(), ONLY_B_ERROR);
let res = app().try_get_matches_from(vec!["", "-c"]);
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
assert_eq!(err.to_string(), ONLY_C_ERROR);
}