From 26309dc9d740582b3f3555f8d75bc380eeb0dd66 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Thu, 29 Sep 2022 15:21:29 +0200 Subject: [PATCH] base{32, 64, enc}: update to clap 4 --- src/bin/coreutils.rs | 2 +- src/uu/base32/Cargo.toml | 2 +- src/uu/base32/src/base32.rs | 2 +- src/uu/base32/src/base_common.rs | 24 +++++++++++++++--------- src/uu/basenc/Cargo.toml | 2 +- src/uu/basenc/src/basenc.rs | 12 ++++++++---- tests/by-util/test_base32.rs | 6 +++--- tests/by-util/test_base64.rs | 3 ++- 8 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index c175d9447..74c6f4cdb 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -167,7 +167,7 @@ fn gen_completions( process::exit(0); } -fn gen_coreutils_app(util_map: &UtilityMap) -> Command<'static> { +fn gen_coreutils_app(util_map: &UtilityMap) -> Command { let mut command = Command::new("coreutils"); for (_, (_, sub_app)) in util_map { command = command.subcommand(sub_app()); diff --git a/src/uu/base32/Cargo.toml b/src/uu/base32/Cargo.toml index bb72325bb..a996fa811 100644 --- a/src/uu/base32/Cargo.toml +++ b/src/uu/base32/Cargo.toml @@ -15,7 +15,7 @@ edition = "2021" path = "src/base32.rs" [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 = ["encoding"] } [[bin]] diff --git a/src/uu/base32/src/base32.rs b/src/uu/base32/src/base32.rs index 2165e0dcc..01932e675 100644 --- a/src/uu/base32/src/base32.rs +++ b/src/uu/base32/src/base32.rs @@ -35,6 +35,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { ) } -pub fn uu_app<'a>() -> Command<'a> { +pub fn uu_app() -> Command { base_common::base_app(ABOUT, USAGE) } diff --git a/src/uu/base32/src/base_common.rs b/src/uu/base32/src/base_common.rs index 6cceb50f4..f68aa6b86 100644 --- a/src/uu/base32/src/base_common.rs +++ b/src/uu/base32/src/base_common.rs @@ -18,7 +18,7 @@ use std::fs::File; use std::io::{BufReader, Stdin}; use std::path::Path; -use clap::{crate_version, Arg, Command}; +use clap::{crate_version, Arg, ArgAction, Command}; pub static BASE_CMD_PARSE_ERROR: i32 = 1; @@ -77,21 +77,25 @@ impl Config { .transpose()?; Ok(Self { - decode: options.contains_id(options::DECODE), - ignore_garbage: options.contains_id(options::IGNORE_GARBAGE), + decode: options.get_flag(options::DECODE), + ignore_garbage: options.get_flag(options::IGNORE_GARBAGE), wrap_cols: cols, to_read: file, }) } } -pub fn parse_base_cmd_args(args: impl uucore::Args, about: &str, usage: &str) -> UResult { +pub fn parse_base_cmd_args( + args: impl uucore::Args, + about: &'static str, + usage: &str, +) -> UResult { let command = base_app(about, usage); let arg_list = args.collect_lossy(); Config::from(&command.try_get_matches_from(arg_list)?) } -pub fn base_app<'a>(about: &'a str, usage: &'a str) -> Command<'a> { +pub fn base_app(about: &'static str, usage: &str) -> Command { Command::new(uucore::util_name()) .version(crate_version!()) .about(about) @@ -102,19 +106,21 @@ pub fn base_app<'a>(about: &'a str, usage: &'a str) -> Command<'a> { Arg::new(options::DECODE) .short('d') .long(options::DECODE) - .help("decode data"), + .help("decode data") + .action(ArgAction::SetTrue), ) .arg( Arg::new(options::IGNORE_GARBAGE) .short('i') .long(options::IGNORE_GARBAGE) - .help("when decoding, ignore non-alphabetic characters"), + .help("when decoding, ignore non-alphabetic characters") + .action(ArgAction::SetTrue), ) .arg( Arg::new(options::WRAP) .short('w') .long(options::WRAP) - .takes_value(true) + .value_name("COLS") .help( "wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", ), @@ -124,7 +130,7 @@ pub fn base_app<'a>(about: &'a str, usage: &'a str) -> Command<'a> { .arg( Arg::new(options::FILE) .index(1) - .multiple_occurrences(true) + .action(clap::ArgAction::Append) .value_hint(clap::ValueHint::FilePath), ) } diff --git a/src/uu/basenc/Cargo.toml b/src/uu/basenc/Cargo.toml index 713d84e76..ac22c84b7 100644 --- a/src/uu/basenc/Cargo.toml +++ b/src/uu/basenc/Cargo.toml @@ -15,7 +15,7 @@ edition = "2021" path = "src/basenc.rs" [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 = ["encoding"] } uu_base32 = { version=">=0.0.16", package="uu_base32", path="../base32"} diff --git a/src/uu/basenc/src/basenc.rs b/src/uu/basenc/src/basenc.rs index d884c04e3..cb9e38879 100644 --- a/src/uu/basenc/src/basenc.rs +++ b/src/uu/basenc/src/basenc.rs @@ -8,7 +8,7 @@ //spell-checker:ignore (args) lsbf msbf -use clap::{Arg, Command}; +use clap::{Arg, ArgAction, Command}; use uu_base32::base_common::{self, Config, BASE_CMD_PARSE_ERROR}; use uucore::{ @@ -41,10 +41,14 @@ const ENCODINGS: &[(&str, Format)] = &[ const USAGE: &str = "{} [OPTION]... [FILE]"; -pub fn uu_app<'a>() -> Command<'a> { +pub fn uu_app() -> Command { let mut command = base_common::base_app(ABOUT, USAGE); for encoding in ENCODINGS { - command = command.arg(Arg::new(encoding.0).long(encoding.0)); + command = command.arg( + Arg::new(encoding.0) + .long(encoding.0) + .action(ArgAction::SetTrue), + ); } command } @@ -55,7 +59,7 @@ fn parse_cmd_args(args: impl uucore::Args) -> UResult<(Config, Format)> { .with_exit_code(1)?; let format = ENCODINGS .iter() - .find(|encoding| matches.contains_id(encoding.0)) + .find(|encoding| matches.get_flag(encoding.0)) .ok_or_else(|| UUsageError::new(BASE_CMD_PARSE_ERROR, "missing encoding type"))? .1; let config = Config::from(&matches)?; diff --git a/tests/by-util/test_base32.rs b/tests/by-util/test_base32.rs index 1f46b7c47..bb51cda57 100644 --- a/tests/by-util/test_base32.rs +++ b/tests/by-util/test_base32.rs @@ -86,12 +86,12 @@ fn test_wrap() { fn test_wrap_no_arg() { for wrap_param in ["-w", "--wrap"] { let ts = TestScenario::new(util_name!()); - let expected_stderr = "error: The argument '--wrap \' requires a value but none was \ - supplied\n\nFor more information try --help"; + let expected_stderr = "The argument '--wrap ' requires a value but none was supplied"; ts.ucmd() .arg(wrap_param) .fails() - .stderr_only(expected_stderr); + .stderr_contains(expected_stderr) + .no_stdout(); } } diff --git a/tests/by-util/test_base64.rs b/tests/by-util/test_base64.rs index bbb0da5ad..a5478f6b7 100644 --- a/tests/by-util/test_base64.rs +++ b/tests/by-util/test_base64.rs @@ -79,7 +79,8 @@ fn test_wrap_no_arg() { new_ucmd!() .arg(wrap_param) .fails() - .stderr_contains("The argument '--wrap ' requires a value but none was supplied"); + .stderr_contains("The argument '--wrap ' requires a value but none was supplied") + .no_stdout(); } }