diff --git a/Cargo.lock b/Cargo.lock index 64254d4e9..3497f177c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2267,7 +2267,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..4b4cdbeeb 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 = "Display 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 }