From 07c13d707f4b104b2068d1816801f0540085e705 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Mon, 3 Feb 2025 18:19:39 +1300 Subject: Use switchboard crate for parsing command line arguments --- src/main.rs | 88 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 82814e4..dc432ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,29 +12,59 @@ use std::collections::HashSet; use std::time::SystemTime; use log::{info, warn, error, fatal}; +use switchboard::{Switchboard, SwitchQuery}; + + +fn print_help() -> ! { + eprintln!("\ +Usage: toaster + +Generate a website from a structured directory of markdown files. + +Arguments: + source Source directory with markdown files + destination Path to output directory + +Switches: + --delete Delete the destination directory first if it exists + --html Generate HTML output + --version, -v Print information as each file is parsed + --version Print the program version and exit + --help, -h Print help +"); + std::process::exit(0); +} + +fn print_version() -> ! { + let version = env!("CARGO_PKG_VERSION"); + eprintln!("toaster, version {version}"); + eprintln!("written by ben bridle"); + std::process::exit(0); +} fn main() { - let args = Arguments::from_env_or_exit(); - if args.version { - let version = env!("CARGO_PKG_VERSION"); - eprintln!("toaster, version {version}"); - std::process::exit(0); + let mut args = Switchboard::from_env(); + if args.named("help").short('h').as_bool() { + print_help(); } - if args.verbose { - log::set_log_level(log::LogLevel::Info); + if args.named("version").as_bool() { + print_version(); } - if args.source.is_none() || args.destination.is_none() { - fatal!("Provide a source directory and a destination directory.") + if args.named("verbose").short('v').as_bool() { + log::set_log_level(log::LogLevel::Info); } - let source_directory = match args.source.as_ref().unwrap().canonicalize() { - Ok(source_directory) => source_directory, - Err(err) => fatal!("{:?}: {err}", args.source.unwrap()), + let source = args.positional("source").as_path(); + let destination = args.positional("destination").as_path(); + let delete_existing = args.named("delete").as_bool(); + let export_html = args.named("html").as_bool(); + + let source = match source.canonicalize() { + Ok(source) => source, + Err(err) => fatal!("{source:?}: {err}"), }; - let destination_directory = args.destination.unwrap(); - - let website = Website::from_path(&source_directory); + let website = Website::from_path(&source); // Check for duplicate output paths for pages. let mut destinations: HashSet<&str> = HashSet::new(); @@ -55,10 +85,10 @@ fn main() { } } - let mut destination = destination_directory.clone(); + let mut destination = destination; destination.push(make_url_safe(&website.name)); - if args.delete && Entry::from_path(&destination).is_ok() { + if delete_existing && Entry::from_path(&destination).is_ok() { info!("Deleting existing destination directory {destination:?}"); remove(&destination).unwrap_or_else(|_| error!("Failed to delete existing destination directory {destination:?}")); @@ -69,7 +99,7 @@ fn main() { let mut destination = destination.clone(); destination.push(&page.full_url); // Convert document to different formats. - if args.html { + if export_html { let html = generate_html(&page.document, page, &website); write_file(&html, &destination, "html", page.last_modified); } @@ -97,7 +127,7 @@ fn main() { let mut destination = destination.clone(); destination.push(&redirect.full_url); let path = &redirect.redirect; - if args.html { + if export_html { if !path.contains("://") { if let Some(path) = website.has_page(redirect, &path, "html") { write_file(&generate_html_redirect(&path), &destination, "html", redirect.last_modified); @@ -138,23 +168,3 @@ pub fn make_url_safe(text: &str) -> String { } - -xflags::xflags! { - /// Generate a website from a structured directory of markdown files. - cmd arguments { - /// Source directory with markdown files - optional source: PathBuf - /// Path to output directory - optional destination: PathBuf - /// Delete the destination directory first if it exists - optional --delete - /// Generate HTML output - optional --html - /// Generate Gemtext output - optional --gmi - /// Print information as each file is parsed - optional -v, --verbose - /// Print the program version and exit - optional --version - } -} -- cgit v1.2.3-70-g09d2