From 5e5e02d6a715042c69812d26357b956d7f53f1af Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 17 Jun 2020 01:41:34 +0100 Subject: refactor(cheddar): Switch to clap-rs for command line arguments The complexity of the arg parsing is increasing somewhat because we're adding more features to cheddar, so to set us up for that this switches the arg parsing to the somewhat more flexible clap. Change-Id: I187bc0c1b6c6bd596fa0f6bb494b04e335262ba9 Reviewed-on: https://cl.tvl.fyi/c/depot/+/445 Reviewed-by: lukegb Reviewed-by: tazjin --- tools/cheddar/Cargo.lock | 1 + tools/cheddar/Cargo.toml | 1 + tools/cheddar/src/main.rs | 57 ++++++++++++++++++++++------------------------- 3 files changed, 29 insertions(+), 30 deletions(-) (limited to 'tools') diff --git a/tools/cheddar/Cargo.lock b/tools/cheddar/Cargo.lock index be70e178ab..ab1225cc6b 100644 --- a/tools/cheddar/Cargo.lock +++ b/tools/cheddar/Cargo.lock @@ -98,6 +98,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "cheddar" version = "0.1.0" dependencies = [ + "clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)", "comrak 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntect 4.2.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/tools/cheddar/Cargo.toml b/tools/cheddar/Cargo.toml index ca9ed0dfe4..b7d9a77586 100644 --- a/tools/cheddar/Cargo.toml +++ b/tools/cheddar/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" comrak = "0.7" lazy_static = "1.4" syntect = "4.2.0" +clap = "2.33" diff --git a/tools/cheddar/src/main.rs b/tools/cheddar/src/main.rs index 01e6c868f7..9e16b1de43 100644 --- a/tools/cheddar/src/main.rs +++ b/tools/cheddar/src/main.rs @@ -1,3 +1,4 @@ +use clap::{Arg, App}; use comrak::arena_tree::Node; use comrak::nodes::{Ast, AstNode, NodeValue, NodeCodeBlock, NodeHtmlBlock}; use comrak::{Arena, parse_document, format_html, ComrakOptions}; @@ -72,35 +73,6 @@ struct Args { lang_override: Option<&'static str>, } -/// Parse the command-line flags passed to cheddar to determine -/// whether it is running in about-filter mode (`--about-filter`) and -/// what file extension has been supplied. -fn parse_args() -> Args { - let mut args = Args::default(); - - for (i, arg) in env::args().enumerate() { - if i == 0 { - continue; - } - - if arg == "--about-filter" { - args.about_filter = true; - continue; - } - - if let Some(lang) = (*FILENAME_OVERRIDES).get(arg.as_str()) { - args.lang_override = Some(lang); - } - - args.extension = Path::new(&arg) - .extension() - .and_then(OsStr::to_str) - .map(|s| s.to_string()); - } - - return args -} - fn should_continue(res: &io::Result) -> bool { match *res { Ok(n) => n > 0, @@ -312,7 +284,32 @@ fn format_code(args: &Args) { } fn main() { - let args = parse_args(); + // Parse the command-line flags passed to cheddar to determine + // whether it is running in about-filter mode (`--about-filter`) + // and what file extension has been supplied. + let matches = App::new("cheddar") + .about("TVL's syntax highlighter") + .arg(Arg::with_name("about-filter") + .help("Run as a cgit about-filter (renders Markdown)") + .long("about-filter") + .takes_value(false)) + .arg(Arg::with_name("filename") + .help("File to render") + .index(1)) + .get_matches(); + + let mut args = Args::default(); + args.about_filter = matches.is_present("about-filter"); + + let filename = matches.value_of("filename").expect("filename is required"); + if let Some(lang) = (*FILENAME_OVERRIDES).get(filename) { + args.lang_override = Some(lang); + } + + args.extension = Path::new(&filename) + .extension() + .and_then(OsStr::to_str) + .map(|s| s.to_string()); match args.extension.as_ref().map(String::as_str) { Some("md") if args.about_filter => format_markdown(), -- cgit 1.4.1