1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

du: update to clap 4

This commit is contained in:
Terts Diepraam 2022-09-29 17:07:58 +02:00
parent 98673ad76f
commit 8f3c43b238
2 changed files with 46 additions and 27 deletions

View file

@ -18,7 +18,7 @@ path = "src/du.rs"
chrono = { version="^0.4.19", default-features=false, features=["std", "alloc", "clock"]} chrono = { version="^0.4.19", default-features=false, features=["std", "alloc", "clock"]}
# For the --exclude & --exclude-from options # For the --exclude & --exclude-from options
glob = "0.3.0" glob = "0.3.0"
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" }
[target.'cfg(target_os = "windows")'.dependencies] [target.'cfg(target_os = "windows")'.dependencies]

View file

@ -10,6 +10,7 @@ extern crate uucore;
use chrono::prelude::DateTime; use chrono::prelude::DateTime;
use chrono::Local; use chrono::Local;
use clap::ArgAction;
use clap::{crate_version, Arg, ArgMatches, Command}; use clap::{crate_version, Arg, ArgMatches, Command};
use glob::Pattern; use glob::Pattern;
use std::collections::HashSet; use std::collections::HashSet;
@ -282,9 +283,9 @@ fn read_block_size(s: Option<&str>) -> u64 {
} }
fn choose_size(matches: &ArgMatches, stat: &Stat) -> u64 { fn choose_size(matches: &ArgMatches, stat: &Stat) -> u64 {
if matches.contains_id(options::INODES) { if matches.get_flag(options::INODES) {
stat.inodes stat.inodes
} else if matches.contains_id(options::APPARENT_SIZE) || matches.contains_id(options::BYTES) { } else if matches.get_flag(options::APPARENT_SIZE) || matches.get_flag(options::BYTES) {
stat.size stat.size
} else { } else {
// The st_blocks field indicates the number of blocks allocated to the file, 512-byte units. // The st_blocks field indicates the number of blocks allocated to the file, 512-byte units.
@ -501,7 +502,7 @@ fn build_exclude_patterns(matches: &ArgMatches) -> UResult<Vec<Pattern>> {
let mut exclude_patterns = Vec::new(); let mut exclude_patterns = Vec::new();
for f in excludes_iterator.chain(exclude_from_iterator) { for f in excludes_iterator.chain(exclude_from_iterator) {
if matches.contains_id(options::VERBOSE) { if matches.get_flag(options::VERBOSE) {
println!("adding {:?} to the exclude list ", &f); println!("adding {:?} to the exclude list ", &f);
} }
match parse_glob::from_str(&f) { match parse_glob::from_str(&f) {
@ -519,7 +520,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?; let matches = uu_app().try_get_matches_from(args)?;
let summarize = matches.contains_id(options::SUMMARIZE); let summarize = matches.get_flag(options::SUMMARIZE);
let max_depth = parse_depth( let max_depth = parse_depth(
matches matches
@ -529,14 +530,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
)?; )?;
let options = Options { let options = Options {
all: matches.contains_id(options::ALL), all: matches.get_flag(options::ALL),
max_depth, max_depth,
total: matches.contains_id(options::TOTAL), total: matches.get_flag(options::TOTAL),
separate_dirs: matches.contains_id(options::SEPARATE_DIRS), separate_dirs: matches.get_flag(options::SEPARATE_DIRS),
one_file_system: matches.contains_id(options::ONE_FILE_SYSTEM), one_file_system: matches.get_flag(options::ONE_FILE_SYSTEM),
dereference: matches.contains_id(options::DEREFERENCE), dereference: matches.get_flag(options::DEREFERENCE),
inodes: matches.contains_id(options::INODES), inodes: matches.get_flag(options::INODES),
verbose: matches.contains_id(options::VERBOSE), verbose: matches.get_flag(options::VERBOSE),
}; };
let files = match matches.get_one::<String>(options::FILE) { let files = match matches.get_one::<String>(options::FILE) {
@ -549,7 +550,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}; };
if options.inodes if options.inodes
&& (matches.contains_id(options::APPARENT_SIZE) || matches.contains_id(options::BYTES)) && (matches.get_flag(options::APPARENT_SIZE) || matches.get_flag(options::BYTES))
{ {
show_warning!("options --apparent-size and -b are ineffective with --inodes"); show_warning!("options --apparent-size and -b are ineffective with --inodes");
} }
@ -565,19 +566,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.unwrap_or_else(|e| crash!(1, "{}", format_error_message(&e, s, options::THRESHOLD))) .unwrap_or_else(|e| crash!(1, "{}", format_error_message(&e, s, options::THRESHOLD)))
}); });
let multiplier: u64 = if matches.contains_id(options::SI) { let multiplier: u64 = if matches.get_flag(options::SI) {
1000 1000
} else { } else {
1024 1024
}; };
let convert_size_fn = { let convert_size_fn = {
if matches.contains_id(options::HUMAN_READABLE) || matches.contains_id(options::SI) { if matches.get_flag(options::HUMAN_READABLE) || matches.get_flag(options::SI) {
convert_size_human convert_size_human
} else if matches.contains_id(options::BYTES) { } else if matches.get_flag(options::BYTES) {
convert_size_b convert_size_b
} else if matches.contains_id(options::BLOCK_SIZE_1K) { } else if matches.get_flag(options::BLOCK_SIZE_1K) {
convert_size_k convert_size_k
} else if matches.contains_id(options::BLOCK_SIZE_1M) { } else if matches.get_flag(options::BLOCK_SIZE_1M) {
convert_size_m convert_size_m
} else { } else {
convert_size_other convert_size_other
@ -594,7 +595,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let time_format_str = let time_format_str =
parse_time_style(matches.get_one::<String>("time-style").map(|s| s.as_str()))?; parse_time_style(matches.get_one::<String>("time-style").map(|s| s.as_str()))?;
let line_separator = if matches.contains_id(options::NULL) { let line_separator = if matches.get_flag(options::NULL) {
"\0" "\0"
} else { } else {
"\n" "\n"
@ -710,23 +711,26 @@ fn parse_depth(max_depth_str: Option<&str>, summarize: bool) -> UResult<Option<u
} }
} }
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)
.after_help(LONG_HELP) .after_help(LONG_HELP)
.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::ALL) Arg::new(options::ALL)
.short('a') .short('a')
.long(options::ALL) .long(options::ALL)
.help("write counts for all files, not just directories"), .help("write counts for all files, not just directories")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::APPARENT_SIZE) Arg::new(options::APPARENT_SIZE)
@ -736,6 +740,7 @@ pub fn uu_app<'a>() -> Command<'a> {
although the apparent size is usually smaller, it may be larger due to holes \ although the apparent size is usually smaller, it may be larger due to holes \
in ('sparse') files, internal fragmentation, indirect blocks, and the like" in ('sparse') files, internal fragmentation, indirect blocks, and the like"
) )
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::BLOCK_SIZE) Arg::new(options::BLOCK_SIZE)
@ -752,12 +757,14 @@ pub fn uu_app<'a>() -> Command<'a> {
.short('b') .short('b')
.long("bytes") .long("bytes")
.help("equivalent to '--apparent-size --block-size=1'") .help("equivalent to '--apparent-size --block-size=1'")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::TOTAL) Arg::new(options::TOTAL)
.long("total") .long("total")
.short('c') .short('c')
.help("produce a grand total") .help("produce a grand total")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::MAX_DEPTH) Arg::new(options::MAX_DEPTH)
@ -775,6 +782,7 @@ pub fn uu_app<'a>() -> Command<'a> {
.long("human-readable") .long("human-readable")
.short('h') .short('h')
.help("print sizes in human readable format (e.g., 1K 234M 2G)") .help("print sizes in human readable format (e.g., 1K 234M 2G)")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::INODES) Arg::new(options::INODES)
@ -782,70 +790,81 @@ pub fn uu_app<'a>() -> Command<'a> {
.help( .help(
"list inode usage information instead of block usage like --block-size=1K" "list inode usage information instead of block usage like --block-size=1K"
) )
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::BLOCK_SIZE_1K) Arg::new(options::BLOCK_SIZE_1K)
.short('k') .short('k')
.help("like --block-size=1K") .help("like --block-size=1K")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::COUNT_LINKS) Arg::new(options::COUNT_LINKS)
.short('l') .short('l')
.long("count-links") .long("count-links")
.help("count sizes many times if hard linked") .help("count sizes many times if hard linked")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::DEREFERENCE) Arg::new(options::DEREFERENCE)
.short('L') .short('L')
.long(options::DEREFERENCE) .long(options::DEREFERENCE)
.help("dereference all symbolic links") .help("dereference all symbolic links")
.action(ArgAction::SetTrue)
) )
// .arg( // .arg(
// Arg::new("no-dereference") // Arg::new("no-dereference")
// .short('P') // .short('P')
// .long("no-dereference") // .long("no-dereference")
// .help("don't follow any symbolic links (this is the default)") // .help("don't follow any symbolic links (this is the default)")
// .action(ArgAction::SetTrue),
// ) // )
.arg( .arg(
Arg::new(options::BLOCK_SIZE_1M) Arg::new(options::BLOCK_SIZE_1M)
.short('m') .short('m')
.help("like --block-size=1M") .help("like --block-size=1M")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::NULL) Arg::new(options::NULL)
.short('0') .short('0')
.long("null") .long("null")
.help("end each output line with 0 byte rather than newline") .help("end each output line with 0 byte rather than newline")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::SEPARATE_DIRS) Arg::new(options::SEPARATE_DIRS)
.short('S') .short('S')
.long("separate-dirs") .long("separate-dirs")
.help("do not include size of subdirectories") .help("do not include size of subdirectories")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::SUMMARIZE) Arg::new(options::SUMMARIZE)
.short('s') .short('s')
.long("summarize") .long("summarize")
.help("display only a total for each argument") .help("display only a total for each argument")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::SI) Arg::new(options::SI)
.long(options::SI) .long(options::SI)
.help("like -h, but use powers of 1000 not 1024") .help("like -h, but use powers of 1000 not 1024")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::ONE_FILE_SYSTEM) Arg::new(options::ONE_FILE_SYSTEM)
.short('x') .short('x')
.long(options::ONE_FILE_SYSTEM) .long(options::ONE_FILE_SYSTEM)
.help("skip directories on different file systems") .help("skip directories on different file systems")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::THRESHOLD) Arg::new(options::THRESHOLD)
.short('t') .short('t')
.long(options::THRESHOLD) .long(options::THRESHOLD)
.value_name("SIZE") .value_name("SIZE")
.number_of_values(1) .num_args(1)
.allow_hyphen_values(true) .allow_hyphen_values(true)
.help("exclude entries smaller than SIZE if positive, \ .help("exclude entries smaller than SIZE if positive, \
or entries greater than SIZE if negative") or entries greater than SIZE if negative")
@ -855,13 +874,14 @@ pub fn uu_app<'a>() -> Command<'a> {
.short('v') .short('v')
.long("verbose") .long("verbose")
.help("verbose mode (option not present in GNU/Coreutils)") .help("verbose mode (option not present in GNU/Coreutils)")
.action(ArgAction::SetTrue)
) )
.arg( .arg(
Arg::new(options::EXCLUDE) Arg::new(options::EXCLUDE)
.long(options::EXCLUDE) .long(options::EXCLUDE)
.value_name("PATTERN") .value_name("PATTERN")
.help("exclude files that match PATTERN") .help("exclude files that match PATTERN")
.multiple_occurrences(true) .action(ArgAction::Append)
) )
.arg( .arg(
Arg::new(options::EXCLUDE_FROM) Arg::new(options::EXCLUDE_FROM)
@ -870,15 +890,14 @@ pub fn uu_app<'a>() -> Command<'a> {
.value_name("FILE") .value_name("FILE")
.value_hint(clap::ValueHint::FilePath) .value_hint(clap::ValueHint::FilePath)
.help("exclude files that match any pattern in FILE") .help("exclude files that match any pattern in FILE")
.multiple_occurrences(true) .action(ArgAction::Append)
) )
.arg( .arg(
Arg::new(options::TIME) Arg::new(options::TIME)
.long(options::TIME) .long(options::TIME)
.value_name("WORD") .value_name("WORD")
.require_equals(true) .require_equals(true)
.min_values(0) .num_args(0..)
.value_parser(["atime", "access", "use", "ctime", "status", "birth", "creation"]) .value_parser(["atime", "access", "use", "ctime", "status", "birth", "creation"])
.help( .help(
"show time of the last modification of any file in the \ "show time of the last modification of any file in the \
@ -899,7 +918,7 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::FILE) Arg::new(options::FILE)
.hide(true) .hide(true)
.value_hint(clap::ValueHint::AnyPath) .value_hint(clap::ValueHint::AnyPath)
.multiple_occurrences(true) .action(ArgAction::Append)
) )
} }