1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

truncate: update to clap 4

This commit is contained in:
Terts Diepraam 2022-10-01 11:43:24 +02:00
parent ea8015589b
commit 9cce0bed7f
3 changed files with 40 additions and 38 deletions

View file

@ -15,7 +15,7 @@ edition = "2021"
path = "src/truncate.rs" path = "src/truncate.rs"
[dependencies] [dependencies]
clap = { version = "3.2", features = ["wrap_help", "cargo"] } clap = { version = "4.0", features = ["wrap_help", "cargo"] }
uucore = { version=">=0.0.16", package="uucore", path="../../uucore" } uucore = { version=">=0.0.16", package="uucore", path="../../uucore" }
[[bin]] [[bin]]

View file

@ -6,7 +6,7 @@
// * file that was distributed with this source code. // * file that was distributed with this source code.
// spell-checker:ignore (ToDO) RFILE refsize rfilename fsize tsize // spell-checker:ignore (ToDO) RFILE refsize rfilename fsize tsize
use clap::{crate_version, Arg, Command}; use clap::{crate_version, Arg, ArgAction, Command};
use std::fs::{metadata, OpenOptions}; use std::fs::{metadata, OpenOptions};
use std::io::ErrorKind; use std::io::ErrorKind;
#[cfg(unix)] #[cfg(unix)]
@ -108,15 +108,13 @@ fn get_long_usage() -> String {
#[uucore::main] #[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> { pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let long_usage = get_long_usage();
let matches = uu_app() let matches = uu_app()
.after_help(&long_usage[..]) .after_help(get_long_usage())
.try_get_matches_from(args) .try_get_matches_from(args)
.map_err(|e| { .map_err(|e| {
e.print().expect("Error writing clap::Error"); e.print().expect("Error writing clap::Error");
match e.kind() { match e.kind() {
clap::ErrorKind::DisplayHelp | clap::ErrorKind::DisplayVersion => 0, clap::error::ErrorKind::DisplayHelp | clap::error::ErrorKind::DisplayVersion => 0,
_ => 1, _ => 1,
} }
})?; })?;
@ -129,8 +127,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if files.is_empty() { if files.is_empty() {
return Err(UUsageError::new(1, "missing file operand")); return Err(UUsageError::new(1, "missing file operand"));
} else { } else {
let io_blocks = matches.contains_id(options::IO_BLOCKS); let io_blocks = matches.get_flag(options::IO_BLOCKS);
let no_create = matches.contains_id(options::NO_CREATE); let no_create = matches.get_flag(options::NO_CREATE);
let reference = matches let reference = matches
.get_one::<String>(options::REFERENCE) .get_one::<String>(options::REFERENCE)
.map(String::from); .map(String::from);
@ -139,7 +137,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} }
} }
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)
@ -147,40 +145,48 @@ pub fn uu_app<'a>() -> Command<'a> {
.infer_long_args(true) .infer_long_args(true)
.arg( .arg(
Arg::new(options::IO_BLOCKS) Arg::new(options::IO_BLOCKS)
.short('o') .short('o')
.long(options::IO_BLOCKS) .long(options::IO_BLOCKS)
.help("treat SIZE as the number of I/O blocks of the file rather than bytes (NOT IMPLEMENTED)") .help(
"treat SIZE as the number of I/O blocks of the file rather than bytes \
(NOT IMPLEMENTED)",
)
.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 files that do not exist") .help("do not create files that do not exist")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::REFERENCE) Arg::new(options::REFERENCE)
.short('r') .short('r')
.long(options::REFERENCE) .long(options::REFERENCE)
.required_unless_present(options::SIZE) .required_unless_present(options::SIZE)
.help("base the size of each file on the size of RFILE") .help("base the size of each file on the size of RFILE")
.value_name("RFILE") .value_name("RFILE")
.value_hint(clap::ValueHint::FilePath) .value_hint(clap::ValueHint::FilePath),
) )
.arg( .arg(
Arg::new(options::SIZE) Arg::new(options::SIZE)
.short('s') .short('s')
.long(options::SIZE) .long(options::SIZE)
.required_unless_present(options::REFERENCE) .required_unless_present(options::REFERENCE)
.help("set or adjust the size of each file according to SIZE, which is in bytes unless --io-blocks is specified") .help(
.value_name("SIZE") "set or adjust the size of each file according to SIZE, which is in \
bytes unless --io-blocks is specified",
)
.value_name("SIZE"),
)
.arg(
Arg::new(options::ARG_FILES)
.value_name("FILE")
.action(ArgAction::Append)
.required(true)
.value_hint(clap::ValueHint::FilePath),
) )
.arg(Arg::new(options::ARG_FILES)
.value_name("FILE")
.multiple_occurrences(true)
.takes_value(true)
.required(true)
.min_values(1)
.value_hint(clap::ValueHint::FilePath))
} }
/// Truncate the named file to the specified size. /// Truncate the named file to the specified size.

View file

@ -254,11 +254,7 @@ fn test_error_filename_only() {
.args(&["file"]) .args(&["file"])
.fails() .fails()
.code_is(1) .code_is(1)
.stderr_contains( .stderr_contains("error: The following required arguments were not provided:");
"error: The following required arguments were not provided:
--reference <RFILE>
--size <SIZE>",
);
} }
#[test] #[test]