1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 04:27:45 +00:00

touch: update to clap 4

This commit is contained in:
Terts Diepraam 2022-10-01 11:11:18 +02:00
parent 86cbdbb19e
commit e54e2e0252
2 changed files with 27 additions and 27 deletions

View file

@ -16,7 +16,7 @@ path = "src/touch.rs"
[dependencies] [dependencies]
filetime = "0.2.17" filetime = "0.2.17"
clap = { version = "3.2", features = ["wrap_help", "cargo"] } clap = { version = "4.0", features = ["wrap_help", "cargo"] }
time = { version = "0.3", features = ["parsing", "formatting", "local-offset", "macros"] } time = { version = "0.3", features = ["parsing", "formatting", "local-offset", "macros"] }
uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["libc"] } uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["libc"] }

View file

@ -13,7 +13,7 @@ pub extern crate filetime;
extern crate uucore; extern crate uucore;
use clap::builder::ValueParser; use clap::builder::ValueParser;
use clap::{crate_version, Arg, ArgGroup, Command}; use clap::{crate_version, Arg, ArgAction, ArgGroup, Command};
use filetime::*; use filetime::*;
use std::ffi::OsString; use std::ffi::OsString;
use std::fs::{self, File}; use std::fs::{self, File};
@ -80,10 +80,7 @@ Try 'touch --help' for more information."##,
})?; })?;
let (mut atime, mut mtime) = let (mut atime, mut mtime) =
if let Some(reference) = matches.get_one::<OsString>(options::sources::REFERENCE) { if let Some(reference) = matches.get_one::<OsString>(options::sources::REFERENCE) {
stat( stat(Path::new(reference), !matches.get_flag(options::NO_DEREF))?
Path::new(reference),
!matches.contains_id(options::NO_DEREF),
)?
} else { } else {
let timestamp = if let Some(date) = matches.get_one::<String>(options::sources::DATE) { let timestamp = if let Some(date) = matches.get_one::<String>(options::sources::DATE) {
parse_date(date)? parse_date(date)?
@ -110,11 +107,11 @@ Try 'touch --help' for more information."##,
return Err(e.map_err_context(|| format!("setting times of {}", filename.quote()))); return Err(e.map_err_context(|| format!("setting times of {}", filename.quote())));
} }
if matches.contains_id(options::NO_CREATE) { if matches.get_flag(options::NO_CREATE) {
continue; continue;
} }
if matches.contains_id(options::NO_DEREF) { if matches.get_flag(options::NO_DEREF) {
show!(USimpleError::new( show!(USimpleError::new(
1, 1,
format!( format!(
@ -138,17 +135,17 @@ Try 'touch --help' for more information."##,
// If changing "only" atime or mtime, grab the existing value of the other. // If changing "only" atime or mtime, grab the existing value of the other.
// Note that "-a" and "-m" may be passed together; this is not an xor. // Note that "-a" and "-m" may be passed together; this is not an xor.
if matches.contains_id(options::ACCESS) if matches.get_flag(options::ACCESS)
|| matches.contains_id(options::MODIFICATION) || matches.get_flag(options::MODIFICATION)
|| matches.contains_id(options::TIME) || matches.contains_id(options::TIME)
{ {
let st = stat(path, !matches.contains_id(options::NO_DEREF))?; let st = stat(path, !matches.get_flag(options::NO_DEREF))?;
let time = matches let time = matches
.get_one::<String>(options::TIME) .get_one::<String>(options::TIME)
.map(|s| s.as_str()) .map(|s| s.as_str())
.unwrap_or(""); .unwrap_or("");
if !(matches.contains_id(options::ACCESS) if !(matches.get_flag(options::ACCESS)
|| time.contains(&"access".to_owned()) || time.contains(&"access".to_owned())
|| time.contains(&"atime".to_owned()) || time.contains(&"atime".to_owned())
|| time.contains(&"use".to_owned())) || time.contains(&"use".to_owned()))
@ -156,7 +153,7 @@ Try 'touch --help' for more information."##,
atime = st.0; atime = st.0;
} }
if !(matches.contains_id(options::MODIFICATION) if !(matches.get_flag(options::MODIFICATION)
|| time.contains(&"modify".to_owned()) || time.contains(&"modify".to_owned())
|| time.contains(&"mtime".to_owned())) || time.contains(&"mtime".to_owned()))
{ {
@ -164,7 +161,7 @@ Try 'touch --help' for more information."##,
} }
} }
if matches.contains_id(options::NO_DEREF) { if matches.get_flag(options::NO_DEREF) {
set_symlink_file_times(path, atime, mtime) set_symlink_file_times(path, atime, mtime)
} else { } else {
filetime::set_file_times(path, atime, mtime) filetime::set_file_times(path, atime, mtime)
@ -175,28 +172,30 @@ Try 'touch --help' for more information."##,
Ok(()) Ok(())
} }
pub fn uu_app<'a>() -> Command<'a> { pub fn uu_app() -> Command {
Command::new(uucore::util_name()) Command::new(uucore::util_name())
.version(crate_version!()) .version(crate_version!())
.about(ABOUT) .about(ABOUT)
.override_usage(format_usage(USAGE)) .override_usage(format_usage(USAGE))
.infer_long_args(true) .infer_long_args(true)
.disable_help_flag(true)
.arg( .arg(
Arg::new(options::HELP) Arg::new(options::HELP)
.long(options::HELP) .long(options::HELP)
.help("Print help information."), .help("Print help information.")
.action(ArgAction::Help),
) )
.arg( .arg(
Arg::new(options::ACCESS) Arg::new(options::ACCESS)
.short('a') .short('a')
.help("change only the access time"), .help("change only the access time")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::sources::CURRENT) Arg::new(options::sources::CURRENT)
.short('t') .short('t')
.help("use [[CC]YY]MMDDhhmm[.ss] instead of the current time") .help("use [[CC]YY]MMDDhhmm[.ss] instead of the current time")
.value_name("STAMP") .value_name("STAMP"),
.takes_value(true),
) )
.arg( .arg(
Arg::new(options::sources::DATE) Arg::new(options::sources::DATE)
@ -208,13 +207,15 @@ pub fn uu_app<'a>() -> Command<'a> {
.arg( .arg(
Arg::new(options::MODIFICATION) Arg::new(options::MODIFICATION)
.short('m') .short('m')
.help("change only the modification time"), .help("change only the modification time")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::NO_CREATE) Arg::new(options::NO_CREATE)
.short('c') .short('c')
.long(options::NO_CREATE) .long(options::NO_CREATE)
.help("do not create any files"), .help("do not create any files")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::NO_DEREF) Arg::new(options::NO_DEREF)
@ -223,7 +224,8 @@ pub fn uu_app<'a>() -> Command<'a> {
.help( .help(
"affect each symbolic link instead of any referenced file \ "affect each symbolic link instead of any referenced file \
(only for systems that can change the timestamps of a symlink)", (only for systems that can change the timestamps of a symlink)",
), )
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::sources::REFERENCE) Arg::new(options::sources::REFERENCE)
@ -243,14 +245,12 @@ pub fn uu_app<'a>() -> Command<'a> {
equivalent to -m", equivalent to -m",
) )
.value_name("WORD") .value_name("WORD")
.value_parser(["access", "atime", "use"]) .value_parser(["access", "atime", "use"]),
.takes_value(true),
) )
.arg( .arg(
Arg::new(ARG_FILES) Arg::new(ARG_FILES)
.multiple_occurrences(true) .action(ArgAction::Append)
.takes_value(true) .num_args(1..)
.min_values(1)
.value_parser(ValueParser::os_string()) .value_parser(ValueParser::os_string())
.value_hint(clap::ValueHint::AnyPath), .value_hint(clap::ValueHint::AnyPath),
) )