2023-01-01 11:26:56 -05:00
|
|
|
use super::Cmd;
|
|
|
|
use clap::{Arg, ArgAction, Command};
|
|
|
|
use std::path::Path;
|
|
|
|
|
2023-01-13 01:08:32 -05:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct Dirname;
|
2023-01-01 11:26:56 -05:00
|
|
|
|
|
|
|
impl Cmd for Dirname {
|
|
|
|
fn cli(&self) -> clap::Command {
|
|
|
|
Command::new("dirname")
|
|
|
|
.about("strip last component from file name")
|
|
|
|
.args([
|
|
|
|
Arg::new("zero")
|
|
|
|
.short('z')
|
|
|
|
.long("zero")
|
|
|
|
.help("end each output line with NUL, not newline")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
Arg::new("name").num_args(1..).required(true),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
if let Some(names) = matches.get_many::<String>("name") {
|
|
|
|
names.for_each(|name| {
|
|
|
|
let path = match Path::new(name).parent() {
|
|
|
|
Some(p) => p,
|
|
|
|
None => Path::new("."),
|
|
|
|
};
|
|
|
|
let path = path.to_string_lossy();
|
|
|
|
let path = if path.is_empty() {
|
|
|
|
String::from(".")
|
|
|
|
} else {
|
|
|
|
path.to_string()
|
|
|
|
};
|
|
|
|
if matches.get_flag("zero") {
|
|
|
|
print!("{path}\0");
|
|
|
|
} else {
|
|
|
|
println!("{path}");
|
|
|
|
}
|
|
|
|
});
|
2023-01-01 11:26:56 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self) -> Option<crate::Path> {
|
2023-01-13 01:08:32 -05:00
|
|
|
Some(crate::Path::UsrBin)
|
2023-01-01 11:26:56 -05:00
|
|
|
}
|
|
|
|
}
|