summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorBen Bridle <ben@derelict.engineering>2025-02-03 18:19:39 +1300
committerBen Bridle <ben@derelict.engineering>2025-02-03 18:19:39 +1300
commit07c13d707f4b104b2068d1816801f0540085e705 (patch)
tree34fa4b89254cec6bf56456bb3f3e161fb01d988f /src/main.rs
parent582b9c3408f391f7dbba85bb5909c3deb7091f4b (diff)
downloadtoaster-07c13d707f4b104b2068d1816801f0540085e705.zip
Use switchboard crate for parsing command line arguments
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs88
1 files changed, 49 insertions, 39 deletions
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 <source> <destination>
+
+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
- }
-}