Simplify CLI definition with derive macro (#43)
Co-authored-by: Jan-Erik Rediger <janerik@fnordig.de>
This commit is contained in:
parent
a6280bf91f
commit
e897dbffcc
20
Cargo.lock
generated
20
Cargo.lock
generated
|
@ -223,6 +223,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5fd304a20bff958a57f04c4e96a2e7594cc4490a0e809cbd48bb6437edaa452d"
|
checksum = "5fd304a20bff958a57f04c4e96a2e7594cc4490a0e809cbd48bb6437edaa452d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap_builder",
|
"clap_builder",
|
||||||
|
"clap_derive",
|
||||||
|
"once_cell",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -248,6 +250,18 @@ dependencies = [
|
||||||
"clap",
|
"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]]
|
[[package]]
|
||||||
name = "clap_lex"
|
name = "clap_lex"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
|
@ -597,6 +611,12 @@ dependencies = [
|
||||||
"http",
|
"http",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "heck"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.3.2"
|
version = "0.3.2"
|
||||||
|
|
|
@ -13,7 +13,7 @@ rust-version = "1.66"
|
||||||
mdbook = "0.4.32"
|
mdbook = "0.4.32"
|
||||||
pulldown-cmark = "0.9.3"
|
pulldown-cmark = "0.9.3"
|
||||||
log = "0.4.19"
|
log = "0.4.19"
|
||||||
clap = { version = "4.3.19", features = ["cargo"] }
|
clap = { version = "4.3.19", features = ["cargo", "derive"] }
|
||||||
serde_json = "1.0.104"
|
serde_json = "1.0.104"
|
||||||
toml = "0.7.6"
|
toml = "0.7.6"
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ extern crate mdbook;
|
||||||
extern crate mdbook_toc;
|
extern crate mdbook_toc;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
use clap::{crate_version, Arg, ArgMatches, Command};
|
use clap::{Parser, Subcommand};
|
||||||
use mdbook::errors::Error;
|
use mdbook::errors::Error;
|
||||||
use mdbook::preprocess::{CmdPreprocessor, Preprocessor};
|
use mdbook::preprocess::{CmdPreprocessor, Preprocessor};
|
||||||
use mdbook_toc::Toc;
|
use mdbook_toc::Toc;
|
||||||
|
@ -11,22 +11,24 @@ use mdbook_toc::Toc;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
pub fn make_app() -> Command {
|
#[derive(Parser)]
|
||||||
Command::new("mdbook-toc")
|
#[command(about, version)]
|
||||||
.version(crate_version!())
|
struct App {
|
||||||
.about("mdbook preprocessor to add Table of Contents")
|
#[command(subcommand)]
|
||||||
.subcommand(
|
cmd: Option<Cmd>,
|
||||||
Command::new("supports")
|
}
|
||||||
.arg(Arg::new("renderer").required(true))
|
|
||||||
.about("Check whether a renderer is supported by this preprocessor"),
|
#[derive(Subcommand)]
|
||||||
)
|
enum Cmd {
|
||||||
|
/// Check whether a renderer is supported by this preprocessor
|
||||||
|
Supports { renderer: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = make_app().get_matches();
|
let app = App::parse();
|
||||||
|
|
||||||
if let Some(sub_args) = matches.subcommand_matches("supports") {
|
if let Some(Cmd::Supports { renderer }) = app.cmd {
|
||||||
handle_supports(sub_args);
|
handle_supports(&renderer);
|
||||||
} else if let Err(e) = handle_preprocessing() {
|
} else if let Err(e) = handle_preprocessing() {
|
||||||
eprintln!("{e}");
|
eprintln!("{e}");
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
|
@ -51,10 +53,7 @@ fn handle_preprocessing() -> Result<(), Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_supports(sub_args: &ArgMatches) -> ! {
|
fn handle_supports(renderer: &str) -> ! {
|
||||||
let renderer = sub_args
|
|
||||||
.get_one::<String>("renderer")
|
|
||||||
.expect("Required argument");
|
|
||||||
let supported = Toc.supports_renderer(renderer);
|
let supported = Toc.supports_renderer(renderer);
|
||||||
|
|
||||||
// Signal whether the renderer is supported by exiting with 1 or 0.
|
// Signal whether the renderer is supported by exiting with 1 or 0.
|
||||||
|
|
Loading…
Reference in a new issue