11use std:: { env, io, path:: Path , process} ;
22
3- use clap:: ArgMatches ;
43use mdbook:: {
54 errors:: Error ,
65 preprocess:: { CmdPreprocessor , Preprocessor } ,
@@ -21,15 +20,6 @@ fn main() {
2120 . init ( ) ;
2221 }
2322
24- let matches = clap:: clap_app!( blacksmith =>
25- ( about: clap:: crate_description!( ) )
26- ( @subcommand supports =>
27- ( about: "Check whether a renderer is supported by this preprocessor" )
28- ( @arg renderer: +takes_value +required)
29- )
30- )
31- . get_matches ( ) ;
32-
3323 macro_rules! log_unwrap {
3424 ( $result: expr) => {
3525 match $result {
@@ -51,25 +41,34 @@ fn main() {
5141 Blacksmith :: new ( )
5242 } ;
5343
54- if let Some ( sub_args) = matches. subcommand_matches ( "supports" ) {
55- handle_supports ( & blacksmith, sub_args) ;
56- } else {
57- let mut update_cache = false ;
58- if blacksmith. is_stale ( CACHE_TTL_SECONDS ) {
59- blacksmith = log_unwrap ! ( Blacksmith :: init( ) ) ;
60- update_cache = true ;
61- } else {
62- log:: info!( "Using cached data in {}" , cache_file. display( ) ) ;
44+ let mut args = std:: env:: args ( ) . skip ( 1 ) ;
45+ match args. next ( ) . as_deref ( ) {
46+ Some ( "supports" ) => {
47+ let renderer = args. next ( ) . expect ( "renderer name must be second argument" ) ;
48+ handle_supports ( & blacksmith, & renderer) ;
6349 }
64- log_unwrap ! ( handle_preprocessing( & blacksmith) ) ;
65-
66- if update_cache {
67- log:: info!( "Storing the cache in {}" , cache_file. display( ) ) ;
68- log_unwrap ! ( std:: fs:: write(
69- & cache_file,
70- & log_unwrap!( serde_json:: to_vec( & blacksmith) )
71- ) ) ;
50+ Some ( arg) => {
51+ eprintln ! ( "unknown argument: {arg}" ) ;
52+ std:: process:: exit ( 1 ) ;
7253 }
54+ None => { }
55+ }
56+
57+ let mut update_cache = false ;
58+ if blacksmith. is_stale ( CACHE_TTL_SECONDS ) {
59+ blacksmith = log_unwrap ! ( Blacksmith :: init( ) ) ;
60+ update_cache = true ;
61+ } else {
62+ log:: info!( "Using cached data in {}" , cache_file. display( ) ) ;
63+ }
64+ log_unwrap ! ( handle_preprocessing( & blacksmith) ) ;
65+
66+ if update_cache {
67+ log:: info!( "Storing the cache in {}" , cache_file. display( ) ) ;
68+ log_unwrap ! ( std:: fs:: write(
69+ & cache_file,
70+ & log_unwrap!( serde_json:: to_vec( & blacksmith) )
71+ ) ) ;
7372 }
7473}
7574
@@ -94,9 +93,8 @@ fn handle_preprocessing(pre: &Blacksmith) -> Result<(), Error> {
9493 Ok ( ( ) )
9594}
9695
97- fn handle_supports ( pre : & Blacksmith , sub_args : & ArgMatches ) -> ! {
98- let renderer = sub_args. value_of ( "renderer" ) . expect ( "Required argument" ) ;
99- let supported = pre. supports_renderer ( & renderer) ;
96+ fn handle_supports ( pre : & Blacksmith , renderer : & str ) -> ! {
97+ let supported = pre. supports_renderer ( renderer) ;
10098
10199 // Signal whether the renderer is supported by exiting with 1 or 0.
102100 if supported {
0 commit comments