1
Fork 0

Add the file parts subcommand.

This commit is contained in:
Bauke 2023-11-06 18:54:33 +01:00
parent 6dc3aed5e8
commit 115b99ee66
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 48 additions and 0 deletions

View File

@ -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,
},
}

View File

@ -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 } => {