aboutsummaryrefslogtreecommitdiff
path: root/src/cliargs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/cliargs.rs')
-rw-r--r--src/cliargs.rs173
1 files changed, 131 insertions, 42 deletions
diff --git a/src/cliargs.rs b/src/cliargs.rs
index 082ecda..e766e77 100644
--- a/src/cliargs.rs
+++ b/src/cliargs.rs
@@ -15,23 +15,23 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/////
-use color_eyre::eyre::Result;
use dirs::{config_dir, home_dir};
+use indoc::formatdoc;
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::bibiman::citekeys::CitekeyFormatting;
+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,9 +39,16 @@ pub struct CLIArgs {
}
impl CLIArgs {
- pub fn parse_args() -> Result<CLIArgs, lexopt::Error> {
+ /// This struct parses the command line and initializes and returns the
+ /// necessary structs `CLIArgs` and `BibiConfig`.
+ ///
+ /// Additionally, it returns a bool which defines if the TUI should be run
+ /// or not. The latter is the case for pure CLI processes as `format-citekeys`.
+ pub fn parse_args() -> color_eyre::Result<(CLIArgs, BibiConfig, bool)> {
let mut args = CLIArgs::default();
let mut parser = lexopt::Parser::from_env();
+ let mut subcommand = None;
+ let mut run_tui = true;
// Default config
args.cfg_path = if config_dir().is_some() {
@@ -52,22 +59,77 @@ 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() {
+ let value = pos_arg
+ .into_string()
+ .unwrap_or_else(|os| os.to_string_lossy().to_string());
+ match value.as_str() {
+ "format-citekeys" => {
+ subcommand = Some(value);
+ run_tui = false;
+ break;
+ }
+ _ => {
+ args.pos_args.push(value.into());
+ }
+ }
+ } else {
+ args.pos_args.push(pos_arg.into());
+ }
+ }
+ _ => return Err(arg.unexpected().into()),
}
}
- Ok(args)
+ if args
+ .cfg_path
+ .as_ref()
+ .is_some_and(|f| f.try_exists().is_err() || !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)
+ };
+
+ if let Some(cmd) = subcommand {
+ match cmd.as_str() {
+ "format-citekeys" => {
+ CitekeyFormatting::parse_citekey_cli(&mut parser, &cfg)?;
+ }
+ _ => {}
+ }
+ }
+
+ cfg.cli_overwrite(&args);
+
+ Ok((args, cfg, run_tui))
}
}
@@ -121,14 +183,21 @@ pub fn help_func() -> String {
env!("CARGO_PKG_VERSION").fg::<LightGreen>(),
),
format!(
- "{}:\n\t{} [Flags] [files/dirs]\n",
+ "{}\n\t{} [OPTIONS] [SUBCOMMAND | POSITIONAL ARGUMENTS]\n",
"USAGE".bold(),
- "bibiman".bold()
+ env!("CARGO_PKG_NAME").fg::<White>().bold()
+ ),
+ formatdoc!(
+ "
+ \tYou can either use a {} or {}, not both!
+ ",
+ "subcommand".bold(),
+ "positional arguments".bold()
),
format!(
- "{}:\n\t{}\t\tPath to {} file",
+ "{}\n\t{}\t\tPath to {} file",
"POSITIONAL ARGUMENTS".bold(),
- "<file>".fg::<BrightMagenta>().bold(),
+ "<file>".fg::<Magenta>().bold(),
".bib".fg::<BrightBlack>().bold()
),
format!(
@@ -137,38 +206,58 @@ pub fn help_func() -> String {
".bib".fg::<BrightBlack>().bold()
),
format!("\n\t{}", "Both can be passed multiple times".italic()),
- format!("\n{}:", "FLAGS".bold()),
- format!("\t{}", "-h, --help".bold().fg::<BrightCyan>()),
- format!("\t\t{}", "Show this help and exit"),
- format!("\t{}", "-v, --version".bold().fg::<BrightCyan>()),
- format!("\t\t{}", "Show the version and exit"),
- format!("\t{}", "--light-terminal".bold().fg::<BrightCyan>()),
- format!(
- "\t\t{}",
- "Enable default colors for light terminal background"
+ format!("\n{}", "SUBCOMMANDS".bold()),
+ formatdoc!(
+ "
+ \t{}
+ \tRun the citekey formatting procedure on a specified bibfile.
+ \tFor further infos run {}
+ ",
+ "format-citekeys".fg::<BrightYellow>().bold(),
+ "bibiman format-citekeys --help".fg::<BrightBlack>().bold()
),
- format!(
- "\t{}{}",
- "-c, --config-file=".bold().fg::<BrightCyan>(),
- "<value>".bold().italic().fg::<BrightCyan>()
+ format!("{}", "OPTIONS".bold()),
+ formatdoc!(
+ "
+ \t{}
+ \tShow this help and exit
+ ",
+ "-h, --help".bold().fg::<White>()
),
- format!("\t\t{}", "Path to config file used for current session."),
- format!("\t\t{}", "Takes precedence over standard config file."),
- format!(
- "\t{}{}",
- "--pdf-path=".bold().fg::<BrightCyan>(),
- "<value>".bold().italic().fg::<BrightCyan>()
+ formatdoc!(
+ "
+ \t{}
+ \tShow the version and exit
+ ",
+ "-v, --version".bold().fg::<White>()
),
- format!("\t\t{}", "Path to directory containing PDF files."),
- format!(
- "\t\t{}",
- "If the pdf files basename matches an entrys citekey,"
+ formatdoc!(
+ "
+ \t{}
+ \tEnable default colors for light terminal background
+ ",
+ "--light-terminal".bold().fg::<White>()
),
- format!(
- "\t\t{}",
- "its attached as connected PDF file for the current session."
+ formatdoc!(
+ "
+ \t{}{}
+ \tPath to config file used for current session.
+ \tTakes precedence over standard config file.
+ ",
+ "-c, --config-file=".bold().fg::<White>(),
+ "<value>".bold().italic().fg::<White>()
+ ),
+ formatdoc!(
+ "
+ \t{}{}
+ \tPath to directory containing PDF files.
+ \tIf the pdf files basename matches an entrys citekey,
+ \tits attached as connected PDF file for the current session.
+ \tDoes not edit the bibfile itself!
+ ",
+ "--pdf-path=".bold().fg::<White>(),
+ "<value>".bold().italic().fg::<White>()
),
- format!("\t\t{}", "Does not edit the bibfile itself!"),
];
let help = help.join("\n");
help