diff --git a/src/uu/basename/src/basename.rs b/src/uu/basename/src/basename.rs index af961f5fb..4d0125330 100644 --- a/src/uu/basename/src/basename.rs +++ b/src/uu/basename/src/basename.rs @@ -60,8 +60,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let opt_multiple = matches.contains_id(options::MULTIPLE); let opt_zero = matches.contains_id(options::ZERO); let multiple_paths = opt_suffix || opt_multiple; + let name_args_count = matches + .get_many::(options::NAME) + .map(|n| n.len()) + .unwrap_or(0); + // too many arguments - if !multiple_paths && matches.occurrences_of(options::NAME) > 2 { + if !multiple_paths && name_args_count > 2 { return Err(UUsageError::new( 1, format!( @@ -78,7 +83,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let suffix = if opt_suffix { matches.value_of(options::SUFFIX).unwrap() - } else if !opt_multiple && matches.occurrences_of(options::NAME) > 1 { + } else if !opt_multiple && name_args_count > 1 { matches .get_many::(options::NAME) .unwrap() diff --git a/src/uu/df/src/columns.rs b/src/uu/df/src/columns.rs index f1c1ffec0..16ecf8b05 100644 --- a/src/uu/df/src/columns.rs +++ b/src/uu/df/src/columns.rs @@ -4,7 +4,7 @@ // * file that was distributed with this source code. // spell-checker:ignore itotal iused iavail ipcent pcent squashfs use crate::{OPT_INODES, OPT_OUTPUT, OPT_PRINT_TYPE}; -use clap::ArgMatches; +use clap::{ArgMatches, ValueSource}; /// The columns in the output table produced by `df`. /// @@ -77,7 +77,7 @@ impl Column { match ( matches.contains_id(OPT_PRINT_TYPE), matches.contains_id(OPT_INODES), - matches.occurrences_of(OPT_OUTPUT) > 0, + matches.value_source(OPT_OUTPUT) == Some(ValueSource::CommandLine), ) { (false, false, false) => Ok(vec![ Self::Source, diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 250786acd..425a5bb0e 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -20,7 +20,7 @@ use uucore::fsext::{read_fs_list, MountInfo}; use uucore::parse_size::ParseSizeError; use uucore::{format_usage, show}; -use clap::{crate_version, Arg, ArgMatches, Command}; +use clap::{crate_version, Arg, ArgMatches, Command, ValueSource}; use std::error::Error; use std::fmt; @@ -200,7 +200,7 @@ impl Options { HeaderMode::PosixPortability // contains_id() doesn't work here, it always returns true because OPT_OUTPUT has // default values and hence is always present - } else if matches.occurrences_of(OPT_OUTPUT) > 0 { + } else if matches.value_source(OPT_OUTPUT) == Some(ValueSource::CommandLine) { HeaderMode::Output } else { HeaderMode::Default diff --git a/src/uu/mv/src/mv.rs b/src/uu/mv/src/mv.rs index 8d9fa5650..63d20e93f 100644 --- a/src/uu/mv/src/mv.rs +++ b/src/uu/mv/src/mv.rs @@ -80,7 +80,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .try_get_matches_from_mut(args) .unwrap_or_else(|e| e.exit()); - if !matches.contains_id(OPT_TARGET_DIRECTORY) && matches.occurrences_of(ARG_FILES) == 1 { + if !matches.contains_id(OPT_TARGET_DIRECTORY) + && matches + .get_many::(ARG_FILES) + .map(|f| f.len()) + .unwrap_or(0) + == 1 + { app.error( ErrorKind::TooFewValues, format!( diff --git a/src/uu/numfmt/src/numfmt.rs b/src/uu/numfmt/src/numfmt.rs index 189acbd51..36eb295cc 100644 --- a/src/uu/numfmt/src/numfmt.rs +++ b/src/uu/numfmt/src/numfmt.rs @@ -9,7 +9,7 @@ use crate::errors::*; use crate::format::format_and_print; use crate::options::*; use crate::units::{Result, Unit}; -use clap::{crate_version, Arg, ArgMatches, Command}; +use clap::{crate_version, Arg, ArgMatches, Command, ValueSource}; use std::io::{BufRead, Write}; use units::{IEC_BASES, SI_BASES}; use uucore::display::Quotable; @@ -143,20 +143,19 @@ fn parse_options(args: &ArgMatches) -> Result { None => Ok(0), }?; - let header = match args.occurrences_of(options::HEADER) { - 0 => Ok(0), - _ => { - let value = args.value_of(options::HEADER).unwrap(); + let header = if args.value_source(options::HEADER) == Some(ValueSource::CommandLine) { + let value = args.value_of(options::HEADER).unwrap(); - value - .parse::() - .map_err(|_| value) - .and_then(|n| match n { - 0 => Err(value), - _ => Ok(n), - }) - .map_err(|value| format!("invalid header value {}", value.quote())) - } + value + .parse::() + .map_err(|_| value) + .and_then(|n| match n { + 0 => Err(value), + _ => Ok(n), + }) + .map_err(|value| format!("invalid header value {}", value.quote())) + } else { + Ok(0) }?; let fields = match args.value_of(options::FIELD).unwrap() { diff --git a/src/uu/od/src/od.rs b/src/uu/od/src/od.rs index 81068f0b7..3d99b8fa8 100644 --- a/src/uu/od/src/od.rs +++ b/src/uu/od/src/od.rs @@ -43,7 +43,7 @@ use crate::parse_nrofbytes::parse_number_of_bytes; use crate::partialreader::*; use crate::peekreader::*; use crate::prn_char::format_ascii_dump; -use clap::{crate_version, AppSettings, Arg, ArgMatches, Command}; +use clap::{crate_version, AppSettings, Arg, ArgMatches, Command, ValueSource}; use uucore::display::Quotable; use uucore::error::{UResult, USimpleError}; use uucore::format_usage; @@ -167,9 +167,7 @@ impl OdOptions { let mut line_bytes = match matches.value_of(options::WIDTH) { None => 16, Some(s) => { - if matches.occurrences_of(options::WIDTH) == 0 { - 16 - } else { + if matches.value_source(options::WIDTH) == Some(ValueSource::CommandLine) { match parse_number_of_bytes(s) { Ok(n) => usize::try_from(n) .map_err(|_| USimpleError::new(1, format!("‘{}‘ is too large", s)))?, @@ -180,6 +178,8 @@ impl OdOptions { )) } } + } else { + 16 } } }; diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index 37e535b6b..6652af5a0 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -13,7 +13,7 @@ mod platform; use crate::filenames::FilenameIterator; use crate::filenames::SuffixType; -use clap::{crate_version, Arg, ArgMatches, Command}; +use clap::{crate_version, Arg, ArgMatches, Command, ValueSource}; use std::env; use std::fmt; use std::fs::{metadata, File}; @@ -368,28 +368,28 @@ impl Strategy { // `ArgGroup` since `ArgGroup` considers a default value `Arg` // as "defined". match ( - matches.occurrences_of(OPT_LINES), - matches.occurrences_of(OPT_BYTES), - matches.occurrences_of(OPT_LINE_BYTES), - matches.occurrences_of(OPT_NUMBER), + matches.value_source(OPT_LINES) == Some(ValueSource::CommandLine), + matches.value_source(OPT_BYTES) == Some(ValueSource::CommandLine), + matches.value_source(OPT_LINE_BYTES) == Some(ValueSource::CommandLine), + matches.value_source(OPT_NUMBER) == Some(ValueSource::CommandLine), ) { - (0, 0, 0, 0) => Ok(Self::Lines(1000)), - (1, 0, 0, 0) => { + (false, false, false, false) => Ok(Self::Lines(1000)), + (true, false, false, false) => { let s = matches.value_of(OPT_LINES).unwrap(); let n = parse_size(s).map_err(StrategyError::Lines)?; Ok(Self::Lines(n)) } - (0, 1, 0, 0) => { + (false, true, false, false) => { let s = matches.value_of(OPT_BYTES).unwrap(); let n = parse_size(s).map_err(StrategyError::Bytes)?; Ok(Self::Bytes(n)) } - (0, 0, 1, 0) => { + (false, false, true, false) => { let s = matches.value_of(OPT_LINE_BYTES).unwrap(); let n = parse_size(s).map_err(StrategyError::Bytes)?; Ok(Self::LineBytes(n)) } - (0, 0, 0, 1) => { + (false, false, false, true) => { let s = matches.value_of(OPT_NUMBER).unwrap(); let number_type = NumberType::from(s).map_err(StrategyError::NumberType)?; Ok(Self::Number(number_type)) @@ -401,9 +401,9 @@ impl Strategy { /// Parse the suffix type from the command-line arguments. fn suffix_type_from(matches: &ArgMatches) -> SuffixType { - if matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0 { + if matches.value_source(OPT_NUMERIC_SUFFIXES) == Some(ValueSource::CommandLine) { SuffixType::Decimal - } else if matches.occurrences_of(OPT_HEX_SUFFIXES) > 0 { + } else if matches.value_source(OPT_HEX_SUFFIXES) == Some(ValueSource::CommandLine) { SuffixType::Hexadecimal } else { SuffixType::Alphabetic @@ -515,7 +515,7 @@ impl Settings { .map_err(|_| SettingsError::SuffixNotParsable(suffix_length_str.to_string()))?, suffix_type, additional_suffix, - verbose: matches.occurrences_of("verbose") > 0, + verbose: matches.value_source("verbose") == Some(ValueSource::CommandLine), strategy, input: matches.value_of(ARG_INPUT).unwrap().to_owned(), prefix: matches.value_of(ARG_PREFIX).unwrap().to_owned(), diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index 32ebb21b0..ee70f7ef1 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -28,7 +28,7 @@ mod platform; use crate::files::FileHandling; use chunks::ReverseChunks; -use clap::{Arg, Command}; +use clap::{Arg, Command, ValueSource}; use notify::{RecommendedWatcher, RecursiveMode, Watcher, WatcherKind}; use std::collections::{HashMap, VecDeque}; use std::ffi::OsString; @@ -140,7 +140,7 @@ impl Settings { settings.follow = if matches.contains_id(options::FOLLOW_RETRY) { Some(FollowMode::Name) - } else if matches.occurrences_of(options::FOLLOW) == 0 { + } else if matches.value_source(options::FOLLOW) != Some(ValueSource::CommandLine) { None } else if matches.value_of(options::FOLLOW) == Some("name") { Some(FollowMode::Name)