2023-01-08 01:12:36 -05:00
|
|
|
use super::Cmd;
|
|
|
|
use clap::{Arg, ArgMatches, Command};
|
|
|
|
|
2023-01-13 01:08:32 -05:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct Basename;
|
2023-01-08 01:12:36 -05:00
|
|
|
|
|
|
|
impl Cmd for Basename {
|
|
|
|
fn cli(&self) -> clap::Command {
|
2023-01-13 01:08:32 -05:00
|
|
|
Command::new("basename")
|
2023-01-08 01:12:36 -05:00
|
|
|
.about("Print NAME with any leading directory components removed.")
|
|
|
|
.long_about(
|
|
|
|
"Print NAME with any leading directory components removed.\n\
|
|
|
|
If specified, also remove a trailing SUFFIX.",
|
|
|
|
)
|
|
|
|
.author("Nathan Fisher")
|
|
|
|
.args([
|
|
|
|
Arg::new("NAME")
|
|
|
|
.index(1)
|
|
|
|
.required(true)
|
|
|
|
.help("the filename to process"),
|
|
|
|
Arg::new("SUFFIX")
|
|
|
|
.index(2)
|
|
|
|
.required(false)
|
|
|
|
.help("the suffix to be removed"),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
|
2023-01-08 01:12:36 -05:00
|
|
|
if let Some(mut base) = matches
|
|
|
|
.get_one::<String>("NAME")
|
|
|
|
.and_then(|x| x.split('/').last())
|
|
|
|
{
|
|
|
|
if let Some(suffix) = matches.get_one::<String>("SUFFIX") {
|
|
|
|
base = match base.strip_suffix(suffix) {
|
|
|
|
Some(b) => b,
|
|
|
|
None => base,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
println!("{base}");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-05 23:50:59 -05:00
|
|
|
fn path(&self) -> Option<shitbox::Path> {
|
|
|
|
Some(shitbox::Path::UsrBin)
|
2023-01-08 01:12:36 -05:00
|
|
|
}
|
|
|
|
}
|