1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #1623 from sylvestre/clap-pwd

refactor(pwd): move to clap + add a test
This commit is contained in:
Sylvestre Ledru 2020-11-21 00:49:30 +01:00 committed by GitHub
commit 7bbb4c98e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 47 deletions

2
Cargo.lock generated
View file

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

View file

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

View file

@ -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<PathBuf> {
let path_buf = path.canonicalize()?;
@ -32,42 +35,34 @@ pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {
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") {
if matches.is_present(OPT_LOGICAL) {
println!("{}", logical_path.display());
} else {
match absolute_path(&logical_path) {
@ -78,7 +73,6 @@ Print the full filename of the current working directory.",
}
Err(e) => crash!(1, "failed to get current directory {}", e),
};
}
0
}

View file

@ -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();
}