mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
uucore: properly handle aliases in ShortcutValueParser
This commit is contained in:
parent
333e4d9fe9
commit
4ec82948b6
1 changed files with 28 additions and 2 deletions
|
@ -66,7 +66,7 @@ impl TypedValueParser for ShortcutValueParser {
|
||||||
let matched_values: Vec<_> = self
|
let matched_values: Vec<_> = self
|
||||||
.0
|
.0
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|x| x.get_name().starts_with(value))
|
.filter(|x| x.get_name_and_aliases().any(|name| name.starts_with(value)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
match matched_values.len() {
|
match matched_values.len() {
|
||||||
|
@ -101,7 +101,7 @@ where
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
|
|
||||||
use clap::{builder::TypedValueParser, error::ErrorKind, Command};
|
use clap::{builder::PossibleValue, builder::TypedValueParser, error::ErrorKind, Command};
|
||||||
|
|
||||||
use super::ShortcutValueParser;
|
use super::ShortcutValueParser;
|
||||||
|
|
||||||
|
@ -166,4 +166,30 @@ mod tests {
|
||||||
let result = parser.parse_ref(&cmd, None, OsStr::from_bytes(&[0xc3, 0x28]));
|
let result = parser.parse_ref(&cmd, None, OsStr::from_bytes(&[0xc3, 0x28]));
|
||||||
assert_eq!(ErrorKind::InvalidUtf8, result.unwrap_err().kind());
|
assert_eq!(ErrorKind::InvalidUtf8, result.unwrap_err().kind());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ambiguous_word_same_meaning() {
|
||||||
|
let cmd = Command::new("cmd");
|
||||||
|
let parser = ShortcutValueParser::new([
|
||||||
|
PossibleValue::new("atime").alias("access"),
|
||||||
|
"status".into(),
|
||||||
|
]);
|
||||||
|
// Even though "a" is ambiguous (it might mean "atime" or "access"),
|
||||||
|
// the meaning is uniquely defined, therefore accept it.
|
||||||
|
let atime_values = [
|
||||||
|
// spell-checker:disable-next-line
|
||||||
|
"atime", "atim", "at", "a", "access", "acces", "acce", "acc", "ac",
|
||||||
|
];
|
||||||
|
// spell-checker:disable-next-line
|
||||||
|
let status_values = ["status", "statu", "stat", "sta", "st", "st"];
|
||||||
|
|
||||||
|
for value in atime_values {
|
||||||
|
let result = parser.parse_ref(&cmd, None, OsStr::new(value));
|
||||||
|
assert_eq!("atime", result.unwrap());
|
||||||
|
}
|
||||||
|
for value in status_values {
|
||||||
|
let result = parser.parse_ref(&cmd, None, OsStr::new(value));
|
||||||
|
assert_eq!("status", result.unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue