Merge pull request #3828 from cakebaker/clap_replace_deprecated_value_of

Replace deprecated value_of() with get_one()
This commit is contained in:
Sylvestre Ledru
2022-09-27 06:59:54 -10:00
committed by GitHub
60 changed files with 380 additions and 247 deletions
+1 -1
View File
@@ -152,7 +152,7 @@ fn gen_completions<T: uucore::Args>(
)
.get_matches_from(std::iter::once(OsString::from("completion")).chain(args));
let utility = matches.value_of("utility").unwrap();
let utility = matches.get_one::<String>("utility").unwrap();
let shell = matches.get_one::<Shell>("shell").unwrap().to_owned();
let mut command = if utility == "coreutils" {
+1 -1
View File
@@ -65,7 +65,7 @@ impl Config {
};
let cols = options
.value_of(options::WRAP)
.get_one::<String>(options::WRAP)
.map(|num| {
num.parse::<usize>().map_err(|_| {
USimpleError::new(
+1 -1
View File
@@ -82,7 +82,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
let suffix = if opt_suffix {
matches.value_of(options::SUFFIX).unwrap()
matches.get_one::<String>(options::SUFFIX).unwrap()
} else if !opt_multiple && name_args_count > 1 {
matches
.get_many::<String>(options::NAME)
+5 -2
View File
@@ -26,12 +26,15 @@ const USAGE: &str = "\
{} [OPTION]... --reference=RFILE FILE...";
fn parse_gid_and_uid(matches: &ArgMatches) -> UResult<(Option<u32>, Option<u32>, IfFrom)> {
let dest_gid = if let Some(file) = matches.value_of(options::REFERENCE) {
let dest_gid = if let Some(file) = matches.get_one::<String>(options::REFERENCE) {
fs::metadata(&file)
.map(|meta| Some(meta.gid()))
.map_err_context(|| format!("failed to get attributes of {}", file.quote()))?
} else {
let group = matches.value_of(options::ARG_GROUP).unwrap_or_default();
let group = matches
.get_one::<String>(options::ARG_GROUP)
.map(|s| s.as_str())
.unwrap_or_default();
if group.is_empty() {
None
} else {
+2 -2
View File
@@ -62,7 +62,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let verbose = matches.contains_id(options::VERBOSE);
let preserve_root = matches.contains_id(options::PRESERVE_ROOT);
let recursive = matches.contains_id(options::RECURSIVE);
let fmode = match matches.value_of(options::REFERENCE) {
let fmode = match matches.get_one::<String>(options::REFERENCE) {
Some(fref) => match fs::metadata(fref) {
Ok(meta) => Some(meta.mode()),
Err(err) => {
@@ -74,7 +74,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
},
None => None,
};
let modes = matches.value_of(options::MODE).unwrap(); // should always be Some because required
let modes = matches.get_one::<String>(options::MODE).unwrap(); // should always be Some because required
let cmode = if mode_had_minus_prefix {
// clap parsing is finished, now put prefix back
format!("-{}", modes)
+3 -3
View File
@@ -26,7 +26,7 @@ const USAGE: &str = "\
{} [OPTION]... --reference=RFILE FILE...";
fn parse_gid_uid_and_filter(matches: &ArgMatches) -> UResult<(Option<u32>, Option<u32>, IfFrom)> {
let filter = if let Some(spec) = matches.value_of(options::FROM) {
let filter = if let Some(spec) = matches.get_one::<String>(options::FROM) {
match parse_spec(spec, ':')? {
(Some(uid), None) => IfFrom::User(uid),
(None, Some(gid)) => IfFrom::Group(gid),
@@ -39,13 +39,13 @@ fn parse_gid_uid_and_filter(matches: &ArgMatches) -> UResult<(Option<u32>, Optio
let dest_uid: Option<u32>;
let dest_gid: Option<u32>;
if let Some(file) = matches.value_of(options::REFERENCE) {
if let Some(file) = matches.get_one::<String>(options::REFERENCE) {
let meta = fs::metadata(&file)
.map_err_context(|| format!("failed to get attributes of {}", file.quote()))?;
dest_gid = Some(meta.gid());
dest_uid = Some(meta.uid());
} else {
let (u, g) = parse_spec(matches.value_of(options::ARG_OWNER).unwrap(), ':')?;
let (u, g) = parse_spec(matches.get_one::<String>(options::ARG_OWNER).unwrap(), ':')?;
dest_uid = u;
dest_gid = g;
}
+14 -5
View File
@@ -43,7 +43,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let default_option: &'static str = "-i";
let user_shell = std::env::var("SHELL");
let newroot: &Path = match matches.value_of(options::NEWROOT) {
let newroot: &Path = match matches.get_one::<String>(options::NEWROOT) {
Some(v) => Path::new(v),
None => return Err(ChrootError::MissingNewRoot.into()),
};
@@ -165,10 +165,19 @@ pub fn uu_app<'a>() -> Command<'a> {
}
fn set_context(root: &Path, options: &clap::ArgMatches) -> UResult<()> {
let userspec_str = options.value_of(options::USERSPEC);
let user_str = options.value_of(options::USER).unwrap_or_default();
let group_str = options.value_of(options::GROUP).unwrap_or_default();
let groups_str = options.value_of(options::GROUPS).unwrap_or_default();
let userspec_str = options.get_one::<String>(options::USERSPEC);
let user_str = options
.get_one::<String>(options::USER)
.map(|s| s.as_str())
.unwrap_or_default();
let group_str = options
.get_one::<String>(options::GROUP)
.map(|s| s.as_str())
.unwrap_or_default();
let groups_str = options
.get_one::<String>(options::GROUPS)
.map(|s| s.as_str())
.unwrap_or_default();
let skip_chdir = options.contains_id(options::SKIP_CHDIR);
let userspec = match userspec_str {
Some(u) => {
+3 -3
View File
@@ -33,7 +33,7 @@ mod options {
fn mkdelim(col: usize, opts: &ArgMatches) -> String {
let mut s = String::new();
let delim = match opts.value_of(options::DELIMITER).unwrap() {
let delim = match opts.get_one::<String>(options::DELIMITER).unwrap().as_str() {
"" => "\0",
delim => delim,
};
@@ -135,8 +135,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args.collect_lossy();
let matches = uu_app().try_get_matches_from(args)?;
let filename1 = matches.value_of(options::FILE_1).unwrap();
let filename2 = matches.value_of(options::FILE_2).unwrap();
let filename1 = matches.get_one::<String>(options::FILE_1).unwrap();
let filename2 = matches.get_one::<String>(options::FILE_2).unwrap();
let mut f1 = open_file(filename1).map_err_context(|| filename1.to_string())?;
let mut f2 = open_file(filename2).map_err_context(|| filename2.to_string())?;
+18 -13
View File
@@ -722,7 +722,7 @@ impl Options {
// Parse target directory options
let no_target_dir = matches.contains_id(options::NO_TARGET_DIRECTORY);
let target_dir = matches
.value_of(options::TARGET_DIRECTORY)
.get_one::<String>(options::TARGET_DIRECTORY)
.map(ToString::to_string);
// Parse attributes to preserve
@@ -775,8 +775,8 @@ impl Options {
verbose: matches.contains_id(options::VERBOSE),
strip_trailing_slashes: matches.contains_id(options::STRIP_TRAILING_SLASHES),
reflink_mode: {
if let Some(reflink) = matches.value_of(options::REFLINK) {
match reflink {
if let Some(reflink) = matches.get_one::<String>(options::REFLINK) {
match reflink.as_str() {
"always" => ReflinkMode::Always,
"auto" => ReflinkMode::Auto,
"never" => ReflinkMode::Never,
@@ -802,17 +802,22 @@ impl Options {
}
}
},
sparse_mode: match matches.value_of(options::SPARSE) {
Some("always") => SparseMode::Always,
Some("auto") => SparseMode::Auto,
Some("never") => SparseMode::Never,
Some(val) => {
return Err(Error::InvalidArgument(format!(
"invalid argument {} for \'sparse\'",
val
)));
sparse_mode: {
if let Some(val) = matches.get_one::<String>(options::SPARSE) {
match val.as_str() {
"always" => SparseMode::Always,
"auto" => SparseMode::Auto,
"never" => SparseMode::Never,
_ => {
return Err(Error::InvalidArgument(format!(
"invalid argument {} for \'sparse\'",
val
)))
}
}
} else {
SparseMode::Auto
}
None => SparseMode::Auto,
},
backup: backup_mode,
backup_suffix,
+10 -4
View File
@@ -61,9 +61,15 @@ impl CsplitOptions {
split_name: crash_if_err!(
1,
SplitName::new(
matches.value_of(options::PREFIX).map(str::to_string),
matches.value_of(options::SUFFIX_FORMAT).map(str::to_string),
matches.value_of(options::DIGITS).map(str::to_string)
matches
.get_one::<String>(options::PREFIX)
.map(|s| s.to_owned()),
matches
.get_one::<String>(options::SUFFIX_FORMAT)
.map(|s| s.to_owned()),
matches
.get_one::<String>(options::DIGITS)
.map(|s| s.to_owned())
)
),
keep_files,
@@ -718,7 +724,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
// get the file to split
let file_name = matches.value_of(options::FILE).unwrap();
let file_name = matches.get_one::<String>(options::FILE).unwrap();
// get the patterns to split on
let patterns: Vec<String> = matches
+9 -7
View File
@@ -406,9 +406,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let complement = matches.contains_id(options::COMPLEMENT);
let mode_parse = match (
matches.value_of(options::BYTES),
matches.value_of(options::CHARACTERS),
matches.value_of(options::FIELDS),
matches.get_one::<String>(options::BYTES),
matches.get_one::<String>(options::CHARACTERS),
matches.get_one::<String>(options::FIELDS),
) {
(Some(byte_ranges), None, None) => list_to_ranges(byte_ranges, complement).map(|ranges| {
Mode::Bytes(
@@ -416,7 +416,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Options {
out_delim: Some(
matches
.value_of(options::OUTPUT_DELIMITER)
.get_one::<String>(options::OUTPUT_DELIMITER)
.map(|s| s.as_str())
.unwrap_or_default()
.to_owned(),
),
@@ -430,7 +431,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Options {
out_delim: Some(
matches
.value_of(options::OUTPUT_DELIMITER)
.get_one::<String>(options::OUTPUT_DELIMITER)
.map(|s| s.as_str())
.unwrap_or_default()
.to_owned(),
),
@@ -440,7 +442,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}),
(None, None, Some(field_ranges)) => {
list_to_ranges(field_ranges, complement).and_then(|ranges| {
let out_delim = match matches.value_of(options::OUTPUT_DELIMITER) {
let out_delim = match matches.get_one::<String>(options::OUTPUT_DELIMITER) {
Some(s) => {
if s.is_empty() {
Some("\0".to_owned())
@@ -454,7 +456,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let only_delimited = matches.contains_id(options::ONLY_DELIMITED);
let zero_terminated = matches.contains_id(options::ZERO_TERMINATED);
match matches.value_of(options::DELIMITER) {
match matches.get_one::<String>(options::DELIMITER).map(|s| s.as_str()) {
Some(mut delim) => {
// GNU's `cut` supports `-d=` to set the delimiter to `=`.
// Clap parsing is limited in this situation, see:
+8 -5
View File
@@ -146,7 +146,7 @@ impl<'a> From<&'a str> for Rfc3339Format {
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
let format = if let Some(form) = matches.value_of(OPT_FORMAT) {
let format = if let Some(form) = matches.get_one::<String>(OPT_FORMAT) {
if !form.starts_with('+') {
return Err(USimpleError::new(
1,
@@ -162,21 +162,24 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Format::Iso8601(fmt)
} else if matches.contains_id(OPT_RFC_EMAIL) {
Format::Rfc5322
} else if let Some(fmt) = matches.value_of(OPT_RFC_3339).map(Into::into) {
} else if let Some(fmt) = matches
.get_one::<String>(OPT_RFC_3339)
.map(|s| s.as_str().into())
{
Format::Rfc3339(fmt)
} else {
Format::Default
};
let date_source = if let Some(date) = matches.value_of(OPT_DATE) {
let date_source = if let Some(date) = matches.get_one::<String>(OPT_DATE) {
DateSource::Custom(date.into())
} else if let Some(file) = matches.value_of(OPT_FILE) {
} else if let Some(file) = matches.get_one::<String>(OPT_FILE) {
DateSource::File(file.into())
} else {
DateSource::Now
};
let set_to = match matches.value_of(OPT_SET).map(parse_date) {
let set_to = match matches.get_one::<String>(OPT_SET).map(parse_date) {
None => None,
Some(Err((input, _err))) => {
return Err(USimpleError::new(
+1 -1
View File
@@ -164,7 +164,7 @@ impl Default for BlockSize {
pub(crate) fn read_block_size(matches: &ArgMatches) -> Result<BlockSize, ParseSizeError> {
if matches.contains_id(OPT_BLOCKSIZE) {
let s = matches.value_of(OPT_BLOCKSIZE).unwrap();
let s = matches.get_one::<String>(OPT_BLOCKSIZE).unwrap();
let bytes = parse_size(s)?;
if bytes > 0 {
+4 -1
View File
@@ -188,7 +188,10 @@ impl Options {
block_size: read_block_size(matches).map_err(|e| match e {
ParseSizeError::InvalidSuffix(s) => OptionsError::InvalidSuffix(s),
ParseSizeError::SizeTooBig(_) => OptionsError::BlockSizeTooLarge(
matches.value_of(OPT_BLOCKSIZE).unwrap().to_string(),
matches
.get_one::<String>(OPT_BLOCKSIZE)
.unwrap()
.to_string(),
),
ParseSizeError::ParseFailure(s) => OptionsError::InvalidBlockSize(s),
})?,
+17 -7
View File
@@ -521,7 +521,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let summarize = matches.contains_id(options::SUMMARIZE);
let max_depth = parse_depth(matches.value_of(options::MAX_DEPTH), summarize)?;
let max_depth = parse_depth(
matches
.get_one::<String>(options::MAX_DEPTH)
.map(|s| s.as_str()),
summarize,
)?;
let options = Options {
all: matches.contains_id(options::ALL),
@@ -534,7 +539,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
verbose: matches.contains_id(options::VERBOSE),
};
let files = match matches.value_of(options::FILE) {
let files = match matches.get_one::<String>(options::FILE) {
Some(_) => matches
.get_many::<String>(options::FILE)
.unwrap()
@@ -549,9 +554,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
show_warning!("options --apparent-size and -b are ineffective with --inodes");
}
let block_size = read_block_size(matches.value_of(options::BLOCK_SIZE));
let block_size = read_block_size(
matches
.get_one::<String>(options::BLOCK_SIZE)
.map(|s| s.as_str()),
);
let threshold = matches.value_of(options::THRESHOLD).map(|s| {
let threshold = matches.get_one::<String>(options::THRESHOLD).map(|s| {
Threshold::from_str(s)
.unwrap_or_else(|e| crash!(1, "{}", format_error_message(&e, s, options::THRESHOLD)))
});
@@ -582,7 +591,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
};
let time_format_str = parse_time_style(matches.value_of("time-style"))?;
let time_format_str =
parse_time_style(matches.get_one::<String>("time-style").map(|s| s.as_str()))?;
let line_separator = if matches.contains_id(options::NULL) {
"\0"
@@ -630,8 +640,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if matches.is_present(options::TIME) {
let tm = {
let secs = {
match matches.value_of(options::TIME) {
Some(s) => match s {
match matches.get_one::<String>(options::TIME) {
Some(s) => match s.as_str() {
"ctime" | "status" => stat.modified,
"access" | "atime" | "use" => stat.accessed,
"birth" | "creation" => stat
+1 -1
View File
@@ -177,7 +177,7 @@ fn run_env(args: impl uucore::Args) -> UResult<()> {
let ignore_env = matches.contains_id("ignore-environment");
let null = matches.contains_id("null");
let running_directory = matches.value_of("chdir");
let running_directory = matches.get_one::<String>("chdir").map(|s| s.as_str());
let files = match matches.get_many::<String>("file") {
Some(v) => v.map(|s| s.as_str()).collect(),
None => Vec::with_capacity(0),
+5 -5
View File
@@ -108,17 +108,17 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
fmt_opts.xprefix = matches.contains_id(OPT_EXACT_PREFIX);
fmt_opts.xanti_prefix = matches.contains_id(OPT_SKIP_PREFIX);
if let Some(s) = matches.value_of(OPT_PREFIX).map(String::from) {
if let Some(s) = matches.get_one::<String>(OPT_PREFIX).map(String::from) {
fmt_opts.prefix = s;
fmt_opts.use_prefix = true;
};
if let Some(s) = matches.value_of(OPT_SKIP_PREFIX).map(String::from) {
if let Some(s) = matches.get_one::<String>(OPT_SKIP_PREFIX).map(String::from) {
fmt_opts.anti_prefix = s;
fmt_opts.use_anti_prefix = true;
};
if let Some(s) = matches.value_of(OPT_WIDTH) {
if let Some(s) = matches.get_one::<String>(OPT_WIDTH) {
fmt_opts.width = match s.parse::<usize>() {
Ok(t) => t,
Err(e) => {
@@ -140,7 +140,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
fmt_opts.goal = cmp::min(fmt_opts.width * 94 / 100, fmt_opts.width - 3);
};
if let Some(s) = matches.value_of(OPT_GOAL) {
if let Some(s) = matches.get_one::<String>(OPT_GOAL) {
fmt_opts.goal = match s.parse::<usize>() {
Ok(t) => t,
Err(e) => {
@@ -157,7 +157,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
};
if let Some(s) = matches.value_of(OPT_TAB_WIDTH) {
if let Some(s) = matches.get_one::<String>(OPT_TAB_WIDTH) {
fmt_opts.tabwidth = match s.parse::<usize>() {
Ok(t) => t,
Err(e) => {
+1 -1
View File
@@ -38,7 +38,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let bytes = matches.contains_id(options::BYTES);
let spaces = matches.contains_id(options::SPACES);
let poss_width = match matches.value_of(options::WIDTH) {
let poss_width = match matches.get_one::<String>(options::WIDTH) {
Some(v) => Some(v.to_owned()),
None => obs_width,
};
+6 -6
View File
@@ -80,7 +80,7 @@ fn detect_algo(
Box::new(blake3::Hasher::new()) as Box<dyn Digest>,
256,
),
"sha3sum" => match matches.value_of("bits") {
"sha3sum" => match matches.get_one::<String>("bits") {
Some(bits_str) => match (bits_str).parse::<usize>() {
Ok(224) => (
"SHA3-224",
@@ -130,7 +130,7 @@ fn detect_algo(
Box::new(Sha3_512::new()) as Box<dyn Digest>,
512,
),
"shake128sum" => match matches.value_of("bits") {
"shake128sum" => match matches.get_one::<String>("bits") {
Some(bits_str) => match (bits_str).parse::<usize>() {
Ok(bits) => (
"SHAKE128",
@@ -141,7 +141,7 @@ fn detect_algo(
},
None => crash!(1, "--bits required for SHAKE-128"),
},
"shake256sum" => match matches.value_of("bits") {
"shake256sum" => match matches.get_one::<String>("bits") {
Some(bits_str) => match (bits_str).parse::<usize>() {
Ok(bits) => (
"SHAKE256",
@@ -187,7 +187,7 @@ fn detect_algo(
set_or_crash("BLAKE3", Box::new(blake3::Hasher::new()), 256);
}
if matches.contains_id("sha3") {
match matches.value_of("bits") {
match matches.get_one::<String>("bits") {
Some(bits_str) => match (bits_str).parse::<usize>() {
Ok(224) => set_or_crash(
"SHA3-224",
@@ -231,7 +231,7 @@ fn detect_algo(
set_or_crash("SHA3-512", Box::new(Sha3_512::new()), 512);
}
if matches.contains_id("shake128") {
match matches.value_of("bits") {
match matches.get_one::<String>("bits") {
Some(bits_str) => match (bits_str).parse::<usize>() {
Ok(bits) => set_or_crash("SHAKE128", Box::new(Shake128::new()), bits),
Err(err) => crash!(1, "{}", err),
@@ -240,7 +240,7 @@ fn detect_algo(
}
}
if matches.contains_id("shake256") {
match matches.value_of("bits") {
match matches.get_one::<String>("bits") {
Some(bits_str) => match (bits_str).parse::<usize>() {
Ok(bits) => set_or_crash("SHAKE256", Box::new(Shake256::new()), bits),
Err(err) => crash!(1, "{}", err),
+2 -2
View File
@@ -130,7 +130,7 @@ impl Default for Mode {
impl Mode {
fn from(matches: &ArgMatches) -> Result<Self, String> {
if let Some(v) = matches.value_of(options::BYTES_NAME) {
if let Some(v) = matches.get_one::<String>(options::BYTES_NAME) {
let (n, all_but_last) =
parse::parse_num(v).map_err(|err| format!("invalid number of bytes: {}", err))?;
if all_but_last {
@@ -138,7 +138,7 @@ impl Mode {
} else {
Ok(Self::FirstBytes(n))
}
} else if let Some(v) = matches.value_of(options::LINES_NAME) {
} else if let Some(v) = matches.get_one::<String>(options::LINES_NAME) {
let (n, all_but_last) =
parse::parse_num(v).map_err(|err| format!("invalid number of lines: {}", err))?;
if all_but_last {

Some files were not shown because too many files have changed in this diff Show More