mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
env: update to clap 4
This commit is contained in:
parent
b007318b51
commit
b7c1216a1a
3 changed files with 66 additions and 66 deletions
2
src/uu/env/Cargo.toml
vendored
2
src/uu/env/Cargo.toml
vendored
|
@ -15,7 +15,7 @@ edition = "2021"
|
||||||
path = "src/env.rs"
|
path = "src/env.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "3.2", features = ["wrap_help", "cargo"] }
|
clap = { version = "4.0", features = ["wrap_help", "cargo"] }
|
||||||
rust-ini = "0.18.0"
|
rust-ini = "0.18.0"
|
||||||
uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["signals"]}
|
uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["signals"]}
|
||||||
|
|
||||||
|
|
128
src/uu/env/src/env.rs
vendored
128
src/uu/env/src/env.rs
vendored
|
@ -16,7 +16,7 @@ extern crate clap;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
use clap::{Arg, Command};
|
use clap::{Arg, ArgAction, Command};
|
||||||
use ini::Ini;
|
use ini::Ini;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use nix::sys::signal::{raise, sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal};
|
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[..])
|
(progname, &args[..])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app<'a>() -> Command<'a> {
|
pub fn uu_app() -> Command {
|
||||||
Command::new(crate_name!())
|
Command::new(crate_name!())
|
||||||
.version(crate_version!())
|
.version(crate_version!())
|
||||||
.about(ABOUT)
|
.about(ABOUT)
|
||||||
.override_usage(format_usage(USAGE))
|
.override_usage(format_usage(USAGE))
|
||||||
.after_help(AFTER_HELP)
|
.after_help(AFTER_HELP)
|
||||||
.allow_external_subcommands(true)
|
|
||||||
.infer_long_args(true)
|
.infer_long_args(true)
|
||||||
.arg(Arg::new("ignore-environment")
|
.trailing_var_arg(true)
|
||||||
.short('i')
|
.arg(
|
||||||
.long("ignore-environment")
|
Arg::new("ignore-environment")
|
||||||
.help("start with an empty environment"))
|
.short('i')
|
||||||
.arg(Arg::new("chdir")
|
.long("ignore-environment")
|
||||||
.short('C') // GNU env compatibility
|
.help("start with an empty environment")
|
||||||
.long("chdir")
|
.action(ArgAction::SetTrue),
|
||||||
.takes_value(true)
|
)
|
||||||
.number_of_values(1)
|
.arg(
|
||||||
.value_name("DIR")
|
Arg::new("chdir")
|
||||||
.value_hint(clap::ValueHint::DirPath)
|
.short('C') // GNU env compatibility
|
||||||
.help("change working directory to DIR"))
|
.long("chdir")
|
||||||
.arg(Arg::new("null")
|
.number_of_values(1)
|
||||||
.short('0')
|
.value_name("DIR")
|
||||||
.long("null")
|
.value_hint(clap::ValueHint::DirPath)
|
||||||
.help("end each output line with a 0 byte rather than a newline (only valid when \
|
.help("change working directory to DIR"),
|
||||||
printing the environment)"))
|
)
|
||||||
.arg(Arg::new("file")
|
.arg(
|
||||||
.short('f')
|
Arg::new("null")
|
||||||
.long("file")
|
.short('0')
|
||||||
.takes_value(true)
|
.long("null")
|
||||||
.number_of_values(1)
|
.help(
|
||||||
.value_name("PATH")
|
"end each output line with a 0 byte rather than a newline (only \
|
||||||
.value_hint(clap::ValueHint::FilePath)
|
valid when printing the environment)",
|
||||||
.multiple_occurrences(true)
|
)
|
||||||
.help("read and set variables from a \".env\"-style configuration file (prior to any \
|
.action(ArgAction::SetTrue),
|
||||||
unset and/or set)"))
|
)
|
||||||
.arg(Arg::new("unset")
|
.arg(
|
||||||
.short('u')
|
Arg::new("file")
|
||||||
.long("unset")
|
.short('f')
|
||||||
.takes_value(true)
|
.long("file")
|
||||||
.number_of_values(1)
|
.value_name("PATH")
|
||||||
.value_name("NAME")
|
.value_hint(clap::ValueHint::FilePath)
|
||||||
.multiple_occurrences(true)
|
.action(ArgAction::Append)
|
||||||
.help("remove variable from the environment"))
|
.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<()> {
|
fn run_env(args: impl uucore::Args) -> UResult<()> {
|
||||||
let app = uu_app();
|
let app = uu_app();
|
||||||
let matches = app.try_get_matches_from(args).with_exit_code(125)?;
|
let matches = app.try_get_matches_from(args).with_exit_code(125)?;
|
||||||
|
|
||||||
let ignore_env = matches.contains_id("ignore-environment");
|
let ignore_env = matches.get_flag("ignore-environment");
|
||||||
let null = matches.contains_id("null");
|
let null = matches.get_flag("null");
|
||||||
let running_directory = matches.get_one::<String>("chdir").map(|s| s.as_str());
|
let running_directory = matches.get_one::<String>("chdir").map(|s| s.as_str());
|
||||||
let files = match matches.get_many::<String>("file") {
|
let files = match matches.get_many::<String>("file") {
|
||||||
Some(v) => v.map(|s| s.as_str()).collect(),
|
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
|
let mut begin_prog_opts = false;
|
||||||
// subcommands in clap
|
if let Some(mut iter) = matches.get_many::<String>("vars") {
|
||||||
if let Some((external, matches)) = matches.subcommand() {
|
// read NAME=VALUE arguments (and up to a single program argument)
|
||||||
let mut begin_prog_opts = false;
|
while !begin_prog_opts {
|
||||||
|
if let Some(opt) = iter.next() {
|
||||||
if external == "-" {
|
begin_prog_opts = parse_name_value_opt(&mut opts, opt)?;
|
||||||
// "-" implies -i and stop parsing opts
|
} else {
|
||||||
opts.ignore_env = true;
|
break;
|
||||||
} else {
|
}
|
||||||
begin_prog_opts = parse_name_value_opt(&mut opts, external)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(mut iter) = matches.get_many::<String>("") {
|
// read any leftover program arguments
|
||||||
// read NAME=VALUE arguments (and up to a single program argument)
|
for opt in iter {
|
||||||
while !begin_prog_opts {
|
parse_program_opt(&mut opts, opt)?;
|
||||||
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)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ fn test_env_help() {
|
||||||
.arg("--help")
|
.arg("--help")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.no_stderr()
|
.no_stderr()
|
||||||
.stdout_contains("OPTIONS:");
|
.stdout_contains("Options:");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue