22use super :: Command ;
33use crate :: { Error , Result } ;
44use async_trait:: async_trait;
5- use clap:: { Arg , ArgMatches , Command as ClapCommand } ;
5+ use clap:: { Arg , ArgAction , ArgGroup , ArgMatches , Command as ClapCommand } ;
66
77/// Abstract Test Command
88///
@@ -27,12 +27,11 @@ impl Command for TestCommand {
2727 /// `test` usage
2828 fn usage ( ) -> ClapCommand {
2929 ClapCommand :: new ( "test" )
30- . about ( "Test question by id " )
30+ . about ( "Test a question " )
3131 . visible_alias ( "t" )
3232 . arg (
3333 Arg :: new ( "id" )
3434 . num_args ( 1 )
35- . required ( true )
3635 . value_parser ( clap:: value_parser!( i32 ) )
3736 . help ( "question id" ) ,
3837 )
@@ -42,18 +41,45 @@ impl Command for TestCommand {
4241 . required ( false )
4342 . help ( "custom testcase" ) ,
4443 )
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+ )
4557 }
4658
4759 /// `test` handler
4860 async fn handler ( m : & ArgMatches ) -> Result < ( ) > {
4961 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+
5178 let testcase = m. get_one :: < String > ( "testcase" ) ;
5279 let case_str: Option < String > = match testcase {
5380 Some ( case) => Option :: from ( case. replace ( "\\ n" , "\n " ) ) ,
5481 _ => None ,
5582 } ;
56- let cache = Cache :: new ( ) ?;
5783 let res = cache. exec_problem ( id, Run :: Test , case_str) . await ?;
5884
5985 println ! ( "{}" , res) ;
0 commit comments