Coverage for tekstialue/cli.py: 61.40%
41 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-05 19:20:46 +00:00
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-05 19:20:46 +00:00
1import argparse
2import pathlib
3import sys
4from typing import List, Union
6import tekstialue.tekstialue as api
7from tekstialue import APP_ALIAS, APP_NAME, DEFAULT_CONFIG_NAME, log
10def parse_request(argv: List[str]) -> Union[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 '--config',
17 '-c',
18 dest='cfg_file',
19 default='',
20 help=f'Configuration file to read column defs from. Optional\n(default: {DEFAULT_CONFIG_NAME})',
21 required=False,
22 )
23 parser.add_argument(
24 '--input',
25 '-i',
26 dest='in_file',
27 default='',
28 help='File to transform from. Optional\n(default: positional in-file (first) value)',
29 required=False,
30 )
31 parser.add_argument(
32 'in_file_pos',
33 nargs='?',
34 default='',
35 help='File to transform from. Optional\n(default: value of input option)',
36 )
37 parser.add_argument(
38 '--output',
39 '-o',
40 dest='out_file',
41 default='',
42 help='File to transform to. Optional\n(default: in-file.out)',
43 required=False,
44 )
45 parser.add_argument(
46 '--from',
47 '-f',
48 dest='from_format',
49 default='latex',
50 help='format to transform from (default: latex)',
51 required=False,
52 )
53 parser.add_argument(
54 '--to',
55 '-t',
56 dest='to_format',
57 default='latex',
58 help='format to transform to (default: latex)',
59 required=False,
60 )
61 parser.add_argument(
62 '--verbose',
63 '-v',
64 dest='verbose',
65 default=False,
66 action='store_true',
67 help='work logging more information along the way (default: False)',
68 )
69 parser.add_argument(
70 '--debug',
71 '-d',
72 dest='debug',
73 default=False,
74 action='store_true',
75 help='be even more vrbose to support debugging (default: False)',
76 )
77 if not argv:
78 parser.print_help()
79 return 0
81 options = parser.parse_args(argv)
83 if not options.in_file: 83 ↛ 84line 83 didn't jump to line 84, because the condition on line 83 was never true
84 if options.in_file_pos:
85 options.in_file = options.in_file_pos
87 if not options.out_file: 87 ↛ 90line 87 didn't jump to line 90, because the condition on line 87 was never false
88 options.out_file = f'{options.in_file}.out'
90 if options.from_format != 'latex': 90 ↛ 91line 90 didn't jump to line 91, because the condition on line 90 was never true
91 log.error(f'unsupported from format {options.from_format} - only latex currently supported')
92 return 2
94 if options.to_format != 'latex': 94 ↛ 95line 94 didn't jump to line 95, because the condition on line 94 was never true
95 log.error(f'unsupported to format {options.to_format} - only latex currently supported')
96 return 2
98 if pathlib.Path(options.in_file).is_file(): 98 ↛ 101line 98 didn't jump to line 101, because the condition on line 98 was never false
99 return options
101 log.error(f'input {options.in_file} does not exist or is no file')
102 return 1
105def main(argv: Union[List[str], None] = None) -> int:
106 """Delegate processing to functional module."""
107 argv = sys.argv[1:] if argv is None else argv
108 options = parse_request(argv)
109 if isinstance(options, int):
110 return options
111 return api.main(options) # type: ignore