Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(error): Show possible values when none are supplied #3394

Merged
merged 1 commit into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/parse/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,13 +605,37 @@ impl Error {
Self::for_app(app, c, ErrorKind::ArgumentConflict, others)
}

pub(crate) fn empty_value(app: &App, arg: &Arg, usage: String) -> Self {
pub(crate) fn empty_value(app: &App, good_vals: &[&str], arg: &Arg, usage: String) -> Self {
let mut c = Colorizer::new(true, app.get_color());
let arg = arg.to_string();

start_error(&mut c, "The argument '");
c.warning(&*arg);
c.none("' requires a value but none was supplied");
if !good_vals.is_empty() {
let good_vals: Vec<String> = good_vals
.iter()
.map(|&v| {
if v.contains(char::is_whitespace) {
format!("{:?}", v)
} else {
v.to_owned()
}
})
.collect();
c.none("\n\t[possible values: ");

if let Some((last, elements)) = good_vals.split_last() {
for v in elements {
c.good(v);
c.none(", ");
}

c.good(last);
}

c.none("]");
}
put_usage(&mut c, usage);
try_help(app, &mut c);

Expand Down
12 changes: 12 additions & 0 deletions src/parse/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
if should_err {
return Err(Error::empty_value(
self.p.app,
&o.possible_vals
.iter()
.filter_map(PossibleValue::get_visible_name)
.collect::<Vec<_>>(),
o,
Usage::new(self.p.app, &self.p.required).create_usage_with_title(&[]),
));
Expand Down Expand Up @@ -133,6 +137,10 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
debug!("Validator::validate_arg_values: illegal empty val found");
return Err(Error::empty_value(
self.p.app,
&arg.possible_vals
.iter()
.filter_map(PossibleValue::get_visible_name)
.collect::<Vec<_>>(),
arg,
Usage::new(self.p.app, &self.p.required).create_usage_with_title(&[]),
));
Expand Down Expand Up @@ -407,6 +415,10 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
if a.is_set(ArgSettings::TakesValue) && !min_vals_zero && ma.all_val_groups_empty() {
return Err(Error::empty_value(
self.p.app,
&a.possible_vals
.iter()
.filter_map(PossibleValue::get_visible_name)
.collect::<Vec<_>>(),
a,
Usage::new(self.p.app, &self.p.required).create_usage_with_title(&[]),
));
Expand Down
27 changes: 27 additions & 0 deletions tests/builder/possible_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,33 @@ fn escaped_possible_values_output() {
));
}

#[test]
fn missing_possible_value_error() {
assert!(utils::compare_output(
App::new("test").arg(
Arg::new("option")
.short('O')
.possible_value("slow")
.possible_value(PossibleValue::new("fast").alias("fost"))
.possible_value(PossibleValue::new("ludicrous speed"))
.possible_value(PossibleValue::new("forbidden speed").hide(true))
),
"clap-test -O",
MISSING_PV_ERROR,
true
));
}

static MISSING_PV_ERROR: &str =
"error: The argument '-O <option>' requires a value but none was supplied
\t[possible values: slow, fast, \"ludicrous speed\"]

USAGE:
clap-test [OPTIONS]

For more information try --help
";

#[test]
fn alias() {
let m = App::new("pv")
Expand Down