1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

base{32, 64, enc}: update to clap 4

This commit is contained in:
Terts Diepraam 2022-09-29 15:21:29 +02:00
parent cb8831af71
commit 26309dc9d7
8 changed files with 32 additions and 21 deletions

View file

@ -167,7 +167,7 @@ fn gen_completions<T: uucore::Args>(
process::exit(0); process::exit(0);
} }
fn gen_coreutils_app<T: uucore::Args>(util_map: &UtilityMap<T>) -> Command<'static> { fn gen_coreutils_app<T: uucore::Args>(util_map: &UtilityMap<T>) -> Command {
let mut command = Command::new("coreutils"); let mut command = Command::new("coreutils");
for (_, (_, sub_app)) in util_map { for (_, (_, sub_app)) in util_map {
command = command.subcommand(sub_app()); command = command.subcommand(sub_app());

View file

@ -15,7 +15,7 @@ edition = "2021"
path = "src/base32.rs" path = "src/base32.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 = ["encoding"] } uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features = ["encoding"] }
[[bin]] [[bin]]

View file

@ -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) base_common::base_app(ABOUT, USAGE)
} }

View file

@ -18,7 +18,7 @@ use std::fs::File;
use std::io::{BufReader, Stdin}; use std::io::{BufReader, Stdin};
use std::path::Path; 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; pub static BASE_CMD_PARSE_ERROR: i32 = 1;
@ -77,21 +77,25 @@ impl Config {
.transpose()?; .transpose()?;
Ok(Self { Ok(Self {
decode: options.contains_id(options::DECODE), decode: options.get_flag(options::DECODE),
ignore_garbage: options.contains_id(options::IGNORE_GARBAGE), ignore_garbage: options.get_flag(options::IGNORE_GARBAGE),
wrap_cols: cols, wrap_cols: cols,
to_read: file, to_read: file,
}) })
} }
} }
pub fn parse_base_cmd_args(args: impl uucore::Args, about: &str, usage: &str) -> UResult<Config> { pub fn parse_base_cmd_args(
args: impl uucore::Args,
about: &'static str,
usage: &str,
) -> UResult<Config> {
let command = base_app(about, usage); let command = base_app(about, usage);
let arg_list = args.collect_lossy(); let arg_list = args.collect_lossy();
Config::from(&command.try_get_matches_from(arg_list)?) 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()) Command::new(uucore::util_name())
.version(crate_version!()) .version(crate_version!())
.about(about) .about(about)
@ -102,19 +106,21 @@ pub fn base_app<'a>(about: &'a str, usage: &'a str) -> Command<'a> {
Arg::new(options::DECODE) Arg::new(options::DECODE)
.short('d') .short('d')
.long(options::DECODE) .long(options::DECODE)
.help("decode data"), .help("decode data")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::IGNORE_GARBAGE) Arg::new(options::IGNORE_GARBAGE)
.short('i') .short('i')
.long(options::IGNORE_GARBAGE) .long(options::IGNORE_GARBAGE)
.help("when decoding, ignore non-alphabetic characters"), .help("when decoding, ignore non-alphabetic characters")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::WRAP) Arg::new(options::WRAP)
.short('w') .short('w')
.long(options::WRAP) .long(options::WRAP)
.takes_value(true) .value_name("COLS")
.help( .help(
"wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", "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(
Arg::new(options::FILE) Arg::new(options::FILE)
.index(1) .index(1)
.multiple_occurrences(true) .action(clap::ArgAction::Append)
.value_hint(clap::ValueHint::FilePath), .value_hint(clap::ValueHint::FilePath),
) )
} }

View file

@ -15,7 +15,7 @@ edition = "2021"
path = "src/basenc.rs" path = "src/basenc.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 = ["encoding"] } uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features = ["encoding"] }
uu_base32 = { version=">=0.0.16", package="uu_base32", path="../base32"} uu_base32 = { version=">=0.0.16", package="uu_base32", path="../base32"}

View file

@ -8,7 +8,7 @@
//spell-checker:ignore (args) lsbf msbf //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 uu_base32::base_common::{self, Config, BASE_CMD_PARSE_ERROR};
use uucore::{ use uucore::{
@ -41,10 +41,14 @@ const ENCODINGS: &[(&str, Format)] = &[
const USAGE: &str = "{} [OPTION]... [FILE]"; 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); let mut command = base_common::base_app(ABOUT, USAGE);
for encoding in ENCODINGS { 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 command
} }
@ -55,7 +59,7 @@ fn parse_cmd_args(args: impl uucore::Args) -> UResult<(Config, Format)> {
.with_exit_code(1)?; .with_exit_code(1)?;
let format = ENCODINGS let format = ENCODINGS
.iter() .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"))? .ok_or_else(|| UUsageError::new(BASE_CMD_PARSE_ERROR, "missing encoding type"))?
.1; .1;
let config = Config::from(&matches)?; let config = Config::from(&matches)?;

View file

@ -86,12 +86,12 @@ fn test_wrap() {
fn test_wrap_no_arg() { fn test_wrap_no_arg() {
for wrap_param in ["-w", "--wrap"] { for wrap_param in ["-w", "--wrap"] {
let ts = TestScenario::new(util_name!()); let ts = TestScenario::new(util_name!());
let expected_stderr = "error: The argument '--wrap <wrap>\' requires a value but none was \ let expected_stderr = "The argument '--wrap <COLS>' requires a value but none was supplied";
supplied\n\nFor more information try --help";
ts.ucmd() ts.ucmd()
.arg(wrap_param) .arg(wrap_param)
.fails() .fails()
.stderr_only(expected_stderr); .stderr_contains(expected_stderr)
.no_stdout();
} }
} }

View file

@ -79,7 +79,8 @@ fn test_wrap_no_arg() {
new_ucmd!() new_ucmd!()
.arg(wrap_param) .arg(wrap_param)
.fails() .fails()
.stderr_contains("The argument '--wrap <wrap>' requires a value but none was supplied"); .stderr_contains("The argument '--wrap <COLS>' requires a value but none was supplied")
.no_stdout();
} }
} }