1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

Merge pull request #1620 from sylvestre/clap-printenv

refactor(printenv): use clap instead of getopts
This commit is contained in:
Sylvestre Ledru 2020-11-03 08:36:56 +01:00 committed by GitHub
commit bd41cb621b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 46 deletions

2
Cargo.lock generated
View file

@ -1904,7 +1904,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)",
] ]

View file

@ -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" }

View file

@ -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 = "Display the values of the specified environment VARIABLE(s), or (with no VARIABLE) display name and value pairs for them all.";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static OPT_NULL: &str = "null";
static ARG_VARIABLES: &str = "variables";
fn get_usage() -> String {
format!("{0} [VARIABLE]... [OPTION]...", executable!())
}
pub fn uumain(args: impl uucore::Args) -> i32 { pub fn uumain(args: impl uucore::Args) -> i32 {
let args = args.collect_str(); let usage = get_usage();
let mut opts = getopts::Options::new(); let matches = App::new(executable!())
opts.optflag( .version(VERSION)
"0", .about(ABOUT)
"null", .usage(&usage[..])
"end each output line with 0 byte rather than newline", .arg(
); Arg::with_name(OPT_NULL)
opts.optflag("h", "help", "display this help and exit"); .short("0")
opts.optflag("V", "version", "output version information and exit"); .long(OPT_NULL)
let matches = match opts.parse(&args[1..]) { .help("end each output line with 0 byte rather than newline"),
Ok(m) => m, )
Err(f) => crash!(1, "Invalid options\n{}", f), .arg(
}; Arg::with_name(ARG_VARIABLES)
if matches.opt_present("help") { .multiple(true)
let msg = format!( .takes_value(true)
"{0} {1} .min_values(1),
)
.get_matches_from(args);
Usage: let variables: Vec<String> = matches
{0} [VARIABLE]... [OPTION]... .values_of(ARG_VARIABLES)
.map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default();
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"; let mut separator = "\n";
if matches.opt_present("null") { if matches.is_present(OPT_NULL) {
separator = "\x00"; separator = "\x00";
};
exec(matches.free, separator);
0
} }
pub fn exec(args: Vec<String>, separator: &str) { if variables.is_empty() {
if args.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
} }