summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDominik Nakamura <dnaka91@gmail.com>2023-08-01 21:56:27 +0900
committerGitHub <noreply@github.com>2023-08-01 12:56:27 +0000
commite897dbffcc53f62d84188d1bdcfff00cb47fecc9 (patch)
treeade619d5f0e49494079254010682f4a48b905a75
parenta6280bf91f8b79bd13dad43cf5611ce1f689cae4 (diff)
Simplify CLI definition with derive macro (#43)
Co-authored-by: Jan-Erik Rediger <janerik@fnordig.de>
-rw-r--r--Cargo.lock20
-rw-r--r--Cargo.toml2
-rw-r--r--src/bin/mdbook-toc.rs33
3 files changed, 37 insertions, 18 deletions
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]]
@@ -249,6 +251,18 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -598,6 +612,12 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
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<Cmd>,
+}
+
+#[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::<String>("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.