From 7b1554cc2cce69adba4035202bc486350d60d938 Mon Sep 17 00:00:00 2001 From: Alex Lyon Date: Mon, 12 Mar 2018 19:28:32 -0700 Subject: [PATCH] whoami: switch to clap --- src/whoami/Cargo.toml | 5 +++-- src/whoami/whoami.rs | 47 +++++++++++++++++-------------------------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/whoami/Cargo.toml b/src/whoami/Cargo.toml index 932df68ba..66f6e4219 100644 --- a/src/whoami/Cargo.toml +++ b/src/whoami/Cargo.toml @@ -2,6 +2,7 @@ name = "whoami" version = "0.0.1" authors = [] +description = "Print effective user ID." build = "../../mkmain.rs" [lib] @@ -9,14 +10,14 @@ name = "uu_whoami" path = "whoami.rs" [dependencies] -getopts = "0.2.14" +clap = "2.31" winapi = { version = "0.3", features = ["lmcons"] } advapi32-sys = "0.2.0" [dependencies.uucore] path = "../uucore" default-features = false -features = ["entries"] +features = ["entries", "wide"] [[bin]] name = "whoami" diff --git a/src/whoami/whoami.rs b/src/whoami/whoami.rs index 13bcc586a..097812706 100644 --- a/src/whoami/whoami.rs +++ b/src/whoami/whoami.rs @@ -11,45 +11,34 @@ /* last synced with: whoami (GNU coreutils) 8.21 */ -extern crate getopts; - +#[macro_use] +extern crate clap; #[macro_use] extern crate uucore; -use getopts::Options; - mod platform; -static NAME: &'static str = "whoami"; -static VERSION: &'static str = env!("CARGO_PKG_VERSION"); +// force a re-build whenever Cargo.toml changes +const _CARGO_TOML: &'static str = include_str!("Cargo.toml"); pub fn uumain(args: Vec) -> i32 { - let mut opts = Options::new(); + let app = app_from_crate!(); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); + if let Err(err) = app.get_matches_from_safe(args) { + if err.kind == clap::ErrorKind::HelpDisplayed + || err.kind == clap::ErrorKind::VersionDisplayed + { + println!("{}", err); + 0 + } else { + show_error!("{}", err); + 1 + } + } else { + exec(); - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => crash!(1, "{}", f), - }; - if matches.opt_present("help") { - println!("{} {}", NAME, VERSION); - println!(""); - println!("Usage:"); - println!(" {} [OPTIONS]", NAME); - println!(""); - println!("{}", opts.usage("print effective userid")); - return 0; + 0 } - if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - return 0; - } - - exec(); - - 0 } pub fn exec() {