Added basename
applet
This commit is contained in:
parent
f70244ea49
commit
1e1e909afe
67
src/cmd/basename/mod.rs
Normal file
67
src/cmd/basename/mod.rs
Normal file
@ -0,0 +1,67 @@
|
||||
use super::Cmd;
|
||||
use clap::{Arg, ArgMatches, Command};
|
||||
use std::io;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Basename {
|
||||
name: &'static str,
|
||||
path: Option<crate::Path>,
|
||||
}
|
||||
|
||||
impl Default for Basename {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "basename",
|
||||
path: Some(crate::Path::UsrBin),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Cmd for Basename {
|
||||
fn name(&self) -> &str {
|
||||
self.name
|
||||
}
|
||||
|
||||
fn cli(&self) -> clap::Command {
|
||||
Command::new(self.name)
|
||||
.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"),
|
||||
])
|
||||
}
|
||||
|
||||
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let Some(matches) = matches else {
|
||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
|
||||
};
|
||||
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(())
|
||||
}
|
||||
|
||||
fn path(&self) -> Option<crate::Path> {
|
||||
self.path
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ use std::{error::Error, fmt};
|
||||
|
||||
pub mod base32;
|
||||
pub mod base64;
|
||||
pub mod basename;
|
||||
pub mod bootstrap;
|
||||
mod cat;
|
||||
mod chmod;
|
||||
@ -33,10 +34,10 @@ pub mod r#true;
|
||||
pub mod yes;
|
||||
|
||||
pub use {
|
||||
self::hostname::Hostname, base32::Base32, base64::Base64, bootstrap::Bootstrap,
|
||||
dirname::Dirname, echo::Echo, factor::Factor, head::Head, mountpoint::Mountpoint,
|
||||
nologin::Nologin, nproc::Nproc, r#false::False, r#true::True, rev::Rev, shitbox::Shitbox, sleep::Sleep,
|
||||
yes::Yes,
|
||||
self::hostname::Hostname, base32::Base32, base64::Base64, basename::Basename,
|
||||
bootstrap::Bootstrap, dirname::Dirname, echo::Echo, factor::Factor, head::Head,
|
||||
mountpoint::Mountpoint, nologin::Nologin, nproc::Nproc, r#false::False, r#true::True, rev::Rev,
|
||||
shitbox::Shitbox, sleep::Sleep, yes::Yes,
|
||||
};
|
||||
|
||||
pub trait Cmd: fmt::Debug + Sync {
|
||||
@ -50,6 +51,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
|
||||
match name {
|
||||
"base64" => Some(Box::new(Base64::default())),
|
||||
"base32" => Some(Box::new(Base32::default())),
|
||||
"basename" => Some(Box::new(Basename::default())),
|
||||
"bootstrap" => Some(Box::new(Bootstrap::default())),
|
||||
"dirname" => Some(Box::new(Dirname::default())),
|
||||
"echo" => Some(Box::new(Echo::default())),
|
||||
@ -68,9 +70,10 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
|
||||
}
|
||||
}
|
||||
|
||||
pub static COMMANDS: [&'static str; 17] = [
|
||||
pub static COMMANDS: [&'static str; 18] = [
|
||||
"base32",
|
||||
"base64",
|
||||
"basename",
|
||||
"bootstrap",
|
||||
"dirname",
|
||||
"echo",
|
||||
|
@ -1,6 +1,6 @@
|
||||
use clap::{Arg, Command, ArgAction};
|
||||
use std::io;
|
||||
use super::Cmd;
|
||||
use clap::{Arg, ArgAction, Command};
|
||||
use std::io;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Nproc {
|
||||
|
Loading…
Reference in New Issue
Block a user