feat: add the dirname command

This commit is contained in:
Teesh 2025-03-30 23:05:49 +03:00
parent 51659bde16
commit 9b73d54905
3 changed files with 38 additions and 0 deletions

View file

@ -0,0 +1,36 @@
use std::path::Path;
use boxutils::{args::ArgParser, commands::Command};
pub struct Dirname;
impl Command for Dirname {
fn execute(&self) {
let args = ArgParser::builder()
.add_flag("--help")
.parse_args("dirname");
if args.get_normal_args().len() != 1 || args.get_flag("--help") {
println!("Usage: dirname FILENAME");
return;
}
// note(teesh): we have already checked for argnums, so we're fine :D
let normal_args = args.get_normal_args();
let file = normal_args.get(0).unwrap();
let path = Path::new(file);
if let Some(parent) = path.parent() {
let mut to_print = format!("{}", parent.display());
// note(teesh): this is stupid, but thats how the box busies
if to_print == "" {
to_print = ".".into();
}
println!("{}", to_print);
} else {
println!("dirname: could not get parent")
}
}
}

View file

@ -25,3 +25,4 @@ command!(base32::Base32);
command!(seq::Seq); command!(seq::Seq);
command!(env::Env); command!(env::Env);
command!(ln::Ln); command!(ln::Ln);
command!(dirname::Dirname);

View file

@ -35,6 +35,7 @@ pub fn get_registry() -> CommandRegistry {
"seq" => coreutils::commands::Seq, "seq" => coreutils::commands::Seq,
"env" => coreutils::commands::Env, "env" => coreutils::commands::Env,
"ln" => coreutils::commands::Ln, "ln" => coreutils::commands::Ln,
"dirname" => coreutils::commands::Dirname,
"box" => Boxcmd "box" => Boxcmd
}); });