diff --git a/Cargo.lock b/Cargo.lock index 74997c1..ba84a12 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -223,6 +223,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fd304a20bff958a57f04c4e96a2e7594cc4490a0e809cbd48bb6437edaa452d" dependencies = [ "clap_builder", + "clap_derive", + "once_cell", ] [[package]] @@ -248,6 +250,18 @@ dependencies = [ "clap", ] +[[package]] +name = "clap_derive" +version = "4.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.26", +] + [[package]] name = "clap_lex" version = "0.5.0" @@ -597,6 +611,12 @@ dependencies = [ "http", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.2" diff --git a/Cargo.toml b/Cargo.toml index de2e6d2..7540e94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ rust-version = "1.66" mdbook = "0.4.32" pulldown-cmark = "0.9.3" log = "0.4.19" -clap = { version = "4.3.19", features = ["cargo"] } +clap = { version = "4.3.19", features = ["cargo", "derive"] } serde_json = "1.0.104" toml = "0.7.6" diff --git a/src/bin/mdbook-toc.rs b/src/bin/mdbook-toc.rs index 601b0c5..d1c8d09 100644 --- a/src/bin/mdbook-toc.rs +++ b/src/bin/mdbook-toc.rs @@ -3,7 +3,7 @@ extern crate mdbook; extern crate mdbook_toc; extern crate serde_json; -use clap::{crate_version, Arg, ArgMatches, Command}; +use clap::{Parser, Subcommand}; use mdbook::errors::Error; use mdbook::preprocess::{CmdPreprocessor, Preprocessor}; use mdbook_toc::Toc; @@ -11,22 +11,24 @@ use mdbook_toc::Toc; use std::io; use std::process; -pub fn make_app() -> Command { - Command::new("mdbook-toc") - .version(crate_version!()) - .about("mdbook preprocessor to add Table of Contents") - .subcommand( - Command::new("supports") - .arg(Arg::new("renderer").required(true)) - .about("Check whether a renderer is supported by this preprocessor"), - ) +#[derive(Parser)] +#[command(about, version)] +struct App { + #[command(subcommand)] + cmd: Option, +} + +#[derive(Subcommand)] +enum Cmd { + /// Check whether a renderer is supported by this preprocessor + Supports { renderer: String }, } fn main() { - let matches = make_app().get_matches(); + let app = App::parse(); - if let Some(sub_args) = matches.subcommand_matches("supports") { - handle_supports(sub_args); + if let Some(Cmd::Supports { renderer }) = app.cmd { + handle_supports(&renderer); } else if let Err(e) = handle_preprocessing() { eprintln!("{e}"); process::exit(1); @@ -51,10 +53,7 @@ fn handle_preprocessing() -> Result<(), Error> { Ok(()) } -fn handle_supports(sub_args: &ArgMatches) -> ! { - let renderer = sub_args - .get_one::("renderer") - .expect("Required argument"); +fn handle_supports(renderer: &str) -> ! { let supported = Toc.supports_renderer(renderer); // Signal whether the renderer is supported by exiting with 1 or 0.