diff --git a/src/uu/du/Cargo.toml b/src/uu/du/Cargo.toml index c0137726c..e41cc753e 100644 --- a/src/uu/du/Cargo.toml +++ b/src/uu/du/Cargo.toml @@ -18,7 +18,7 @@ path = "src/du.rs" chrono = { version="^0.4.19", default-features=false, features=["std", "alloc", "clock"]} # For the --exclude & --exclude-from options 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" } [target.'cfg(target_os = "windows")'.dependencies] diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 493ddd3f8..18e9a4af3 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -10,6 +10,7 @@ extern crate uucore; use chrono::prelude::DateTime; use chrono::Local; +use clap::ArgAction; use clap::{crate_version, Arg, ArgMatches, Command}; use glob::Pattern; use std::collections::HashSet; @@ -282,9 +283,9 @@ fn read_block_size(s: Option<&str>) -> u64 { } fn choose_size(matches: &ArgMatches, stat: &Stat) -> u64 { - if matches.contains_id(options::INODES) { + if matches.get_flag(options::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 } else { // 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> { let mut exclude_patterns = Vec::new(); 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); } 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 summarize = matches.contains_id(options::SUMMARIZE); + let summarize = matches.get_flag(options::SUMMARIZE); let max_depth = parse_depth( matches @@ -529,14 +530,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { )?; let options = Options { - all: matches.contains_id(options::ALL), + all: matches.get_flag(options::ALL), max_depth, - total: matches.contains_id(options::TOTAL), - separate_dirs: matches.contains_id(options::SEPARATE_DIRS), - one_file_system: matches.contains_id(options::ONE_FILE_SYSTEM), - dereference: matches.contains_id(options::DEREFERENCE), - inodes: matches.contains_id(options::INODES), - verbose: matches.contains_id(options::VERBOSE), + total: matches.get_flag(options::TOTAL), + separate_dirs: matches.get_flag(options::SEPARATE_DIRS), + one_file_system: matches.get_flag(options::ONE_FILE_SYSTEM), + dereference: matches.get_flag(options::DEREFERENCE), + inodes: matches.get_flag(options::INODES), + verbose: matches.get_flag(options::VERBOSE), }; let files = match matches.get_one::(options::FILE) { @@ -549,7 +550,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { }; 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"); } @@ -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))) }); - let multiplier: u64 = if matches.contains_id(options::SI) { + let multiplier: u64 = if matches.get_flag(options::SI) { 1000 } else { 1024 }; 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 - } else if matches.contains_id(options::BYTES) { + } else if matches.get_flag(options::BYTES) { convert_size_b - } else if matches.contains_id(options::BLOCK_SIZE_1K) { + } else if matches.get_flag(options::BLOCK_SIZE_1K) { convert_size_k - } else if matches.contains_id(options::BLOCK_SIZE_1M) { + } else if matches.get_flag(options::BLOCK_SIZE_1M) { convert_size_m } else { convert_size_other @@ -594,7 +595,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let time_format_str = parse_time_style(matches.get_one::("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" } else { "\n" @@ -710,23 +711,26 @@ fn parse_depth(max_depth_str: Option<&str>, summarize: bool) -> UResult() -> Command<'a> { +pub fn uu_app() -> Command { Command::new(uucore::util_name()) .version(crate_version!()) .about(ABOUT) .after_help(LONG_HELP) .override_usage(format_usage(USAGE)) .infer_long_args(true) + .disable_help_flag(true) .arg( Arg::new(options::HELP) .long(options::HELP) .help("Print help information.") + .action(ArgAction::Help) ) .arg( Arg::new(options::ALL) .short('a') .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::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 \ in ('sparse') files, internal fragmentation, indirect blocks, and the like" ) + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::BLOCK_SIZE) @@ -752,12 +757,14 @@ pub fn uu_app<'a>() -> Command<'a> { .short('b') .long("bytes") .help("equivalent to '--apparent-size --block-size=1'") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::TOTAL) .long("total") .short('c') .help("produce a grand total") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::MAX_DEPTH) @@ -775,6 +782,7 @@ pub fn uu_app<'a>() -> Command<'a> { .long("human-readable") .short('h') .help("print sizes in human readable format (e.g., 1K 234M 2G)") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::INODES) @@ -782,70 +790,81 @@ pub fn uu_app<'a>() -> Command<'a> { .help( "list inode usage information instead of block usage like --block-size=1K" ) + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::BLOCK_SIZE_1K) .short('k') .help("like --block-size=1K") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::COUNT_LINKS) .short('l') .long("count-links") .help("count sizes many times if hard linked") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::DEREFERENCE) .short('L') .long(options::DEREFERENCE) .help("dereference all symbolic links") + .action(ArgAction::SetTrue) ) // .arg( // Arg::new("no-dereference") // .short('P') // .long("no-dereference") // .help("don't follow any symbolic links (this is the default)") + // .action(ArgAction::SetTrue), // ) .arg( Arg::new(options::BLOCK_SIZE_1M) .short('m') .help("like --block-size=1M") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::NULL) .short('0') .long("null") .help("end each output line with 0 byte rather than newline") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::SEPARATE_DIRS) .short('S') .long("separate-dirs") .help("do not include size of subdirectories") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::SUMMARIZE) .short('s') .long("summarize") .help("display only a total for each argument") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::SI) .long(options::SI) .help("like -h, but use powers of 1000 not 1024") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::ONE_FILE_SYSTEM) .short('x') .long(options::ONE_FILE_SYSTEM) .help("skip directories on different file systems") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::THRESHOLD) .short('t') .long(options::THRESHOLD) .value_name("SIZE") - .number_of_values(1) + .num_args(1) .allow_hyphen_values(true) .help("exclude entries smaller than SIZE if positive, \ or entries greater than SIZE if negative") @@ -855,13 +874,14 @@ pub fn uu_app<'a>() -> Command<'a> { .short('v') .long("verbose") .help("verbose mode (option not present in GNU/Coreutils)") + .action(ArgAction::SetTrue) ) .arg( Arg::new(options::EXCLUDE) .long(options::EXCLUDE) .value_name("PATTERN") .help("exclude files that match PATTERN") - .multiple_occurrences(true) + .action(ArgAction::Append) ) .arg( Arg::new(options::EXCLUDE_FROM) @@ -870,15 +890,14 @@ pub fn uu_app<'a>() -> Command<'a> { .value_name("FILE") .value_hint(clap::ValueHint::FilePath) .help("exclude files that match any pattern in FILE") - .multiple_occurrences(true) - + .action(ArgAction::Append) ) .arg( Arg::new(options::TIME) .long(options::TIME) .value_name("WORD") .require_equals(true) - .min_values(0) + .num_args(0..) .value_parser(["atime", "access", "use", "ctime", "status", "birth", "creation"]) .help( "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) .hide(true) .value_hint(clap::ValueHint::AnyPath) - .multiple_occurrences(true) + .action(ArgAction::Append) ) }