mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
Merge #2154
2154: Fix the error logic and error message for suggesting `--` before a flag r=pksunkara a=ldm0 Co-authored-by: Donough Liu <ldm2993593805@163.com>
This commit is contained in:
commit
4ff4879400
5 changed files with 54 additions and 14 deletions
|
@ -798,26 +798,32 @@ impl Error {
|
||||||
c.warning(arg.clone());
|
c.warning(arg.clone());
|
||||||
c.none("' which wasn't expected, or isn't valid in this context");
|
c.none("' which wasn't expected, or isn't valid in this context");
|
||||||
|
|
||||||
if let Some(s) = did_you_mean {
|
if let Some((flag, subcmd)) = did_you_mean {
|
||||||
|
let flag = format!("--{}", flag);
|
||||||
c.none("\n\n\tDid you mean ");
|
c.none("\n\n\tDid you mean ");
|
||||||
|
|
||||||
if let Some(subcmd) = s.1 {
|
if let Some(subcmd) = subcmd {
|
||||||
c.none("to put '");
|
c.none("to put '");
|
||||||
c.good(format!("--{}", &s.0));
|
c.good(flag);
|
||||||
c.none("' after the subcommand '");
|
c.none("' after the subcommand '");
|
||||||
c.good(subcmd);
|
c.good(subcmd);
|
||||||
c.none("'?");
|
c.none("'?");
|
||||||
} else {
|
} else {
|
||||||
c.none("'");
|
c.none("'");
|
||||||
c.good(format!("--{}", &s.0));
|
c.good(flag);
|
||||||
c.none("'?");
|
c.none("'?");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.none(format!(
|
// If the user wants to supply things like `--a-flag` or `-b` as a value,
|
||||||
"\n\nIf you tried to supply `{}` as a PATTERN use `-- {}`",
|
// suggest `--` for disambiguation.
|
||||||
arg, arg
|
if arg.starts_with('-') {
|
||||||
));
|
c.none(format!(
|
||||||
|
"\n\nIf you tried to supply `{}` as a value rather than a flag, use `-- {}`",
|
||||||
|
arg, arg
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
put_usage(&mut c, usage);
|
put_usage(&mut c, usage);
|
||||||
try_help(&mut c);
|
try_help(&mut c);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,16 @@
|
||||||
|
mod utils;
|
||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
|
|
||||||
|
const USE_FLAG_AS_ARGUMENT: &str =
|
||||||
|
"error: Found argument '--another-flag' which wasn't expected, or isn't valid in this context
|
||||||
|
|
||||||
|
If you tried to supply `--another-flag` as a value rather than a flag, use `-- --another-flag`
|
||||||
|
|
||||||
|
USAGE:
|
||||||
|
mycat [FLAGS] [filename]
|
||||||
|
|
||||||
|
For more information try --help";
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn flag_using_short() {
|
fn flag_using_short() {
|
||||||
let m = App::new("flag")
|
let m = App::new("flag")
|
||||||
|
@ -110,3 +121,28 @@ fn multiple_flags_in_single() {
|
||||||
assert!(m.is_present("color"));
|
assert!(m.is_present("color"));
|
||||||
assert!(m.is_present("debug"));
|
assert!(m.is_present("debug"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn issue_1284_argument_in_flag_style() {
|
||||||
|
let app = App::new("mycat")
|
||||||
|
.arg(Arg::new("filename"))
|
||||||
|
.arg(Arg::new("a-flag").long("a-flag"));
|
||||||
|
|
||||||
|
let m = app
|
||||||
|
.clone()
|
||||||
|
.get_matches_from(vec!["", "--", "--another-flag"]);
|
||||||
|
assert_eq!(m.value_of("filename"), Some("--another-flag"));
|
||||||
|
|
||||||
|
let m = app.clone().get_matches_from(vec!["", "--a-flag"]);
|
||||||
|
assert!(m.is_present("a-flag"));
|
||||||
|
|
||||||
|
let m = app.clone().get_matches_from(vec!["", "--", "--a-flag"]);
|
||||||
|
assert_eq!(m.value_of("filename"), Some("--a-flag"));
|
||||||
|
|
||||||
|
assert!(utils::compare_output(
|
||||||
|
app,
|
||||||
|
"mycat --another-flag",
|
||||||
|
USE_FLAG_AS_ARGUMENT,
|
||||||
|
true
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ For more information try --help";
|
||||||
static REQ_GROUP_CONFLICT_ONLY_OPTIONS: &str =
|
static REQ_GROUP_CONFLICT_ONLY_OPTIONS: &str =
|
||||||
"error: Found argument '--all' which wasn't expected, or isn't valid in this context
|
"error: Found argument '--all' which wasn't expected, or isn't valid in this context
|
||||||
|
|
||||||
If you tried to supply `--all` as a PATTERN use `-- --all`
|
If you tried to supply `--all` as a value rather than a flag, use `-- --all`
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
clap-test <-a|--delete>
|
clap-test <-a|--delete>
|
||||||
|
@ -41,8 +41,6 @@ For more information try --help";
|
||||||
static REQ_GROUP_CONFLICT_REV_DEGRADED: &str =
|
static REQ_GROUP_CONFLICT_REV_DEGRADED: &str =
|
||||||
"error: Found argument 'base' which wasn't expected, or isn't valid in this context
|
"error: Found argument 'base' which wasn't expected, or isn't valid in this context
|
||||||
|
|
||||||
If you tried to supply `base` as a PATTERN use `-- base`
|
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
clap-test <base|--delete>
|
clap-test <base|--delete>
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ static DYM: &str =
|
||||||
|
|
||||||
\tDid you mean '--option'?
|
\tDid you mean '--option'?
|
||||||
|
|
||||||
If you tried to supply `--optio` as a PATTERN use `-- --optio`
|
If you tried to supply `--optio` as a value rather than a flag, use `-- --optio`
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
clap-test --option <opt>...
|
clap-test --option <opt>...
|
||||||
|
@ -21,7 +21,7 @@ static DYM_ISSUE_1073: &str =
|
||||||
|
|
||||||
\tDid you mean '--files-without-match'?
|
\tDid you mean '--files-without-match'?
|
||||||
|
|
||||||
If you tried to supply `--files-without-matches` as a PATTERN use `-- --files-without-matches`
|
If you tried to supply `--files-without-matches` as a value rather than a flag, use `-- --files-without-matches`
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
ripgrep-616 --files-without-match
|
ripgrep-616 --files-without-match
|
||||||
|
|
|
@ -86,7 +86,7 @@ static DYM_ARG: &str =
|
||||||
|
|
||||||
Did you mean to put '--subcmdarg' after the subcommand 'subcmd'?
|
Did you mean to put '--subcmdarg' after the subcommand 'subcmd'?
|
||||||
|
|
||||||
If you tried to supply `--subcm` as a PATTERN use `-- --subcm`
|
If you tried to supply `--subcm` as a value rather than a flag, use `-- --subcm`
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
dym [SUBCOMMAND]
|
dym [SUBCOMMAND]
|
||||||
|
|
Loading…
Reference in a new issue