Coverage for piemap/cli.py: 100.00%

34 statements  

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

1"""CLI interface for single view visualization mapping parameter values to an area scale resembling a (quality) pie.""" 

2 

3import argparse 

4import sys 

5 

6import piemap.api as api 

7from piemap import APP_ALIAS, APP_NAME, KNOWN_FORMATS, parse_csl 

8 

9 

10def parse_request(argv: list[str]) -> int | argparse.Namespace: 

11 """DRY.""" 

12 parser = argparse.ArgumentParser( 

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

14 ) 

15 parser.add_argument( 

16 '--input-path', 

17 '-i', 

18 dest='input_path', 

19 default='', 

20 help='the data (format DSL) to create the quality pie. Optional\n(default: positional input path value)', 

21 required=False, 

22 ) 

23 parser.add_argument( 

24 'input_path_pos', 

25 nargs='?', 

26 default='', 

27 help='path to data file.', 

28 ) 

29 parser.add_argument( 

30 '--out-path', 

31 '-o', 

32 dest='out_path', 

33 default='piemap', 

34 help='output file path for graphic (default: piemap.png)', 

35 ) 

36 parser.add_argument( 

37 '--formats', 

38 '-f', 

39 dest='format_type_csl', 

40 default='png', 

41 help='formats (jpeg, png, svg) as comma separated list for taxonomy (default: png)', 

42 ) 

43 parser.add_argument( 

44 '--dry-run', 

45 '-d', 

46 dest='dry_run', 

47 default=False, 

48 action='store_true', 

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

50 ) 

51 parser.add_argument( 

52 '--quiet', 

53 '-q', 

54 dest='quiet', 

55 default=False, 

56 action='store_true', 

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

58 ) 

59 parser.add_argument( 

60 '--verbose', 

61 '-v', 

62 dest='verbose', 

63 default=False, 

64 action='store_true', 

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

66 ) 

67 if not argv: 

68 parser.print_help() 

69 return 0 

70 

71 options = parser.parse_args(argv) 

72 

73 if options.verbose and options.quiet: 

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

75 

76 format_types = parse_csl(options.format_type_csl) 

77 for fmt in format_types: 

78 if fmt not in KNOWN_FORMATS: 

79 parser.error(f'requested format {fmt} for quality pie not in {KNOWN_FORMATS}') 

80 

81 if not options.input_path: 

82 if options.input_path_pos: 

83 options.input_path = options.input_path_pos 

84 else: 

85 options.input_path = 'piemap.dsl' 

86 

87 return options 

88 

89 

90def main(argv: list[str] | None = None) -> int: 

91 """Delegate processing to functional module.""" 

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

93 options = parse_request(argv) 

94 if isinstance(options, int): 

95 return 0 

96 return api.main(options)