1
+ use std:: cell:: LazyCell ;
2
+ use std:: env:: Args ;
3
+
1
4
use getopts:: Options ;
2
5
3
6
#[ derive( Debug ) ]
@@ -8,30 +11,32 @@ pub struct Config {
8
11
pub template : String ,
9
12
}
10
13
11
- /// Create [`Config`] from a vector of command-line arguments.
12
- pub fn parse_config ( args : Vec < String > ) -> Config {
14
+ /// Create [`Config`] from an iterator of command-line arguments.
15
+ pub fn parse_config ( mut args : Args ) -> Option < Config > {
13
16
let mut opts = Options :: new ( ) ;
14
17
opts. reqopt ( "" , "doc-dir" , "Path to the documentation output directory." , "PATH" )
15
18
. reqopt ( "" , "template" , "Path to the input template file." , "PATH" )
16
19
. optflag ( "h" , "help" , "Show this message." ) ;
17
20
18
- let ( argv0, args_) = args. split_first ( ) . unwrap ( ) ;
19
- if args. len ( ) == 1 {
20
- let message = format ! ( "Usage: {} <doc-dir> <template>" , argv0) ;
21
- println ! ( "{}" , opts. usage( & message) ) ;
22
- std:: process:: exit ( 1 ) ;
21
+ let argv0 = args. next ( ) . unwrap ( ) ;
22
+ let usage = & * LazyCell :: new ( || opts. usage ( & format ! ( "Usage: {argv0} <doc-dir> <template>" ) ) ) ;
23
+
24
+ if args. len ( ) == 0 {
25
+ print ! ( "{usage}" ) ;
26
+
27
+ return None ;
23
28
}
24
29
25
- let matches = opts. parse ( args_ ) . unwrap ( ) ;
30
+ let matches = opts. parse ( args ) . unwrap ( ) ;
26
31
27
32
if matches. opt_present ( "h" ) || matches. opt_present ( "help" ) {
28
- let message = format ! ( "Usage: {} <doc-dir> <template>" , argv0 ) ;
29
- println ! ( "{}" , opts . usage ( & message ) ) ;
30
- std :: process :: exit ( 1 ) ;
33
+ print ! ( "{usage}" ) ;
34
+
35
+ return None ;
31
36
}
32
37
33
- Config {
38
+ Some ( Config {
34
39
doc_dir : matches. opt_str ( "doc-dir" ) . unwrap ( ) ,
35
40
template : matches. opt_str ( "template" ) . unwrap ( ) ,
36
- }
41
+ } )
37
42
}
0 commit comments