1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

Merge pull request #2021 from ycd/dirname

dirname: move to clap, write to stderr on errors
This commit is contained in:
Sylvestre Ledru 2021-04-04 14:45:37 +02:00 committed by GitHub
commit 6bee31e8e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 8 deletions

1
Cargo.lock generated
View file

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

View file

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

View file

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