From b7c1216a1a2f1527d7cb450b9187cd9d9d1314ed Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Thu, 29 Sep 2022 17:48:27 +0200 Subject: [PATCH] env: update to clap 4 --- src/uu/env/Cargo.toml | 2 +- src/uu/env/src/env.rs | 128 +++++++++++++++++++------------------- tests/by-util/test_env.rs | 2 +- 3 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/uu/env/Cargo.toml b/src/uu/env/Cargo.toml index 66a4b7139..da9cdeab6 100644 --- a/src/uu/env/Cargo.toml +++ b/src/uu/env/Cargo.toml @@ -15,7 +15,7 @@ edition = "2021" path = "src/env.rs" [dependencies] -clap = { version = "3.2", features = ["wrap_help", "cargo"] } +clap = { version = "4.0", features = ["wrap_help", "cargo"] } rust-ini = "0.18.0" uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["signals"]} diff --git a/src/uu/env/src/env.rs b/src/uu/env/src/env.rs index aea1a2525..0e51cf748 100644 --- a/src/uu/env/src/env.rs +++ b/src/uu/env/src/env.rs @@ -16,7 +16,7 @@ extern crate clap; #[macro_use] extern crate uucore; -use clap::{Arg, Command}; +use clap::{Arg, ArgAction, Command}; use ini::Ini; #[cfg(unix)] use nix::sys::signal::{raise, sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal}; @@ -126,57 +126,69 @@ fn build_command<'a, 'b>(args: &'a mut Vec<&'b str>) -> (Cow<'b, str>, &'a [&'b (progname, &args[..]) } -pub fn uu_app<'a>() -> Command<'a> { +pub fn uu_app() -> Command { Command::new(crate_name!()) .version(crate_version!()) .about(ABOUT) .override_usage(format_usage(USAGE)) .after_help(AFTER_HELP) - .allow_external_subcommands(true) .infer_long_args(true) - .arg(Arg::new("ignore-environment") - .short('i') - .long("ignore-environment") - .help("start with an empty environment")) - .arg(Arg::new("chdir") - .short('C') // GNU env compatibility - .long("chdir") - .takes_value(true) - .number_of_values(1) - .value_name("DIR") - .value_hint(clap::ValueHint::DirPath) - .help("change working directory to DIR")) - .arg(Arg::new("null") - .short('0') - .long("null") - .help("end each output line with a 0 byte rather than a newline (only valid when \ - printing the environment)")) - .arg(Arg::new("file") - .short('f') - .long("file") - .takes_value(true) - .number_of_values(1) - .value_name("PATH") - .value_hint(clap::ValueHint::FilePath) - .multiple_occurrences(true) - .help("read and set variables from a \".env\"-style configuration file (prior to any \ - unset and/or set)")) - .arg(Arg::new("unset") - .short('u') - .long("unset") - .takes_value(true) - .number_of_values(1) - .value_name("NAME") - .multiple_occurrences(true) - .help("remove variable from the environment")) + .trailing_var_arg(true) + .arg( + Arg::new("ignore-environment") + .short('i') + .long("ignore-environment") + .help("start with an empty environment") + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new("chdir") + .short('C') // GNU env compatibility + .long("chdir") + .number_of_values(1) + .value_name("DIR") + .value_hint(clap::ValueHint::DirPath) + .help("change working directory to DIR"), + ) + .arg( + Arg::new("null") + .short('0') + .long("null") + .help( + "end each output line with a 0 byte rather than a newline (only \ + valid when printing the environment)", + ) + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new("file") + .short('f') + .long("file") + .value_name("PATH") + .value_hint(clap::ValueHint::FilePath) + .action(ArgAction::Append) + .help( + "read and set variables from a \".env\"-style configuration file \ + (prior to any unset and/or set)", + ), + ) + .arg( + Arg::new("unset") + .short('u') + .long("unset") + .value_name("NAME") + .action(ArgAction::Append) + .help("remove variable from the environment"), + ) + .arg(Arg::new("vars").action(ArgAction::Append)) } fn run_env(args: impl uucore::Args) -> UResult<()> { let app = uu_app(); let matches = app.try_get_matches_from(args).with_exit_code(125)?; - let ignore_env = matches.contains_id("ignore-environment"); - let null = matches.contains_id("null"); + let ignore_env = matches.get_flag("ignore-environment"); + let null = matches.get_flag("null"); let running_directory = matches.get_one::("chdir").map(|s| s.as_str()); let files = match matches.get_many::("file") { Some(v) => v.map(|s| s.as_str()).collect(), @@ -210,32 +222,20 @@ fn run_env(args: impl uucore::Args) -> UResult<()> { }; } - // we handle the name, value pairs and the program to be executed by treating them as external - // subcommands in clap - if let Some((external, matches)) = matches.subcommand() { - let mut begin_prog_opts = false; - - if external == "-" { - // "-" implies -i and stop parsing opts - opts.ignore_env = true; - } else { - begin_prog_opts = parse_name_value_opt(&mut opts, external)?; + let mut begin_prog_opts = false; + if let Some(mut iter) = matches.get_many::("vars") { + // read NAME=VALUE arguments (and up to a single program argument) + while !begin_prog_opts { + if let Some(opt) = iter.next() { + begin_prog_opts = parse_name_value_opt(&mut opts, opt)?; + } else { + break; + } } - if let Some(mut iter) = matches.get_many::("") { - // read NAME=VALUE arguments (and up to a single program argument) - while !begin_prog_opts { - if let Some(opt) = iter.next() { - begin_prog_opts = parse_name_value_opt(&mut opts, opt)?; - } else { - break; - } - } - - // read any leftover program arguments - for opt in iter { - parse_program_opt(&mut opts, opt)?; - } + // read any leftover program arguments + for opt in iter { + parse_program_opt(&mut opts, opt)?; } } diff --git a/tests/by-util/test_env.rs b/tests/by-util/test_env.rs index 6e29cbf79..74bb805a7 100644 --- a/tests/by-util/test_env.rs +++ b/tests/by-util/test_env.rs @@ -16,7 +16,7 @@ fn test_env_help() { .arg("--help") .succeeds() .no_stderr() - .stdout_contains("OPTIONS:"); + .stdout_contains("Options:"); } #[test]