mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
truncate: update to clap 4
This commit is contained in:
parent
ea8015589b
commit
9cce0bed7f
3 changed files with 40 additions and 38 deletions
|
@ -15,7 +15,7 @@ edition = "2021"
|
|||
path = "src/truncate.rs"
|
||||
|
||||
[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" }
|
||||
|
||||
[[bin]]
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
// * file that was distributed with this source code.
|
||||
|
||||
// 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::io::ErrorKind;
|
||||
#[cfg(unix)]
|
||||
|
@ -108,15 +108,13 @@ fn get_long_usage() -> String {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let long_usage = get_long_usage();
|
||||
|
||||
let matches = uu_app()
|
||||
.after_help(&long_usage[..])
|
||||
.after_help(get_long_usage())
|
||||
.try_get_matches_from(args)
|
||||
.map_err(|e| {
|
||||
e.print().expect("Error writing clap::Error");
|
||||
match e.kind() {
|
||||
clap::ErrorKind::DisplayHelp | clap::ErrorKind::DisplayVersion => 0,
|
||||
clap::error::ErrorKind::DisplayHelp | clap::error::ErrorKind::DisplayVersion => 0,
|
||||
_ => 1,
|
||||
}
|
||||
})?;
|
||||
|
@ -129,8 +127,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
if files.is_empty() {
|
||||
return Err(UUsageError::new(1, "missing file operand"));
|
||||
} else {
|
||||
let io_blocks = matches.contains_id(options::IO_BLOCKS);
|
||||
let no_create = matches.contains_id(options::NO_CREATE);
|
||||
let io_blocks = matches.get_flag(options::IO_BLOCKS);
|
||||
let no_create = matches.get_flag(options::NO_CREATE);
|
||||
let reference = matches
|
||||
.get_one::<String>(options::REFERENCE)
|
||||
.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())
|
||||
.version(crate_version!())
|
||||
.about(ABOUT)
|
||||
|
@ -147,40 +145,48 @@ pub fn uu_app<'a>() -> Command<'a> {
|
|||
.infer_long_args(true)
|
||||
.arg(
|
||||
Arg::new(options::IO_BLOCKS)
|
||||
.short('o')
|
||||
.long(options::IO_BLOCKS)
|
||||
.help("treat SIZE as the number of I/O blocks of the file rather than bytes (NOT IMPLEMENTED)")
|
||||
.short('o')
|
||||
.long(options::IO_BLOCKS)
|
||||
.help(
|
||||
"treat SIZE as the number of I/O blocks of the file rather than bytes \
|
||||
(NOT IMPLEMENTED)",
|
||||
)
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new(options::NO_CREATE)
|
||||
.short('c')
|
||||
.long(options::NO_CREATE)
|
||||
.help("do not create files that do not exist")
|
||||
.short('c')
|
||||
.long(options::NO_CREATE)
|
||||
.help("do not create files that do not exist")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new(options::REFERENCE)
|
||||
.short('r')
|
||||
.long(options::REFERENCE)
|
||||
.required_unless_present(options::SIZE)
|
||||
.help("base the size of each file on the size of RFILE")
|
||||
.value_name("RFILE")
|
||||
.value_hint(clap::ValueHint::FilePath)
|
||||
.short('r')
|
||||
.long(options::REFERENCE)
|
||||
.required_unless_present(options::SIZE)
|
||||
.help("base the size of each file on the size of RFILE")
|
||||
.value_name("RFILE")
|
||||
.value_hint(clap::ValueHint::FilePath),
|
||||
)
|
||||
.arg(
|
||||
Arg::new(options::SIZE)
|
||||
.short('s')
|
||||
.long(options::SIZE)
|
||||
.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")
|
||||
.value_name("SIZE")
|
||||
.short('s')
|
||||
.long(options::SIZE)
|
||||
.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",
|
||||
)
|
||||
.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.
|
||||
|
|
|
@ -254,11 +254,7 @@ fn test_error_filename_only() {
|
|||
.args(&["file"])
|
||||
.fails()
|
||||
.code_is(1)
|
||||
.stderr_contains(
|
||||
"error: The following required arguments were not provided:
|
||||
--reference <RFILE>
|
||||
--size <SIZE>",
|
||||
);
|
||||
.stderr_contains("error: The following required arguments were not provided:");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue