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 55
66import io
77import sys
8- from argparse import ArgumentParser
8+ from argparse import ArgumentError , ArgumentParser
99
1010sys .stdout = io .TextIOWrapper (sys .stdout .buffer , encoding = "utf-8" )
1111sys .stderr = io .TextIOWrapper (sys .stderr .buffer , encoding = "utf-8" )
@@ -23,6 +23,14 @@ def make_usage(command: str) -> dict:
2323
2424
2525def 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+ """
2633 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 1313
1414
1515class CliTestCase (unittest .TestCase ):
16+ def test_cli (self ):
17+ with self .assertRaises ((ArgumentError , SystemExit )):
18+ cli .exit_if_empty ("" , None )
19+
1620 def test_cli_main (self ):
1721 # call with no argument, should exit with 2
1822 with self .assertRaises (SystemExit ) as ex :
1923 __main__ .main ()
2024 self .assertEqual (ex .exception .code , 2 )
2125
2226 with self .assertRaises ((ArgumentError , SystemExit )):
23- self . assertIsNone ( __main__ .main (["thainlp" ]) )
27+ __main__ .main (["thainlp" ])
2428
2529 with self .assertRaises ((ArgumentError , SystemExit )):
26- self .assertIsNone (
27- __main__ .main (["thainlp" , "NOT_EXIST" , "command" ])
28- )
30+ __main__ .main (["thainlp" , "NOT_EXIST" , "command" ])
2931
3032 self .assertIsNone (__main__ .main (["thainlp" , "data" , "path" ]))
3133
You can’t perform that action at this time.
0 commit comments