mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
refactor(realpath): move to clap
This commit is contained in:
parent
e331186fee
commit
9f7259b986
3 changed files with 61 additions and 72 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1933,7 +1933,7 @@ dependencies = [
|
||||||
name = "uu_realpath"
|
name = "uu_realpath"
|
||||||
version = "0.0.2"
|
version = "0.0.2"
|
||||||
dependencies = [
|
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.5",
|
"uucore 0.0.5",
|
||||||
"uucore_procs 0.0.5",
|
"uucore_procs 0.0.5",
|
||||||
]
|
]
|
||||||
|
|
|
@ -15,7 +15,7 @@ edition = "2018"
|
||||||
path = "src/realpath.rs"
|
path = "src/realpath.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
getopts = "0.2.18"
|
clap = "2.33"
|
||||||
uucore = { version=">=0.0.5", package="uucore", path="../../uucore", features=["fs"] }
|
uucore = { version=">=0.0.5", package="uucore", path="../../uucore", features=["fs"] }
|
||||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
||||||
|
|
|
@ -10,61 +10,71 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
|
use clap::{App, Arg};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::PathBuf;
|
||||||
use uucore::fs::{canonicalize, CanonicalizeMode};
|
use uucore::fs::{canonicalize, CanonicalizeMode};
|
||||||
|
|
||||||
static NAME: &str = "realpath";
|
static ABOUT: &str = "print the resolved path";
|
||||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
static OPT_QUIET: &str = "quiet";
|
||||||
|
static OPT_STRIP: &str = "strip";
|
||||||
|
static OPT_ZERO: &str = "zero";
|
||||||
|
|
||||||
|
static ARG_FILES: &str = "files";
|
||||||
|
|
||||||
|
fn get_usage() -> String {
|
||||||
|
format!("{0} [OPTION]... FILE...", executable!())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
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_QUIET)
|
||||||
|
.short("q")
|
||||||
|
.long(OPT_QUIET)
|
||||||
|
.help("Do not print warnings for invalid paths"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(OPT_STRIP)
|
||||||
|
.short("s")
|
||||||
|
.long(OPT_STRIP)
|
||||||
|
.help("Only strip '.' and '..' components, but don't resolve symbolic links"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(OPT_ZERO)
|
||||||
|
.short("z")
|
||||||
|
.long(OPT_ZERO)
|
||||||
|
.help("Separate output filenames with \\0 rather than newline"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(ARG_FILES)
|
||||||
|
.multiple(true)
|
||||||
|
.takes_value(true)
|
||||||
|
.required(true)
|
||||||
|
.min_values(1),
|
||||||
|
)
|
||||||
|
.get_matches_from(args);
|
||||||
|
|
||||||
opts.optflag("h", "help", "Show help and exit");
|
/* the list of files */
|
||||||
opts.optflag("V", "version", "Show version and exit");
|
|
||||||
opts.optflag(
|
|
||||||
"s",
|
|
||||||
"strip",
|
|
||||||
"Only strip '.' and '..' components, but don't resolve symbolic links",
|
|
||||||
);
|
|
||||||
opts.optflag(
|
|
||||||
"z",
|
|
||||||
"zero",
|
|
||||||
"Separate output filenames with \\0 rather than newline",
|
|
||||||
);
|
|
||||||
opts.optflag("q", "quiet", "Do not print warnings for invalid paths");
|
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
let paths: Vec<PathBuf> = matches
|
||||||
Ok(m) => m,
|
.values_of(ARG_FILES)
|
||||||
Err(f) => {
|
.unwrap()
|
||||||
show_error!("{}", f);
|
.map(|path| PathBuf::from(path))
|
||||||
show_usage(&opts);
|
.collect();
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if matches.opt_present("V") {
|
let strip = matches.is_present(OPT_STRIP);
|
||||||
version();
|
let zero = matches.is_present(OPT_ZERO);
|
||||||
return 0;
|
let quiet = matches.is_present(OPT_QUIET);
|
||||||
}
|
|
||||||
if matches.opt_present("h") {
|
|
||||||
show_usage(&opts);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches.free.is_empty() {
|
|
||||||
show_error!("Missing operand: FILENAME, at least one is required");
|
|
||||||
println!("Try `{} --help` for more information.", NAME);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
let strip = matches.opt_present("s");
|
|
||||||
let zero = matches.opt_present("z");
|
|
||||||
let quiet = matches.opt_present("q");
|
|
||||||
let mut retcode = 0;
|
let mut retcode = 0;
|
||||||
for path in &matches.free {
|
for path in &paths {
|
||||||
if !resolve_path(path, strip, zero, quiet) {
|
if !resolve_path(path, strip, zero, quiet) {
|
||||||
retcode = 1
|
retcode = 1
|
||||||
};
|
};
|
||||||
|
@ -72,15 +82,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
retcode
|
retcode
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_path(path: &str, strip: bool, zero: bool, quiet: bool) -> bool {
|
fn resolve_path(p: &PathBuf, strip: bool, zero: bool, quiet: bool) -> bool {
|
||||||
let p = Path::new(path).to_path_buf();
|
|
||||||
let abs = canonicalize(p, CanonicalizeMode::Normal).unwrap();
|
let abs = canonicalize(p, CanonicalizeMode::Normal).unwrap();
|
||||||
|
|
||||||
if strip {
|
if strip {
|
||||||
if zero {
|
if zero {
|
||||||
print!("{}\0", abs.display());
|
print!("{}\0", p.display());
|
||||||
} else {
|
} else {
|
||||||
println!("{}", abs.display())
|
println!("{}", p.display())
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -93,7 +102,7 @@ fn resolve_path(path: &str, strip: bool, zero: bool, quiet: bool) -> bool {
|
||||||
loop {
|
loop {
|
||||||
if links_left == 0 {
|
if links_left == 0 {
|
||||||
if !quiet {
|
if !quiet {
|
||||||
show_error!("Too many symbolic links: {}", path)
|
show_error!("Too many symbolic links: {}", p.display())
|
||||||
};
|
};
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -109,7 +118,7 @@ fn resolve_path(path: &str, strip: bool, zero: bool, quiet: bool) -> bool {
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if !quiet {
|
if !quiet {
|
||||||
show_error!("Invalid path: {}", path)
|
show_error!("Invalid path: {}", p.display())
|
||||||
};
|
};
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -127,23 +136,3 @@ fn resolve_path(path: &str, strip: bool, zero: bool, quiet: bool) -> bool {
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn version() {
|
|
||||||
println!("{} {}", NAME, VERSION)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn show_usage(opts: &getopts::Options) {
|
|
||||||
version();
|
|
||||||
println!();
|
|
||||||
println!("Usage:");
|
|
||||||
println!(" {} [-s|--strip] [-z|--zero] FILENAME...", NAME);
|
|
||||||
println!(" {} -V|--version", NAME);
|
|
||||||
println!(" {} -h|--help", NAME);
|
|
||||||
println!();
|
|
||||||
print!("{}", opts.usage(
|
|
||||||
"Convert each FILENAME to the absolute path.\n\
|
|
||||||
All the symbolic links will be resolved, resulting path will contain no special components like '.' or '..'.\n\
|
|
||||||
Each path component must exist or resolution will fail and non-zero exit status returned.\n\
|
|
||||||
Each resolved FILENAME will be written to the standard output, one per line.")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue