1
1
use std:: { env, io, path:: Path , process} ;
2
2
3
- use clap:: ArgMatches ;
4
3
use mdbook:: {
5
4
errors:: Error ,
6
5
preprocess:: { CmdPreprocessor , Preprocessor } ,
@@ -21,15 +20,6 @@ fn main() {
21
20
. init ( ) ;
22
21
}
23
22
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
-
33
23
macro_rules! log_unwrap {
34
24
( $result: expr) => {
35
25
match $result {
@@ -51,25 +41,34 @@ fn main() {
51
41
Blacksmith :: new ( )
52
42
} ;
53
43
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) ;
63
49
}
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 ) ;
72
53
}
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
+ ) ) ;
73
72
}
74
73
}
75
74
@@ -94,9 +93,8 @@ fn handle_preprocessing(pre: &Blacksmith) -> Result<(), Error> {
94
93
Ok ( ( ) )
95
94
}
96
95
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) ;
100
98
101
99
// Signal whether the renderer is supported by exiting with 1 or 0.
102
100
if supported {
0 commit comments