touch: accept shortcuts for stringly-enum arguments

This commit is contained in:
Ben Wiederhake 2024-04-01 08:06:18 +02:00
parent 25245bde65
commit 3285f95eb3
2 changed files with 15 additions and 4 deletions

View file

@ -10,7 +10,7 @@ use chrono::{
DateTime, Datelike, Duration, Local, LocalResult, NaiveDate, NaiveDateTime, NaiveTime,
TimeZone, Timelike,
};
use clap::builder::ValueParser;
use clap::builder::{PossibleValue, ValueParser};
use clap::{crate_version, Arg, ArgAction, ArgGroup, ArgMatches, Command};
use filetime::{set_file_times, set_symlink_file_times, FileTime};
use std::ffi::OsString;
@ -18,6 +18,7 @@ use std::fs::{self, File};
use std::path::{Path, PathBuf};
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError};
use uucore::shortcut_value_parser::ShortcutValueParser;
use uucore::{format_usage, help_about, help_usage, show};
const ABOUT: &str = help_about!("touch.md");
@ -216,7 +217,10 @@ pub fn uu_app() -> Command {
equivalent to -m",
)
.value_name("WORD")
.value_parser(["access", "atime", "use", "modify", "mtime"]),
.value_parser(ShortcutValueParser::new([
PossibleValue::new("atime").alias("access").alias("use"),
PossibleValue::new("mtime").alias("modify"),
])),
)
.arg(
Arg::new(ARG_FILES)

View file

@ -192,7 +192,14 @@ fn test_touch_set_cymdhms_time() {
#[test]
fn test_touch_set_only_atime() {
let atime_args = ["-a", "--time=access", "--time=atime", "--time=use"];
let atime_args = [
"-a",
"--time=access",
"--time=atime",
"--time=atim", // spell-checker:disable-line
"--time=a",
"--time=use",
];
let file = "test_touch_set_only_atime";
for atime_arg in atime_args {
@ -293,7 +300,7 @@ fn test_touch_set_both_time_and_date() {
#[test]
fn test_touch_set_only_mtime() {
let mtime_args = ["-m", "--time=modify", "--time=mtime"];
let mtime_args = ["-m", "--time=modify", "--time=mtime", "--time=m"];
let file = "test_touch_set_only_mtime";
for mtime_arg in mtime_args {