File tree Expand file tree Collapse file tree 2 files changed +17
-7
lines changed Expand file tree Collapse file tree 2 files changed +17
-7
lines changed Original file line number Diff line number Diff line change 5
5
6
6
import io
7
7
import sys
8
- from argparse import ArgumentParser
8
+ from argparse import ArgumentError , ArgumentParser
9
9
10
10
sys .stdout = io .TextIOWrapper (sys .stdout .buffer , encoding = "utf-8" )
11
11
sys .stderr = io .TextIOWrapper (sys .stderr .buffer , encoding = "utf-8" )
@@ -23,6 +23,14 @@ def make_usage(command: str) -> dict:
23
23
24
24
25
25
def exit_if_empty (command : str , parser : ArgumentParser ) -> None :
26
+ """Print help and exit if command is empty.
27
+
28
+ :param command: command from command line
29
+ :type command: str
30
+ :param parser: parser object of the app
31
+ :type parser: ArgumentParser
32
+ """
26
33
if not command :
27
- parser .print_help ()
28
- sys .exit (0 )
34
+ if parser :
35
+ parser .print_help ()
36
+ raise ArgumentError (None , "No command provided." )
Original file line number Diff line number Diff line change 13
13
14
14
15
15
class CliTestCase (unittest .TestCase ):
16
+ def test_cli (self ):
17
+ with self .assertRaises ((ArgumentError , SystemExit )):
18
+ cli .exit_if_empty ("" , None )
19
+
16
20
def test_cli_main (self ):
17
21
# call with no argument, should exit with 2
18
22
with self .assertRaises (SystemExit ) as ex :
19
23
__main__ .main ()
20
24
self .assertEqual (ex .exception .code , 2 )
21
25
22
26
with self .assertRaises ((ArgumentError , SystemExit )):
23
- self . assertIsNone ( __main__ .main (["thainlp" ]) )
27
+ __main__ .main (["thainlp" ])
24
28
25
29
with self .assertRaises ((ArgumentError , SystemExit )):
26
- self .assertIsNone (
27
- __main__ .main (["thainlp" , "NOT_EXIST" , "command" ])
28
- )
30
+ __main__ .main (["thainlp" , "NOT_EXIST" , "command" ])
29
31
30
32
self .assertIsNone (__main__ .main (["thainlp" , "data" , "path" ]))
31
33
You can’t perform that action at this time.
0 commit comments