mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-31 04:57:45 +00:00
refactor(printenv): use clap instead of getopts
This commit is contained in:
parent
07e94064b0
commit
17a99f8e53
3 changed files with 50 additions and 52 deletions
14
Cargo.lock
generated
14
Cargo.lock
generated
|
@ -1202,7 +1202,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"syn 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)",
|
"syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1260,7 +1260,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.46"
|
version = "1.0.48"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -1966,7 +1966,7 @@ dependencies = [
|
||||||
name = "uu_printenv"
|
name = "uu_printenv"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
"clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"uucore 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)",
|
"uucore 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)",
|
||||||
"uucore_procs 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)",
|
"uucore_procs 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)",
|
||||||
]
|
]
|
||||||
|
@ -2398,7 +2398,7 @@ source = "git+https://github.com/uutils/uucore.git?branch=canary#869573459f00ba0
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"syn 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)",
|
"syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -2450,7 +2450,7 @@ dependencies = [
|
||||||
"log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"syn 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)",
|
"syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"wasm-bindgen-shared 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)",
|
"wasm-bindgen-shared 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -2470,7 +2470,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"syn 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)",
|
"syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"wasm-bindgen-backend 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)",
|
"wasm-bindgen-backend 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"wasm-bindgen-shared 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)",
|
"wasm-bindgen-shared 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
@ -2684,7 +2684,7 @@ dependencies = [
|
||||||
"checksum sha3 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26405905b6a56a94c60109cfda62610507ac14a65be531f5767dec5c5a8dd6a0"
|
"checksum sha3 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26405905b6a56a94c60109cfda62610507ac14a65be531f5767dec5c5a8dd6a0"
|
||||||
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||||
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
|
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
|
||||||
"checksum syn 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)" = "5ad5de3220ea04da322618ded2c42233d02baca219d6f160a3e9c87cda16c942"
|
"checksum syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)" = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac"
|
||||||
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
|
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
|
||||||
"checksum synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a761d12e6d8dcb4dcf952a7a89b475e3a9d69e4a69307e01a470977642914bd"
|
"checksum synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a761d12e6d8dcb4dcf952a7a89b475e3a9d69e4a69307e01a470977642914bd"
|
||||||
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
|
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
|
||||||
|
|
|
@ -15,7 +15,7 @@ edition = "2018"
|
||||||
path = "src/printenv.rs"
|
path = "src/printenv.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
getopts = "0.2.18"
|
clap = "2.33"
|
||||||
uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary" }
|
uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary" }
|
||||||
uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" }
|
uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" }
|
||||||
|
|
||||||
|
|
|
@ -7,69 +7,67 @@
|
||||||
|
|
||||||
/* last synced with: printenv (GNU coreutils) 8.13 */
|
/* last synced with: printenv (GNU coreutils) 8.13 */
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate clap;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
|
use clap::{App, Arg};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
static NAME: &str = "printenv";
|
static ABOUT: &str = "Prints the given environment VARIABLE(s), otherwise prints them all.";
|
||||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
static OPT_NULL: &str = "null";
|
||||||
let args = args.collect_str();
|
|
||||||
|
|
||||||
let mut opts = getopts::Options::new();
|
static ARG_VARIABLES: &str = "variables";
|
||||||
opts.optflag(
|
|
||||||
"0",
|
|
||||||
"null",
|
|
||||||
"end each output line with 0 byte rather than newline",
|
|
||||||
);
|
|
||||||
opts.optflag("h", "help", "display this help and exit");
|
|
||||||
opts.optflag("V", "version", "output version information and exit");
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
|
||||||
Ok(m) => m,
|
|
||||||
Err(f) => crash!(1, "Invalid options\n{}", f),
|
|
||||||
};
|
|
||||||
if matches.opt_present("help") {
|
|
||||||
let msg = format!(
|
|
||||||
"{0} {1}
|
|
||||||
|
|
||||||
Usage:
|
fn get_usage() -> String {
|
||||||
{0} [VARIABLE]... [OPTION]...
|
format!("{0} [VARIABLE]... [OPTION]...", executable!())
|
||||||
|
|
||||||
Prints the given environment VARIABLE(s), otherwise prints them all.",
|
|
||||||
NAME, VERSION
|
|
||||||
);
|
|
||||||
print!("{}", opts.usage(&msg));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if matches.opt_present("version") {
|
|
||||||
println!("{} {}", NAME, VERSION);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
let mut separator = "\n";
|
|
||||||
if matches.opt_present("null") {
|
|
||||||
separator = "\x00";
|
|
||||||
};
|
|
||||||
|
|
||||||
exec(matches.free, separator);
|
|
||||||
|
|
||||||
0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exec(args: Vec<String>, separator: &str) {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
if args.is_empty() {
|
let usage = get_usage();
|
||||||
|
|
||||||
|
let matches = App::new(executable!())
|
||||||
|
.version(VERSION)
|
||||||
|
.about(ABOUT)
|
||||||
|
.usage(&usage[..])
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(OPT_NULL)
|
||||||
|
.short("0")
|
||||||
|
.long(OPT_NULL)
|
||||||
|
.help("end each output line with 0 byte rather than newline"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(ARG_VARIABLES)
|
||||||
|
.multiple(true)
|
||||||
|
.takes_value(true)
|
||||||
|
.min_values(1),
|
||||||
|
)
|
||||||
|
.get_matches_from(args);
|
||||||
|
|
||||||
|
let variables: Vec<String> = matches
|
||||||
|
.values_of(ARG_VARIABLES)
|
||||||
|
.map(|v| v.map(ToString::to_string).collect())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let mut separator = "\n";
|
||||||
|
if matches.is_present(OPT_NULL) {
|
||||||
|
separator = "\x00";
|
||||||
|
}
|
||||||
|
|
||||||
|
if variables.is_empty() {
|
||||||
for (env_var, value) in env::vars() {
|
for (env_var, value) in env::vars() {
|
||||||
print!("{}={}{}", env_var, value, separator);
|
print!("{}={}{}", env_var, value, separator);
|
||||||
}
|
}
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for env_var in &args {
|
for env_var in variables {
|
||||||
if let Ok(var) = env::var(env_var) {
|
if let Ok(var) = env::var(env_var) {
|
||||||
print!("{}{}", var, separator);
|
print!("{}{}", var, separator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue