1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

refactor(readlink): move to clap

This commit is contained in:
Sylvestre Ledru 2020-10-18 22:25:35 +02:00 committed by Roy Ivy III
parent 58d7d89e07
commit 733fe925ad
3 changed files with 97 additions and 66 deletions

14
Cargo.lock generated
View file

@ -1202,7 +1202,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -1260,7 +1260,7 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.46"
version = "1.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2007,7 +2007,7 @@ dependencies = [
name = "uu_readlink"
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)",
"libc 0.2.66 (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)",
@ -2398,7 +2398,7 @@ source = "git+https://github.com/uutils/uucore.git?branch=canary#869573459f00ba0
dependencies = [
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -2450,7 +2450,7 @@ dependencies = [
"log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
"wasm-bindgen-shared 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -2470,7 +2470,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
"wasm-bindgen-backend 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)",
"wasm-bindgen-shared 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -2684,7 +2684,7 @@ dependencies = [
"checksum sha3 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26405905b6a56a94c60109cfda62610507ac14a65be531f5767dec5c5a8dd6a0"
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
"checksum syn 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)" = "5ad5de3220ea04da322618ded2c42233d02baca219d6f160a3e9c87cda16c942"
"checksum syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)" = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac"
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
"checksum synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a761d12e6d8dcb4dcf952a7a89b475e3a9d69e4a69307e01a470977642914bd"
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"

View file

@ -15,7 +15,7 @@ edition = "2018"
path = "src/readlink.rs"
[dependencies]
getopts = "0.2.18"
clap = "2.33"
libc = "0.2.42"
uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary", features=["fs"] }
uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" }

View file

@ -7,11 +7,12 @@
// spell-checker:ignore (ToDO) errno
extern crate getopts;
extern crate clap;
#[macro_use]
extern crate uucore;
use clap::{App, Arg};
use std::fs;
use std::io::{stdout, Write};
use std::path::PathBuf;
@ -19,68 +20,106 @@ use uucore::fs::{canonicalize, CanonicalizeMode};
const NAME: &str = "readlink";
const VERSION: &str = env!("CARGO_PKG_VERSION");
const ABOUT: &str = "Print value of a symbolic link or canonical file name";
const OPT_CANONICALIZE: &str = "canonicalize";
const OPT_CANONICALIZE_MISSING: &str = "canonicalize-missing";
const OPT_CANONICALIZE_EXISTING: &str = "canonicalize-existing";
const OPT_ZERO: &str = "zero";
const OPT_NO_NEWLINE: &str = "no-newline";
const OPT_QUIET: &str = "quiet";
const OPT_SILENT: &str = "silent";
const OPT_VERBOSE: &str = "verbose";
const OPT_FILES: &str = "files";
fn get_usage() -> String {
format!("{0} [OPTION]... [FILE]...", executable!())
}
pub fn uumain(args: impl uucore::Args) -> i32 {
let args = args.collect_str();
let mut opts = getopts::Options::new();
opts.optflag(
"f",
"canonicalize",
"canonicalize by following every symlink in every component of the \
given name recursively; all but the last component must exist",
);
opts.optflag(
"e",
"canonicalize-existing",
"canonicalize by following every symlink in every component of the \
let usage = get_usage();
let matches = App::new(executable!())
.version(VERSION)
.about(ABOUT)
.usage(&usage[..])
.arg(
Arg::with_name(OPT_CANONICALIZE)
.short("f")
.long(OPT_CANONICALIZE)
.help(
"canonicalize by following every symlink in every component of the \
given name recursively; all but the last component must exist",
),
)
.arg(
Arg::with_name(OPT_CANONICALIZE_EXISTING)
.short("e")
.long("canonicalize-existing")
.help(
"canonicalize by following every symlink in every component of the \
given name recursively, all components must exist",
);
opts.optflag(
"m",
"canonicalize-missing",
"canonicalize by following every symlink in every component of the \
),
)
.arg(
Arg::with_name(OPT_CANONICALIZE_MISSING)
.short("m")
.long(OPT_CANONICALIZE_MISSING)
.help(
"canonicalize by following every symlink in every component of the \
given name recursively, without requirements on components existence",
);
opts.optflag("n", "no-newline", "do not output the trailing delimiter");
opts.optflag("q", "quiet", "suppress most error messages");
opts.optflag("s", "silent", "suppress most error messages");
opts.optflag("v", "verbose", "report error message");
opts.optflag("z", "zero", "separate output with NUL rather than newline");
opts.optflag("", "help", "display this help and exit");
opts.optflag("", "version", "output version information and exit");
),
)
.arg(
Arg::with_name(OPT_NO_NEWLINE)
.short("n")
.long(OPT_NO_NEWLINE)
.help("do not output the trailing delimiter"),
)
.arg(
Arg::with_name(OPT_QUIET)
.short("q")
.long(OPT_QUIET)
.help("suppress most error messages"),
)
.arg(
Arg::with_name(OPT_SILENT)
.short("s")
.long(OPT_SILENT)
.help("suppress most error messages"),
)
.arg(
Arg::with_name(OPT_VERBOSE)
.short("v")
.long(OPT_VERBOSE)
.help("report error message"),
)
.arg(
Arg::with_name(OPT_ZERO)
.short("z")
.long(OPT_ZERO)
.help("separate output with NUL rather than newline"),
)
.arg(Arg::with_name(OPT_FILES).multiple(true).takes_value(true))
.get_matches_from(args);
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => crash!(1, "Invalid options\n{}", f),
};
if matches.opt_present("help") {
show_usage(&opts);
return 0;
}
let mut no_newline = matches.is_present(OPT_NO_NEWLINE);
let use_zero = matches.is_present(OPT_ZERO);
let silent = matches.is_present(OPT_SILENT) || matches.is_present(OPT_QUIET);
let verbose = matches.is_present(OPT_VERBOSE);
if matches.opt_present("version") {
println!("{} {}", NAME, VERSION);
return 0;
}
let mut no_newline = matches.opt_present("no-newline");
let use_zero = matches.opt_present("zero");
let silent = matches.opt_present("silent") || matches.opt_present("quiet");
let verbose = matches.opt_present("verbose");
let can_mode = if matches.opt_present("canonicalize") {
let can_mode = if matches.is_present(OPT_CANONICALIZE) {
CanonicalizeMode::Normal
} else if matches.opt_present("canonicalize-existing") {
} else if matches.is_present(OPT_CANONICALIZE_EXISTING) {
CanonicalizeMode::Existing
} else if matches.opt_present("canonicalize-missing") {
} else if matches.is_present(OPT_CANONICALIZE_MISSING) {
CanonicalizeMode::Missing
} else {
CanonicalizeMode::None
};
let files = matches.free;
let files: Vec<String> = matches
.values_of(OPT_FILES)
.map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default();
if files.is_empty() {
crash!(
1,
@ -133,11 +172,3 @@ fn show(path: &PathBuf, no_newline: bool, use_zero: bool) {
}
crash_if_err!(1, stdout().flush());
}
fn show_usage(opts: &getopts::Options) {
println!("{} {}", NAME, VERSION);
println!();
println!("Usage: {0} [OPTION]... [FILE]...", NAME);
print!("Print value of a symbolic link or canonical file name");
print!("{}", opts.usage(""));
}