aboutsummaryrefslogtreecommitdiff
path: root/src/cliargs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/cliargs.rs')
-rw-r--r--src/cliargs.rs52
1 files changed, 43 insertions, 9 deletions
diff --git a/src/cliargs.rs b/src/cliargs.rs
index 082ecda..3b12fc3 100644
--- a/src/cliargs.rs
+++ b/src/cliargs.rs
@@ -18,20 +18,19 @@
use color_eyre::eyre::Result;
use dirs::{config_dir, home_dir};
use lexopt::prelude::*;
+use owo_colors::OwoColorize;
use owo_colors::colors::css::LightGreen;
use owo_colors::colors::*;
-use owo_colors::OwoColorize;
use std::env;
use std::path::PathBuf;
use walkdir::WalkDir;
use crate::app;
+use crate::config::BibiConfig;
// struct for CLIArgs
#[derive(Debug, Default, Clone)]
pub struct CLIArgs {
- pub helparg: bool,
- pub versionarg: bool,
pub pos_args: Vec<PathBuf>,
pub cfg_path: Option<PathBuf>,
pub light_theme: bool,
@@ -39,7 +38,7 @@ pub struct CLIArgs {
}
impl CLIArgs {
- pub fn parse_args() -> Result<CLIArgs, lexopt::Error> {
+ pub fn parse_args() -> color_eyre::Result<(CLIArgs, BibiConfig)> {
let mut args = CLIArgs::default();
let mut parser = lexopt::Parser::from_env();
@@ -52,22 +51,57 @@ impl CLIArgs {
None
};
+ // if parser
+ // .raw_args()
+ // .is_ok_and(|mut arg| arg.next_if(|a| a == "format-citekeys").is_some())
+ // {
+ // todo!("Format citekeys options");
+ // }
+
while let Some(arg) = parser.next()? {
match arg {
- Short('h') | Long("help") => args.helparg = true,
- Short('v') | Long("version") => args.versionarg = true,
+ Short('h') | Long("help") => {
+ println!("{}", help_func());
+ std::process::exit(0);
+ }
+ Short('v') | Long("version") => {
+ println!("{}", version_func());
+ std::process::exit(0);
+ }
Short('c') | Long("config-file") => args.cfg_path = Some(parser.value()?.parse()?),
Long("light-terminal") => args.light_theme = true,
Long("pdf-path") => {
args.pdf_path = Some(parser.value()?.parse()?);
}
// Value(pos_arg) => parse_files(&mut args, pos_arg),
- Value(pos_arg) => args.pos_args.push(pos_arg.into()),
- _ => return Err(arg.unexpected()),
+ Value(pos_arg) => {
+ if args.pos_args.is_empty() && pos_arg == "format-citekeys" {
+ todo!("Write format citekeys function");
+ } else {
+ args.pos_args.push(parser.value()?.into());
+ }
+ }
+ _ => return Err(arg.unexpected().into()),
}
}
- Ok(args)
+ if args
+ .cfg_path
+ .as_ref()
+ .is_some_and(|f| !f.try_exists().unwrap() || !f.is_file())
+ {
+ BibiConfig::create_default_config(&args);
+ }
+
+ let mut cfg = if args.cfg_path.is_some() {
+ BibiConfig::parse_config(&args)?
+ } else {
+ BibiConfig::new(&args)
+ };
+
+ cfg.cli_overwrite(&args);
+
+ Ok((args, cfg))
}
}