diff options
| -rw-r--r-- | Cargo.lock | 7 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/bibiman.rs | 2 | ||||
| -rw-r--r-- | src/cliargs.rs | 80 | ||||
| -rw-r--r-- | src/main.rs | 6 |
5 files changed, 46 insertions, 50 deletions
@@ -83,7 +83,6 @@ dependencies = [ "lexopt", "nucleo-matcher", "ratatui", - "sarge", "signal-hook", "tokio", "tokio-util", @@ -1098,12 +1097,6 @@ dependencies = [ ] [[package]] -name = "sarge" -version = "7.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2096e40214337b2293fa4c3eb63c5376b57f4247cbbaf6d0af477f1d36ac21d9" - -[[package]] name = "scoped-tls" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -27,7 +27,6 @@ itertools = "0.13.0" lexopt = "0.3.0" nucleo-matcher = "0.3.1" ratatui = { version = "0.29.0", features = ["unstable-rendered-line-info"]} -sarge = "7.2.5" signal-hook = "0.3.17" tokio = { version = "1.39.3", features = ["full"] } tokio-util = "0.7.12" diff --git a/src/bibiman.rs b/src/bibiman.rs index 4d0aa60..3ae0520 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -77,7 +77,7 @@ pub struct Bibiman { impl Bibiman { // Constructs a new instance of [`App`]. pub fn new(args: CLIArgs) -> Result<Self> { - let main_bibfiles = args.bibfilearg; + let main_bibfiles = args.pos_args; let main_biblio = BibiSetup::new(&main_bibfiles); let tag_list = TagList::new(main_biblio.keyword_list.clone()); let search_struct = BibiSearch::default(); diff --git a/src/cliargs.rs b/src/cliargs.rs index a298f65..1b03019 100644 --- a/src/cliargs.rs +++ b/src/cliargs.rs @@ -15,57 +15,65 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. ///// -use sarge::prelude::*; +use color_eyre::eyre::Result; +use color_eyre::owo_colors::OwoColorize; +use lexopt::prelude::*; use std::env; +use std::ffi::OsString; use std::path::PathBuf; - -sarge! { - // Name of the struct - ArgumentsCLI, - - // Show help and exit. - 'h' help: bool, - - // Show version and exit. - 'v' version: bool, -} +use walkdir::WalkDir; // struct for CLIArgs +#[derive(Debug, Default)] pub struct CLIArgs { pub helparg: bool, pub versionarg: bool, - pub bibfilearg: Vec<PathBuf>, -} - -impl Default for CLIArgs { - fn default() -> Self { - Self::new() - } + pub pos_args: Vec<PathBuf>, } impl CLIArgs { - pub fn new() -> Self { - let (cli_args, pos_args) = ArgumentsCLI::parse().expect("Could not parse CLI arguments"); - let bibfilearg = if pos_args.len() > 1 { - parse_files(pos_args) - } else { - panic!("No bibfile provided") - }; - Self { - helparg: cli_args.help, - versionarg: cli_args.version, - bibfilearg, + pub fn parse_args() -> Result<CLIArgs, lexopt::Error> { + let mut args = CLIArgs::default(); + let mut parser = lexopt::Parser::from_env(); + + while let Some(arg) = parser.next()? { + match arg { + Short('h') | Long("help") => args.helparg = true, + Short('v') | Long("version") => args.versionarg = true, + Value(pos_arg) => parse_files(&mut args, pos_arg), + _ => return Err(arg.unexpected()), + } } + Ok(args) } } -pub fn parse_files(args: Vec<String>) -> Vec<PathBuf> { - let mut files: Vec<PathBuf> = Vec::new(); - for f in args { - files.push(PathBuf::from(f)) +pub fn parse_files(args: &mut CLIArgs, pos_arg: OsString) { + // convert to PathBuf to use methods for testing the path + let path = PathBuf::from(pos_arg); + // If pos arg is file, just push it to path vec + if path.is_file() { + args.pos_args.push(path); + // If pos arg is dir, walk dir and collect bibfiles + } else if path.is_dir() { + for file in WalkDir::new(path) { + let f = file.unwrap().into_path(); + if f.is_file() && f.extension().unwrap() == "bib" { + args.pos_args.push(f) + } + } + } else { + println!( + "{} {}", + "The positional argument is neither a valid file, nor a path:" + .red() + .bold(), + path.as_os_str().to_string_lossy().bright_red().italic() + ); + println!(); + println!("{}", help_func()); + std::process::exit(1) } - files.remove(0); - files } pub fn help_func() -> String { diff --git a/src/main.rs b/src/main.rs index 95bf16a..747a8f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,7 @@ const TEXT_FG_COLOR_INDEX: u8 = 250; #[tokio::main] async fn main() -> Result<()> { // Parse CLI arguments - let parsed_args = CLIArgs::new(); + let parsed_args = CLIArgs::parse_args().unwrap(); // Print help if -h/--help flag is passed and exit if parsed_args.helparg { @@ -52,10 +52,6 @@ async fn main() -> Result<()> { std::process::exit(0); } - // if !parsed_args.bibfilearg.is_file() { - // panic!("No \'.bib\' file passed, aborting") - // } - init_error_hooks()?; // Create an application. |
