Coverage for sammen/cli.py: 55.32%

35 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 22:12:00 +00:00

1"""CLI interface for overlap (Finnish: limitys) assesses sentences from constrained and overlapping vocabularies.""" 

2 

3import argparse 

4import asyncio 

5import sys 

6from typing import List, Union 

7 

8import sammen.sammen as api 

9from sammen import APP_ALIAS, APP_NAME, log 

10 

11 

12def parse_request(argv: List[str]) -> Union[int, argparse.Namespace]: 

13 """DRY.""" 

14 parser = argparse.ArgumentParser( 

15 prog=APP_ALIAS, description=APP_NAME, formatter_class=argparse.RawTextHelpFormatter 

16 ) 

17 parser.add_argument( 

18 'families', 

19 nargs='+', 

20 default='', 

21 help='comma separated lists of families.', 

22 ) 

23 parser.add_argument( 

24 '--dry-run', 

25 '-d', 

26 dest='dry_run', 

27 default=False, 

28 action='store_true', 

29 help='only log what would be done - no real actions (default: False)', 

30 ) 

31 parser.add_argument( 

32 '--quiet', 

33 '-q', 

34 dest='quiet', 

35 default=False, 

36 action='store_true', 

37 help='work as quiet as possible (default: False)', 

38 ) 

39 parser.add_argument( 

40 '--verbose', 

41 '-v', 

42 dest='verbose', 

43 default=False, 

44 action='store_true', 

45 help='work logging more information along the way (default: False)', 

46 ) 

47 if not argv: 

48 parser.print_help() 

49 return 0 

50 

51 options = parser.parse_args(argv) 

52 

53 if options.verbose and options.quiet: 

54 parser.error('you cannot be quiet and verbose at the same time') 

55 

56 if not options.families: 56 ↛ 57line 56 didn't jump to line 57, because the condition on line 56 was never true

57 parser.error('we need families to watch') 

58 

59 return options 

60 

61 

62def main(argv: Union[List[str], None] = None) -> int: 

63 """Delegate processing to functional module.""" 

64 argv = sys.argv[1:] if argv is None else argv 

65 options = parse_request(argv) 

66 if isinstance(options, int): 

67 return 0 

68 try: 

69 asyncio.run(api.main(options)) 

70 except RuntimeError: 

71 log.debug('Received run time error in CLI - terminating') 

72 except KeyboardInterrupt: 

73 log.debug('Received keyboard interrupt in CLI - terminating') 

74 except Exception as err: 

75 log.warning(f'Received {err} in CLI - terminating') 

76 return 0