Skip to content

Commit

Permalink
Merge pull request clap-rs#3391 from epage/possible
Browse files Browse the repository at this point in the history
fix(error): Consistently respect possible values order
  • Loading branch information
epage committed Feb 2, 2022
2 parents 7c79e76 + 6827491 commit d4cfcee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
7 changes: 3 additions & 4 deletions src/parse/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ impl Error {
let suffix = suggestions::did_you_mean(&bad_val, good_vals.iter()).pop();
let arg = arg.to_string();

let mut sorted: Vec<String> = good_vals
let good_vals: Vec<String> = good_vals
.iter()
.map(|&v| {
if v.contains(char::is_whitespace) {
Expand All @@ -652,15 +652,14 @@ impl Error {
}
})
.collect();
sorted.sort();

start_error(&mut c, "");
c.warning(format!("{:?}", bad_val));
c.none(" isn't a valid value for '");
c.warning(&*arg);
c.none("'\n\t[possible values: ");

if let Some((last, elements)) = sorted.split_last() {
if let Some((last, elements)) = good_vals.split_last() {
for v in elements {
c.good(v);
c.none(", ");
Expand All @@ -681,7 +680,7 @@ impl Error {
try_help(app, &mut c);

let mut info = vec![arg, bad_val];
info.extend(sorted);
info.extend(good_vals);

Self::for_app(app, c, ErrorKind::InvalidValue, info)
}
Expand Down
8 changes: 4 additions & 4 deletions tests/builder/possible_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::{App, Arg, ErrorKind, PossibleValue};

#[cfg(feature = "suggestions")]
static PV_ERROR: &str = "error: \"slo\" isn't a valid value for '-O <option>'
\t[possible values: \"ludicrous speed\", fast, slow]
\t[possible values: slow, fast, \"ludicrous speed\"]
\tDid you mean \"slow\"?
Expand All @@ -16,7 +16,7 @@ For more information try --help

#[cfg(not(feature = "suggestions"))]
static PV_ERROR: &str = "error: \"slo\" isn't a valid value for '-O <option>'
\t[possible values: \"ludicrous speed\", fast, slow]
\t[possible values: slow, fast, \"ludicrous speed\"]
USAGE:
clap-test -O <option>
Expand All @@ -26,7 +26,7 @@ For more information try --help

#[cfg(feature = "suggestions")]
static PV_ERROR_ESCAPED: &str = "error: \"ludicrous\" isn't a valid value for '-O <option>'
\t[possible values: \"ludicrous speed\", fast, slow]
\t[possible values: slow, fast, \"ludicrous speed\"]
\tDid you mean \"ludicrous speed\"?
Expand All @@ -38,7 +38,7 @@ For more information try --help

#[cfg(not(feature = "suggestions"))]
static PV_ERROR_ESCAPED: &str = "error: \"ludicrous\" isn't a valid value for '-O <option>'
\t[possible values: \"ludicrous speed\", fast, slow]
\t[possible values: slow, fast, \"ludicrous speed\"]
USAGE:
clap-test -O <option>
Expand Down

0 comments on commit d4cfcee

Please sign in to comment.