Coverage for limitys/cli.py: 74.00%

36 statements  

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

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

2 

3import argparse 

4import pathlib 

5import sys 

6from typing import List, Union 

7 

8import limitys.limitys as api 

9from limitys import APP_ALIAS, APP_NAME 

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 '--language', 

19 '-l', 

20 dest='language', 

21 required=False, 

22 default='english', 

23 help='language in (english, french, german, spanish) of the documents. Optional\n(default: english)', 

24 ) 

25 parser.add_argument( 

26 '--documents', 

27 '-d', 

28 dest='documents', 

29 default='', 

30 help='file providing the keyed documents. Optional\n(default: positional documents value)', 

31 required=False, 

32 ) 

33 parser.add_argument( 

34 'documents_pos', 

35 nargs='?', 

36 default='', 

37 help='file with format in (json, yaml) providing the keyed documents. Optional\n(default: documents.yml)', 

38 ) 

39 parser.add_argument( 

40 '--out-path', 

41 '-o', 

42 dest='out_path', 

43 default=sys.stdout, 

44 help='output file path for matrix (default: STDOUT)', 

45 ) 

46 parser.add_argument( 

47 '--quiet', 

48 '-q', 

49 dest='quiet', 

50 default=False, 

51 action='store_true', 

52 help='work as quiet as possible - progress bar only if all well (default: False)', 

53 ) 

54 parser.add_argument( 

55 '--verbose', 

56 '-v', 

57 dest='verbose', 

58 default=False, 

59 action='store_true', 

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

61 ) 

62 if not argv: 

63 parser.print_help() 

64 return 0 

65 

66 options = parser.parse_args(argv) 

67 

68 if options.verbose and options.quiet: 68 ↛ 69line 68 didn't jump to line 69, because the condition on line 68 was never true

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

70 

71 if not options.documents: 

72 if options.documents_pos: 72 ↛ 75line 72 didn't jump to line 75, because the condition on line 72 was never false

73 options.documents = options.documents_pos 

74 else: 

75 options.documents = 'documents.yml' 

76 

77 documents = pathlib.Path(options.documents) 

78 if documents.exists(): 

79 if documents.is_file(): 79 ↛ 81line 79 didn't jump to line 81, because the condition on line 79 was never false

80 return options 

81 parser.error(f'requested documents file at ({documents}) is not a file') 

82 

83 parser.error(f'requested documents file at ({documents}) does not exist') 

84 

85 

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

87 """Delegate processing to functional module.""" 

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

89 options = parse_request(argv) 

90 if isinstance(options, int): 

91 return 0 

92 return api.similarity(options) # type: ignore