From 3d599b34626985396eb562ad12aa9ea2585a6ca4 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 22 May 2026 17:37:20 +0200 Subject: [PATCH] feat(cli): add option to run python parser --- midas/cli/main.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/midas/cli/main.py b/midas/cli/main.py index 278abd9..3c033f0 100644 --- a/midas/cli/main.py +++ b/midas/cli/main.py @@ -3,6 +3,9 @@ from typing import Optional, TextIO import click +from midas.ast.printer import PythonAstPrinter +from midas.parser.python import PythonParser + @click.group() def midas(): @@ -22,11 +25,28 @@ def utils(): @utils.command() @click.option("-o", "--output", type=click.File("w")) +@click.option("-p", "--parse", is_flag=True) @click.argument("file", type=click.File("r")) -def dump_ast(output: Optional[TextIO], file: TextIO): +def dump_ast(output: Optional[TextIO], parse: bool, file: TextIO): source: str = file.read() tree: ast.Module = ast.parse(source, filename=file.name) - dump: str = ast.dump(tree, indent=4) + dump: str + + if parse: + parser = PythonParser() + parser.visit(tree) + printer = PythonAstPrinter() + dump = "" + for name, annotation in parser.annotations: + dump += f"{name} = " + if annotation is None: + dump += "None" + else: + dump += printer.print(annotation) + dump += "\n" + else: + dump = ast.dump(tree, indent=4) + if output is None: click.echo(dump) else: