From 4804e52c979ab5499a0bae7c77fd2afc6dde51f5 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 25 Oct 2020 15:26:10 +0100 Subject: [PATCH 1/3] refactor(users): move to clap and simplify the code a bit --- Cargo.lock | 2 +- src/uu/users/Cargo.toml | 2 +- src/uu/users/src/users.rs | 63 ++++++++++++++++----------------------- 3 files changed, 27 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a24e046a..1ae0bcd4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2326,7 +2326,7 @@ dependencies = [ name = "uu_users" version = "0.0.1" 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_procs 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)", ] diff --git a/src/uu/users/Cargo.toml b/src/uu/users/Cargo.toml index e232839b3..ce9f9d212 100644 --- a/src/uu/users/Cargo.toml +++ b/src/uu/users/Cargo.toml @@ -15,7 +15,7 @@ edition = "2018" path = "src/users.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary", features=["utmpx"] } uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" } diff --git a/src/uu/users/src/users.rs b/src/uu/users/src/users.rs index 850b3112c..446d1d753 100644 --- a/src/uu/users/src/users.rs +++ b/src/uu/users/src/users.rs @@ -10,59 +10,44 @@ // Allow dead code here in order to keep all fields, constants here, for consistency. #![allow(dead_code)] -extern crate getopts; +extern crate clap; +#[macro_use] extern crate uucore; use uucore::utmpx::*; -use getopts::Options; +use clap::{App, Arg}; -static NAME: &str = "users"; +static ABOUT: &str = "Output who is currently logged in according to FILE."; static VERSION: &str = env!("CARGO_PKG_VERSION"); +static ARG_FILES: &str = "files"; + +fn get_usage() -> String { + format!("{0} [FILE]...", executable!()) +} + pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let usage = get_usage(); - let mut opts = Options::new(); + let matches = App::new(executable!()) + .version(VERSION) + .about(ABOUT) + .usage(&usage[..]) + .arg(Arg::with_name(ARG_FILES).takes_value(true).max_values(1)) + .get_matches_from(args); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); + let files: Vec = matches + .values_of(ARG_FILES) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => panic!("{}", f), - }; - - if matches.opt_present("help") { - println!("{} {}", NAME, VERSION); - println!(); - println!("Usage:"); - println!(" {} [OPTION]... [FILE]", NAME); - println!(); - println!( - "{}", - opts.usage("Output who is currently logged in according to FILE.") - ); - return 0; - } - - if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - return 0; - } - - let filename = if !matches.free.is_empty() { - matches.free[0].as_ref() + let filename = if !files.is_empty() { + files[0].as_ref() } else { DEFAULT_FILE }; - exec(filename); - - 0 -} - -fn exec(filename: &str) { let mut users = Utmpx::iter_all_records() .read_from(filename) .filter(Utmpx::is_user_process) @@ -73,4 +58,6 @@ fn exec(filename: &str) { users.sort(); println!("{}", users.join(" ")); } + + 0 } From bd339f142e756dee11dd4c7d50d67fc935477495 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 25 Oct 2020 17:07:17 +0100 Subject: [PATCH 2/3] Improve the "about" description Co-authored-by: Roy Ivy III --- src/uu/users/src/users.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/users/src/users.rs b/src/uu/users/src/users.rs index 446d1d753..cba83f778 100644 --- a/src/uu/users/src/users.rs +++ b/src/uu/users/src/users.rs @@ -18,7 +18,7 @@ use uucore::utmpx::*; use clap::{App, Arg}; -static ABOUT: &str = "Output who is currently logged in according to FILE."; +static ABOUT: &str = "Display who is currently logged in, according to FILE."; static VERSION: &str = env!("CARGO_PKG_VERSION"); static ARG_FILES: &str = "files"; From 7fb5aaa10831a622c71f222c475e6dc0ac2fb72e Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 25 Oct 2020 17:07:32 +0100 Subject: [PATCH 3/3] only one file is allowed Co-authored-by: Roy Ivy III --- src/uu/users/src/users.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/users/src/users.rs b/src/uu/users/src/users.rs index cba83f778..4b4cdbeeb 100644 --- a/src/uu/users/src/users.rs +++ b/src/uu/users/src/users.rs @@ -24,7 +24,7 @@ static VERSION: &str = env!("CARGO_PKG_VERSION"); static ARG_FILES: &str = "files"; fn get_usage() -> String { - format!("{0} [FILE]...", executable!()) + format!("{0} [FILE]", executable!()) } pub fn uumain(args: impl uucore::Args) -> i32 {