From 115b99ee66ab1a576f878cd24b0ea2c319cc8df7 Mon Sep 17 00:00:00 2001 From: Bauke Date: Mon, 6 Nov 2023 18:54:33 +0100 Subject: [PATCH] Add the file parts subcommand. --- source/cli/mod.rs | 19 +++++++++++++++++++ source/cli/run.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/source/cli/mod.rs b/source/cli/mod.rs index dd235cb..911e737 100644 --- a/source/cli/mod.rs +++ b/source/cli/mod.rs @@ -71,4 +71,23 @@ pub enum FileSubcommand { #[arg()] file: PathBuf, }, + + /// Extract parts of a file. + Parts { + /// Print the base name of the file (without the extension). + #[arg(long, group = "part-to-print", default_value = "false")] + basename: bool, + + /// Print the directory the file is in. + #[arg(long, group = "part-to-print", default_value = "false")] + directory: bool, + + /// Print the file extension (without the leading dot). + #[arg(long, group = "part-to-print", default_value = "false")] + extension: bool, + + /// The file to include parts from. + #[arg()] + file: PathBuf, + }, } diff --git a/source/cli/run.rs b/source/cli/run.rs index 10f0508..9ce2ea1 100644 --- a/source/cli/run.rs +++ b/source/cli/run.rs @@ -8,6 +8,7 @@ use { logging::append_line_to_file, }, chrono::{SecondsFormat, Utc}, + std::{ffi::OsStr, path::Path}, }; /// Parse the CLI arguments and execute them. @@ -36,6 +37,34 @@ pub fn run() { std::process::exit(1); } } + + FileSubcommand::Parts { + basename, + directory, + extension, + file, + } => { + if basename { + print!( + "{}", + file.file_stem().and_then(OsStr::to_str).unwrap_or_default() + ); + } + + if directory { + print!( + "{}", + file.parent().and_then(Path::to_str).unwrap_or_default() + ); + } + + if extension { + print!( + "{}", + file.extension().and_then(OsStr::to_str).unwrap_or_default() + ); + } + } }, Log { data_to_log, file } => {