mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
dirname: move to clap, simplify code
This commit is contained in:
parent
20d071a482
commit
b940b2d79c
3 changed files with 34 additions and 8 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1720,6 +1720,7 @@ dependencies = [
|
||||||
name = "uu_dirname"
|
name = "uu_dirname"
|
||||||
version = "0.0.6"
|
version = "0.0.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"clap",
|
||||||
"libc",
|
"libc",
|
||||||
"uucore",
|
"uucore",
|
||||||
"uucore_procs",
|
"uucore_procs",
|
||||||
|
|
|
@ -15,6 +15,7 @@ edition = "2018"
|
||||||
path = "src/dirname.rs"
|
path = "src/dirname.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = "2.33"
|
||||||
libc = "0.2.42"
|
libc = "0.2.42"
|
||||||
uucore = { version=">=0.0.8", package="uucore", path="../../uucore" }
|
uucore = { version=">=0.0.8", package="uucore", path="../../uucore" }
|
||||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
|
@ -8,32 +8,57 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
|
use clap::{App, Arg};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
static NAME: &str = "dirname";
|
static NAME: &str = "dirname";
|
||||||
static SYNTAX: &str = "[OPTION] NAME...";
|
static SYNTAX: &str = "[OPTION] NAME...";
|
||||||
static SUMMARY: &str = "strip last component from file name";
|
static SUMMARY: &str = "strip last component from file name";
|
||||||
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
static LONG_HELP: &str = "
|
static LONG_HELP: &str = "
|
||||||
Output each NAME with its last non-slash component and trailing slashes
|
Output each NAME with its last non-slash component and trailing slashes
|
||||||
removed; if NAME contains no /'s, output '.' (meaning the current
|
removed; if NAME contains no /'s, output '.' (meaning the current
|
||||||
directory).
|
directory).
|
||||||
";
|
";
|
||||||
|
|
||||||
|
mod options {
|
||||||
|
pub const ZERO: &str = "zero";
|
||||||
|
pub const DIR: &str = "dir";
|
||||||
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let args = args.collect_str();
|
let args = args.collect_str();
|
||||||
|
|
||||||
let matches = app!(SYNTAX, SUMMARY, LONG_HELP)
|
let matches = App::new(executable!())
|
||||||
.optflag("z", "zero", "separate output with NUL rather than newline")
|
.name(NAME)
|
||||||
.parse(args);
|
.usage(SYNTAX)
|
||||||
|
.about(SUMMARY)
|
||||||
|
.after_help(LONG_HELP)
|
||||||
|
.version(VERSION)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(options::ZERO)
|
||||||
|
.short(options::ZERO)
|
||||||
|
.short("z")
|
||||||
|
.takes_value(false)
|
||||||
|
.help("separate output with NUL rather than newline"),
|
||||||
|
)
|
||||||
|
.arg(Arg::with_name(options::DIR).hidden(true).multiple(true))
|
||||||
|
.get_matches_from(args);
|
||||||
|
|
||||||
let separator = if matches.opt_present("zero") {
|
let separator = if matches.is_present(options::ZERO) {
|
||||||
"\0"
|
"\0"
|
||||||
} else {
|
} else {
|
||||||
"\n"
|
"\n"
|
||||||
};
|
};
|
||||||
|
|
||||||
if !matches.free.is_empty() {
|
let dirnames: Vec<String> = matches
|
||||||
for path in &matches.free {
|
.values_of(options::DIR)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.map(str::to_owned)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if !dirnames.is_empty() {
|
||||||
|
for path in dirnames.iter() {
|
||||||
let p = Path::new(path);
|
let p = Path::new(path);
|
||||||
match p.parent() {
|
match p.parent() {
|
||||||
Some(d) => {
|
Some(d) => {
|
||||||
|
@ -54,8 +79,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
print!("{}", separator);
|
print!("{}", separator);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println!("{0}: missing operand", NAME);
|
show_usage_error!("missing operand");
|
||||||
println!("Try '{0} --help' for more information.", NAME);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue