|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import argparse |
| 16 | +import enum |
15 | 17 | import json |
16 | 18 | import sys |
17 | 19 | from collections import Counter, defaultdict |
18 | 20 | from dataclasses import dataclass |
19 | 21 | from typing import Counter as CounterType, Dict, Iterator |
20 | 22 |
|
21 | 23 |
|
| 24 | +class ErrorCodes(enum.IntEnum): |
| 25 | + DEPRECATED = 1 |
| 26 | + |
| 27 | + |
22 | 28 | def main() -> None: |
23 | | - report_errors() |
| 29 | + """ |
| 30 | + The primary entrypoint of the program. |
| 31 | +
|
| 32 | + Parses the CLI flags, and delegates to other functions as appropriate. |
| 33 | + For details of how to invoke the program, call it with `--help`. |
| 34 | + """ |
| 35 | + parser = argparse.ArgumentParser() |
| 36 | + subparsers = parser.add_subparsers(title="subcommand") |
| 37 | + |
| 38 | + parser.set_defaults(func=_no_command) |
| 39 | + |
| 40 | + parse_parser = subparsers.add_parser( |
| 41 | + "parse", help="Transform Mypy output into JSON." |
| 42 | + ) |
24 | 43 |
|
| 44 | + parse_parser.set_defaults(func=_parse_command) |
25 | 45 |
|
26 | | -def report_errors() -> None: |
| 46 | + args = sys.argv[1:] |
| 47 | + parsed = parser.parse_args(args) |
| 48 | + parsed.func(parsed) |
| 49 | + |
| 50 | + |
| 51 | +def _parse_command(args: object) -> None: |
| 52 | + """Handle the `parse` command.""" |
27 | 53 | errors = parse_errors_report(sys.stdin) |
28 | 54 | error_json = json.dumps(errors, sort_keys=True, indent=2) |
29 | 55 | print(error_json) |
30 | 56 |
|
31 | 57 |
|
| 58 | +def _no_command(args: object) -> None: |
| 59 | + """ |
| 60 | + Handle the lack of an explicit command. |
| 61 | +
|
| 62 | + This will be hit when the program is called without arguments. |
| 63 | + """ |
| 64 | + print("A subcommand is required. Pass --help for usage info.") |
| 65 | + sys.exit(ErrorCodes.DEPRECATED) |
| 66 | + |
| 67 | + |
32 | 68 | @dataclass(frozen=True) |
33 | 69 | class MypyError: |
34 | 70 | filename: str |
|
0 commit comments