diff --git a/Cargo.lock b/Cargo.lock index f40f1c094..20966391c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1937,7 +1937,7 @@ dependencies = [ name = "uu_pwd" 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", "uucore_procs 0.0.4", ] diff --git a/src/uu/pwd/Cargo.toml b/src/uu/pwd/Cargo.toml index c61e8bc14..876aee173 100644 --- a/src/uu/pwd/Cargo.toml +++ b/src/uu/pwd/Cargo.toml @@ -15,7 +15,7 @@ edition = "2018" path = "src/pwd.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" uucore = { version=">=0.0.4", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.4", package="uucore_procs", path="../../uucore_procs" } diff --git a/src/uu/pwd/src/pwd.rs b/src/uu/pwd/src/pwd.rs index 1861c8084..9196328ec 100644 --- a/src/uu/pwd/src/pwd.rs +++ b/src/uu/pwd/src/pwd.rs @@ -5,17 +5,20 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -extern crate getopts; +extern crate clap; #[macro_use] extern crate uucore; +use clap::{App, Arg}; use std::env; use std::io; use std::path::{Path, PathBuf}; -static NAME: &str = "pwd"; +static ABOUT: &str = "Display the full filename of the current working directory."; static VERSION: &str = env!("CARGO_PKG_VERSION"); +static OPT_LOGICAL: &str = "logical"; +static OPT_PHYSICAL: &str = "physical"; pub fn absolute_path(path: &Path) -> io::Result { let path_buf = path.canonicalize()?; @@ -32,53 +35,44 @@ pub fn absolute_path(path: &Path) -> io::Result { Ok(path_buf) } +fn get_usage() -> String { + format!("{0} [OPTION]... FILE...", executable!()) +} + 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!()) + .version(VERSION) + .about(ABOUT) + .usage(&usage[..]) + .arg( + Arg::with_name(OPT_LOGICAL) + .short("L") + .long(OPT_LOGICAL) + .help("use PWD from environment, even if it contains symlinks"), + ) + .arg( + Arg::with_name(OPT_PHYSICAL) + .short("P") + .long(OPT_PHYSICAL) + .help("avoid all symlinks"), + ) + .get_matches_from(args); - opts.optflag("", "help", "display this help and exit"); - opts.optflag("", "version", "output version information and exit"); - opts.optflag( - "L", - "logical", - "use PWD from environment, even if it contains symlinks", - ); - opts.optflag("P", "physical", "avoid all symlinks"); - - 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: - {0} [OPTION]... - -Print the full filename of the current working directory.", - NAME, VERSION - ); - print!("{}", opts.usage(&msg)); - } else if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - } else { - match env::current_dir() { - Ok(logical_path) => { - if matches.opt_present("logical") { - println!("{}", logical_path.display()); - } else { - match absolute_path(&logical_path) { - Ok(physical_path) => println!("{}", physical_path.display()), - Err(e) => crash!(1, "failed to get absolute path {}", e), - }; - } + match env::current_dir() { + Ok(logical_path) => { + if matches.is_present(OPT_LOGICAL) { + println!("{}", logical_path.display()); + } else { + match absolute_path(&logical_path) { + Ok(physical_path) => println!("{}", physical_path.display()), + Err(e) => crash!(1, "failed to get absolute path {}", e), + }; } - Err(e) => crash!(1, "failed to get current directory {}", e), - }; - } + } + Err(e) => crash!(1, "failed to get current directory {}", e), + }; 0 } diff --git a/tests/by-util/test_pwd.rs b/tests/by-util/test_pwd.rs index aecb700da..b6a6c87a4 100644 --- a/tests/by-util/test_pwd.rs +++ b/tests/by-util/test_pwd.rs @@ -5,3 +5,9 @@ fn test_default() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.run().stdout_is(at.root_dir_resolved() + "\n"); } + +#[test] +fn test_failed() { + let (_at, mut ucmd) = at_and_ucmd!(); + ucmd.arg("willfail").fails(); +}