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

uucore: don't show error for ambiguous value #5127

Merged
Merged
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
68 changes: 46 additions & 22 deletions src/uucore/src/lib/parser/shortcut_value_parser.rs
@@ -1,3 +1,4 @@
// spell-checker:ignore abcdefgh
use clap::{
builder::{PossibleValue, TypedValueParser},
error::{ContextKind, ContextValue, ErrorKind},
Expand All @@ -15,6 +16,34 @@ impl ShortcutValueParser {
pub fn new(values: impl Into<Self>) -> Self {
values.into()
}

fn generate_clap_error(
&self,
cmd: &clap::Command,
arg: Option<&clap::Arg>,
value: &str,
) -> clap::Error {
let mut err = clap::Error::new(ErrorKind::InvalidValue).with_cmd(cmd);

if let Some(arg) = arg {
err.insert(
ContextKind::InvalidArg,
ContextValue::String(arg.to_string()),
);
}

err.insert(
ContextKind::InvalidValue,
ContextValue::String(value.to_string()),
);

err.insert(
ContextKind::ValidValue,
ContextValue::Strings(self.0.iter().map(|x| x.get_name().to_string()).collect()),
);

err
}
}

impl TypedValueParser for ShortcutValueParser {
Expand All @@ -36,29 +65,16 @@ impl TypedValueParser for ShortcutValueParser {
.filter(|x| x.get_name().starts_with(value))
.collect();

if matched_values.len() == 1 {
Ok(matched_values[0].get_name().to_string())
} else {
let mut err = clap::Error::new(ErrorKind::InvalidValue).with_cmd(cmd);

if let Some(arg) = arg {
err.insert(
ContextKind::InvalidArg,
ContextValue::String(arg.to_string()),
);
match matched_values.len() {
0 => Err(self.generate_clap_error(cmd, arg, value)),
1 => Ok(matched_values[0].get_name().to_string()),
_ => {
if let Some(direct_match) = matched_values.iter().find(|x| x.get_name() == value) {
Ok(direct_match.get_name().to_string())
} else {
Err(self.generate_clap_error(cmd, arg, value))
}
}

err.insert(
ContextKind::InvalidValue,
ContextValue::String(value.to_string()),
);

err.insert(
ContextKind::ValidValue,
ContextValue::Strings(self.0.iter().map(|x| x.get_name().to_string()).collect()),
);

Err(err)
}
}

Expand Down Expand Up @@ -127,6 +143,14 @@ mod tests {
assert_eq!("abef", result.unwrap());
}

#[test]
fn test_parse_ref_with_ambiguous_value_that_is_a_possible_value() {
let cmd = Command::new("cmd");
let parser = ShortcutValueParser::new(["abcd", "abcdefgh"]);
let result = parser.parse_ref(&cmd, None, OsStr::new("abcd"));
assert_eq!("abcd", result.unwrap());
}

#[test]
#[cfg(unix)]
fn test_parse_ref_with_invalid_utf8() {
Expand Down