@@ -9,28 +9,22 @@ mod render;
9
9
mod serve;
10
10
11
11
use crate :: error:: { Error , Result } ;
12
+ use clap:: { Parser as ClapParser , Subcommand as ClapSubcommand } ;
12
13
use oqueue:: { Color :: Red , Sequencer } ;
13
- use std:: env;
14
14
use std:: io:: { self , Write } ;
15
15
use std:: process;
16
16
17
- fn should_serve ( ) -> bool {
18
- let mut args = env:: args_os ( ) . skip ( 1 ) ;
19
-
20
- let Some ( arg) = args. next ( ) else {
21
- return false ;
22
- } ;
23
-
24
- if arg == "serve" {
25
- true
26
- } else {
27
- let _ = writeln ! (
28
- io:: stderr( ) ,
29
- "Unrecognized argument: `{}`" ,
30
- arg. to_string_lossy( )
31
- ) ;
32
- process:: exit ( 1 ) ;
33
- }
17
+ #[ derive( ClapParser , Debug ) ]
18
+ #[ command( about = "Rust Quiz" , version, author) ]
19
+ struct Opt {
20
+ #[ clap( subcommand) ]
21
+ serve : Option < Subcommand > ,
22
+ }
23
+
24
+ #[ derive( ClapSubcommand , Debug ) ]
25
+ enum Subcommand {
26
+ /// Serve website over http at localhost:8000
27
+ Serve ,
34
28
}
35
29
36
30
fn report ( result : Result < ( ) > ) {
@@ -47,10 +41,17 @@ fn report(result: Result<()>) {
47
41
48
42
#[ tokio:: main]
49
43
async fn main ( ) {
44
+ let opt = Opt :: parse ( ) ;
45
+
50
46
report ( render:: main ( ) ) ;
51
47
52
- if should_serve ( ) {
48
+ if opt . serve . is_some ( ) {
53
49
let _ = writeln ! ( io:: stderr( ) ) ;
54
50
report ( serve:: main ( ) . await ) ;
55
51
}
56
52
}
53
+
54
+ #[ test]
55
+ fn test_cli ( ) {
56
+ <Opt as clap:: CommandFactory >:: command ( ) . debug_assert ( ) ;
57
+ }
0 commit comments