2
2
use super :: Command ;
3
3
use crate :: { Error , Result } ;
4
4
use async_trait:: async_trait;
5
- use clap:: { Arg , ArgMatches , Command as ClapCommand } ;
5
+ use clap:: { Arg , ArgAction , ArgGroup , ArgMatches , Command as ClapCommand } ;
6
6
7
7
/// Abstract Test Command
8
8
///
@@ -27,12 +27,11 @@ impl Command for TestCommand {
27
27
/// `test` usage
28
28
fn usage ( ) -> ClapCommand {
29
29
ClapCommand :: new ( "test" )
30
- . about ( "Test question by id " )
30
+ . about ( "Test a question " )
31
31
. visible_alias ( "t" )
32
32
. arg (
33
33
Arg :: new ( "id" )
34
34
. num_args ( 1 )
35
- . required ( true )
36
35
. value_parser ( clap:: value_parser!( i32 ) )
37
36
. help ( "question id" ) ,
38
37
)
@@ -42,18 +41,45 @@ impl Command for TestCommand {
42
41
. required ( false )
43
42
. help ( "custom testcase" ) ,
44
43
)
44
+ . arg (
45
+ Arg :: new ( "daily" )
46
+ . short ( 'd' )
47
+ . long ( "daily" )
48
+ . help ( "Test today's daily challenge" )
49
+ . action ( ArgAction :: SetTrue ) ,
50
+ )
51
+ . group (
52
+ ArgGroup :: new ( "question-id" )
53
+ . args ( [ "id" , "daily" ] )
54
+ . multiple ( false )
55
+ . required ( true ) ,
56
+ )
45
57
}
46
58
47
59
/// `test` handler
48
60
async fn handler ( m : & ArgMatches ) -> Result < ( ) > {
49
61
use crate :: cache:: { Cache , Run } ;
50
- let id: i32 = * m. get_one :: < i32 > ( "id" ) . ok_or ( Error :: NoneError ) ?;
62
+
63
+ let cache = Cache :: new ( ) ?;
64
+
65
+ let daily = m. get_one :: < bool > ( "daily" ) . unwrap_or ( & false ) ;
66
+ let daily_id = if * daily {
67
+ Some ( cache. get_daily_problem_id ( ) . await ?)
68
+ } else {
69
+ None
70
+ } ;
71
+
72
+ let id = m
73
+ . get_one :: < i32 > ( "id" )
74
+ . copied ( )
75
+ . or ( daily_id)
76
+ . ok_or ( Error :: NoneError ) ?;
77
+
51
78
let testcase = m. get_one :: < String > ( "testcase" ) ;
52
79
let case_str: Option < String > = match testcase {
53
80
Some ( case) => Option :: from ( case. replace ( "\\ n" , "\n " ) ) ,
54
81
_ => None ,
55
82
} ;
56
- let cache = Cache :: new ( ) ?;
57
83
let res = cache. exec_problem ( id, Run :: Test , case_str) . await ?;
58
84
59
85
println ! ( "{}" , res) ;
0 commit comments