compose-viz/compose_viz/cli.py

67 lines
1.4 KiB
Python
Raw Permalink Normal View History

2022-05-07 18:42:14 +02:00
from enum import Enum
from typing import Optional
2022-05-18 17:28:18 +02:00
import typer
2022-05-07 18:42:14 +02:00
from compose_viz import __app_name__, __version__
2022-05-16 18:57:14 +02:00
from compose_viz.graph import Graph
2022-05-18 17:28:18 +02:00
from compose_viz.parser import Parser
2022-05-07 18:42:14 +02:00
2022-05-16 20:49:48 +02:00
2022-05-07 18:42:14 +02:00
class VisualizationFormats(str, Enum):
png = "PNG"
dot = "DOT"
app = typer.Typer(
invoke_without_command=True,
no_args_is_help=True,
subcommand_metavar="",
add_completion=False,
)
def _version_callback(value: bool) -> None:
if value:
typer.echo(f"{__app_name__} {__version__}")
raise typer.Exit()
@app.callback()
def compose_viz(
input_path: str,
2022-05-15 13:18:10 +02:00
output_path: str = typer.Option(
"./compose-viz.png",
"--output-path",
2022-05-07 18:42:14 +02:00
"-o",
2022-05-15 13:18:10 +02:00
help="Output path for the generated visualization file.",
2022-05-07 18:42:14 +02:00
),
format: VisualizationFormats = typer.Option(
"PNG",
"--format",
"-m",
2022-05-15 13:18:10 +02:00
help="Output format for the generated visualization file.",
2022-05-07 18:42:14 +02:00
),
_: Optional[bool] = typer.Option(
None,
"--version",
"-v",
2022-05-15 13:18:10 +02:00
help="Show the version of compose-viz.",
2022-05-07 18:42:14 +02:00
callback=_version_callback,
is_eager=True,
2022-05-18 17:28:18 +02:00
),
2022-05-07 18:42:14 +02:00
) -> None:
parser = Parser()
compose = parser.parse(input_path)
if compose:
typer.echo(f"Successfully parsed {input_path}")
2022-05-16 18:57:14 +02:00
Graph(compose, output_path).render(format)
2022-05-07 18:42:14 +02:00
raise typer.Exit()
2022-05-08 17:03:16 +02:00
def start_cli() -> None:
2022-05-15 13:18:10 +02:00
app(prog_name="cpv")