1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-16 11:46:17 +00:00

df: update to clap 4

This commit is contained in:
Terts Diepraam 2022-09-29 16:57:04 +02:00
parent 2420830e9d
commit 811a06fd66
4 changed files with 48 additions and 39 deletions

View file

@ -15,7 +15,7 @@ edition = "2021"
path = "src/df.rs" path = "src/df.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", features=["libc", "fsext"] } uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["libc", "fsext"] }
unicode-width = "0.1.9" unicode-width = "0.1.9"

View file

@ -172,7 +172,7 @@ pub(crate) fn read_block_size(matches: &ArgMatches) -> Result<BlockSize, ParseSi
} else { } else {
Err(ParseSizeError::ParseFailure(format!("{}", s.quote()))) Err(ParseSizeError::ParseFailure(format!("{}", s.quote())))
} }
} else if matches.contains_id(OPT_PORTABILITY) { } else if matches.get_flag(OPT_PORTABILITY) {
Ok(BlockSize::default()) Ok(BlockSize::default())
} else if let Some(bytes) = block_size_from_env() { } else if let Some(bytes) = block_size_from_env() {
Ok(BlockSize::Bytes(bytes)) Ok(BlockSize::Bytes(bytes))

View file

@ -4,7 +4,7 @@
// * file that was distributed with this source code. // * file that was distributed with this source code.
// spell-checker:ignore itotal iused iavail ipcent pcent squashfs // spell-checker:ignore itotal iused iavail ipcent pcent squashfs
use crate::{OPT_INODES, OPT_OUTPUT, OPT_PRINT_TYPE}; use crate::{OPT_INODES, OPT_OUTPUT, OPT_PRINT_TYPE};
use clap::{ArgMatches, ValueSource}; use clap::{parser::ValueSource, ArgMatches};
/// The columns in the output table produced by `df`. /// The columns in the output table produced by `df`.
/// ///
@ -75,8 +75,8 @@ impl Column {
/// than once in the command-line argument. /// than once in the command-line argument.
pub(crate) fn from_matches(matches: &ArgMatches) -> Result<Vec<Self>, ColumnError> { pub(crate) fn from_matches(matches: &ArgMatches) -> Result<Vec<Self>, ColumnError> {
match ( match (
matches.contains_id(OPT_PRINT_TYPE), matches.get_flag(OPT_PRINT_TYPE),
matches.contains_id(OPT_INODES), matches.get_flag(OPT_INODES),
matches.value_source(OPT_OUTPUT) == Some(ValueSource::CommandLine), matches.value_source(OPT_OUTPUT) == Some(ValueSource::CommandLine),
) { ) {
(false, false, false) => Ok(vec![ (false, false, false) => Ok(vec![

View file

@ -21,7 +21,7 @@ use uucore::fsext::{read_fs_list, MountInfo};
use uucore::parse_size::ParseSizeError; use uucore::parse_size::ParseSizeError;
use uucore::{format_usage, show}; use uucore::{format_usage, show};
use clap::{crate_version, Arg, ArgMatches, Command, ValueSource}; use clap::{crate_version, parser::ValueSource, Arg, ArgAction, ArgMatches, Command};
use std::error::Error; use std::error::Error;
use std::ffi::OsString; use std::ffi::OsString;
@ -187,9 +187,9 @@ impl Options {
} }
Ok(Self { Ok(Self {
show_local_fs: matches.contains_id(OPT_LOCAL), show_local_fs: matches.get_flag(OPT_LOCAL),
show_all_fs: matches.contains_id(OPT_ALL), show_all_fs: matches.get_flag(OPT_ALL),
sync: matches.contains_id(OPT_SYNC), sync: matches.get_flag(OPT_SYNC),
block_size: read_block_size(matches).map_err(|e| match e { block_size: read_block_size(matches).map_err(|e| match e {
ParseSizeError::InvalidSuffix(s) => OptionsError::InvalidSuffix(s), ParseSizeError::InvalidSuffix(s) => OptionsError::InvalidSuffix(s),
ParseSizeError::SizeTooBig(_) => OptionsError::BlockSizeTooLarge( ParseSizeError::SizeTooBig(_) => OptionsError::BlockSizeTooLarge(
@ -201,13 +201,13 @@ impl Options {
ParseSizeError::ParseFailure(s) => OptionsError::InvalidBlockSize(s), ParseSizeError::ParseFailure(s) => OptionsError::InvalidBlockSize(s),
})?, })?,
header_mode: { header_mode: {
if matches.contains_id(OPT_HUMAN_READABLE_BINARY) if matches.get_flag(OPT_HUMAN_READABLE_BINARY)
|| matches.contains_id(OPT_HUMAN_READABLE_DECIMAL) || matches.get_flag(OPT_HUMAN_READABLE_DECIMAL)
{ {
HeaderMode::HumanReadable HeaderMode::HumanReadable
} else if matches.contains_id(OPT_PORTABILITY) { } else if matches.get_flag(OPT_PORTABILITY) {
HeaderMode::PosixPortability HeaderMode::PosixPortability
// contains_id() doesn't work here, it always returns true because OPT_OUTPUT has // get_flag() doesn't work here, it always returns true because OPT_OUTPUT has
// default values and hence is always present // default values and hence is always present
} else if matches.value_source(OPT_OUTPUT) == Some(ValueSource::CommandLine) { } else if matches.value_source(OPT_OUTPUT) == Some(ValueSource::CommandLine) {
HeaderMode::Output HeaderMode::Output
@ -216,9 +216,9 @@ impl Options {
} }
}, },
human_readable: { human_readable: {
if matches.contains_id(OPT_HUMAN_READABLE_BINARY) { if matches.get_flag(OPT_HUMAN_READABLE_BINARY) {
Some(HumanReadable::Binary) Some(HumanReadable::Binary)
} else if matches.contains_id(OPT_HUMAN_READABLE_DECIMAL) { } else if matches.get_flag(OPT_HUMAN_READABLE_DECIMAL) {
Some(HumanReadable::Decimal) Some(HumanReadable::Decimal)
} else { } else {
None None
@ -226,7 +226,7 @@ impl Options {
}, },
include, include,
exclude, exclude,
show_total: matches.contains_id(OPT_TOTAL), show_total: matches.get_flag(OPT_TOTAL),
columns: Column::from_matches(matches).map_err(OptionsError::ColumnError)?, columns: Column::from_matches(matches).map_err(OptionsError::ColumnError)?,
}) })
} }
@ -443,7 +443,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
#[cfg(windows)] #[cfg(windows)]
{ {
if matches.contains_id(OPT_INODES) { if matches.get_flag(OPT_INODES) {
println!("{}: doesn't support -i option", uucore::util_name()); println!("{}: doesn't support -i option", uucore::util_name());
return Ok(()); return Ok(());
} }
@ -482,30 +482,32 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
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))
.after_help(LONG_HELP) .after_help(LONG_HELP)
.infer_long_args(true) .infer_long_args(true)
.disable_help_flag(true)
.arg( .arg(
Arg::new(OPT_HELP) Arg::new(OPT_HELP)
.long(OPT_HELP) .long(OPT_HELP)
.help("Print help information."), .help("Print help information.")
.action(ArgAction::Help),
) )
.arg( .arg(
Arg::new(OPT_ALL) Arg::new(OPT_ALL)
.short('a') .short('a')
.long("all") .long("all")
.overrides_with(OPT_ALL) .overrides_with(OPT_ALL)
.help("include dummy file systems"), .help("include dummy file systems")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_BLOCKSIZE) Arg::new(OPT_BLOCKSIZE)
.short('B') .short('B')
.long("block-size") .long("block-size")
.takes_value(true)
.value_name("SIZE") .value_name("SIZE")
.overrides_with_all(&[OPT_KILO, OPT_BLOCKSIZE]) .overrides_with_all(&[OPT_KILO, OPT_BLOCKSIZE])
.help( .help(
@ -517,57 +519,63 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(OPT_TOTAL) Arg::new(OPT_TOTAL)
.long("total") .long("total")
.overrides_with(OPT_TOTAL) .overrides_with(OPT_TOTAL)
.help("produce a grand total"), .help("produce a grand total")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_HUMAN_READABLE_BINARY) Arg::new(OPT_HUMAN_READABLE_BINARY)
.short('h') .short('h')
.long("human-readable") .long("human-readable")
.overrides_with_all(&[OPT_HUMAN_READABLE_DECIMAL, OPT_HUMAN_READABLE_BINARY]) .overrides_with_all(&[OPT_HUMAN_READABLE_DECIMAL, OPT_HUMAN_READABLE_BINARY])
.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(OPT_HUMAN_READABLE_DECIMAL) Arg::new(OPT_HUMAN_READABLE_DECIMAL)
.short('H') .short('H')
.long("si") .long("si")
.overrides_with_all(&[OPT_HUMAN_READABLE_BINARY, OPT_HUMAN_READABLE_DECIMAL]) .overrides_with_all(&[OPT_HUMAN_READABLE_BINARY, OPT_HUMAN_READABLE_DECIMAL])
.help("likewise, but use powers of 1000 not 1024"), .help("likewise, but use powers of 1000 not 1024")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_INODES) Arg::new(OPT_INODES)
.short('i') .short('i')
.long("inodes") .long("inodes")
.overrides_with(OPT_INODES) .overrides_with(OPT_INODES)
.help("list inode information instead of block usage"), .help("list inode information instead of block usage")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_KILO) Arg::new(OPT_KILO)
.short('k') .short('k')
.help("like --block-size=1K") .help("like --block-size=1K")
.overrides_with_all(&[OPT_BLOCKSIZE, OPT_KILO]), .overrides_with_all(&[OPT_BLOCKSIZE, OPT_KILO])
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_LOCAL) Arg::new(OPT_LOCAL)
.short('l') .short('l')
.long("local") .long("local")
.overrides_with(OPT_LOCAL) .overrides_with(OPT_LOCAL)
.help("limit listing to local file systems"), .help("limit listing to local file systems")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_NO_SYNC) Arg::new(OPT_NO_SYNC)
.long("no-sync") .long("no-sync")
.overrides_with_all(&[OPT_SYNC, OPT_NO_SYNC]) .overrides_with_all(&[OPT_SYNC, OPT_NO_SYNC])
.help("do not invoke sync before getting usage info (default)"), .help("do not invoke sync before getting usage info (default)")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_OUTPUT) Arg::new(OPT_OUTPUT)
.long("output") .long("output")
.takes_value(true)
.value_name("FIELD_LIST") .value_name("FIELD_LIST")
.min_values(0) .action(ArgAction::Append)
.num_args(0..)
.require_equals(true) .require_equals(true)
.use_value_delimiter(true) .use_value_delimiter(true)
.multiple_occurrences(true)
.value_parser(OUTPUT_FIELD_LIST) .value_parser(OUTPUT_FIELD_LIST)
.default_missing_values(&OUTPUT_FIELD_LIST) .default_missing_values(&OUTPUT_FIELD_LIST)
.default_values(&["source", "size", "used", "avail", "pcent", "target"]) .default_values(&["source", "size", "used", "avail", "pcent", "target"])
@ -582,22 +590,23 @@ pub fn uu_app<'a>() -> Command<'a> {
.short('P') .short('P')
.long("portability") .long("portability")
.overrides_with(OPT_PORTABILITY) .overrides_with(OPT_PORTABILITY)
.help("use the POSIX output format"), .help("use the POSIX output format")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_SYNC) Arg::new(OPT_SYNC)
.long("sync") .long("sync")
.overrides_with_all(&[OPT_NO_SYNC, OPT_SYNC]) .overrides_with_all(&[OPT_NO_SYNC, OPT_SYNC])
.help("invoke sync before getting usage info (non-windows only)"), .help("invoke sync before getting usage info (non-windows only)")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_TYPE) Arg::new(OPT_TYPE)
.short('t') .short('t')
.long("type") .long("type")
.value_parser(ValueParser::os_string()) .value_parser(ValueParser::os_string())
.takes_value(true)
.value_name("TYPE") .value_name("TYPE")
.multiple_occurrences(true) .action(ArgAction::Append)
.help("limit listing to file systems of type TYPE"), .help("limit listing to file systems of type TYPE"),
) )
.arg( .arg(
@ -605,22 +614,22 @@ pub fn uu_app<'a>() -> Command<'a> {
.short('T') .short('T')
.long("print-type") .long("print-type")
.overrides_with(OPT_PRINT_TYPE) .overrides_with(OPT_PRINT_TYPE)
.help("print file system type"), .help("print file system type")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_EXCLUDE_TYPE) Arg::new(OPT_EXCLUDE_TYPE)
.short('x') .short('x')
.long("exclude-type") .long("exclude-type")
.action(ArgAction::Append)
.value_parser(ValueParser::os_string()) .value_parser(ValueParser::os_string())
.takes_value(true)
.value_name("TYPE") .value_name("TYPE")
.use_value_delimiter(true) .use_value_delimiter(true)
.multiple_occurrences(true)
.help("limit listing to file systems not of type TYPE"), .help("limit listing to file systems not of type TYPE"),
) )
.arg( .arg(
Arg::new(OPT_PATHS) Arg::new(OPT_PATHS)
.multiple_occurrences(true) .action(ArgAction::Append)
.value_hint(clap::ValueHint::AnyPath), .value_hint(clap::ValueHint::AnyPath),
) )
} }