compose-viz/compose_viz/cli.py

61 lines
1.3 KiB
Python
Raw Permalink Normal View History

2022-05-08 00:42:14 +08:00
from typing import Optional
2022-05-18 23:28:18 +08:00
import typer
2022-05-08 00:42:14 +08:00
from compose_viz import __app_name__, __version__
2022-05-17 00:57:14 +08:00
from compose_viz.graph import Graph
2022-05-27 14:17:04 +08:00
from compose_viz.models.viz_formats import VizFormats
2022-05-18 23:28:18 +08:00
from compose_viz.parser import Parser
2022-05-08 00:42:14 +08:00
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-26 11:06:06 +08:00
output_filename: str = typer.Option(
"compose-viz",
"--output-filename",
2022-05-08 00:42:14 +08:00
"-o",
2022-05-26 11:06:06 +08:00
help="Output filename for the generated visualization file.",
2022-05-08 00:42:14 +08:00
),
2022-05-26 11:06:06 +08:00
format: VizFormats = typer.Option(
"png",
2022-05-08 00:42:14 +08:00
"--format",
"-m",
2022-05-15 19:18:10 +08:00
help="Output format for the generated visualization file.",
2022-05-08 00:42:14 +08:00
),
_: Optional[bool] = typer.Option(
None,
"--version",
"-v",
2022-05-15 19:18:10 +08:00
help="Show the version of compose-viz.",
2022-05-08 00:42:14 +08:00
callback=_version_callback,
is_eager=True,
2022-05-18 23:28:18 +08:00
),
2022-05-08 00:42:14 +08:00
) -> None:
parser = Parser()
compose = parser.parse(input_path)
if compose:
typer.echo(f"Successfully parsed {input_path}")
2022-05-26 11:06:06 +08:00
Graph(compose, output_filename).render(format)
2022-05-17 00:57:14 +08:00
2022-05-08 00:42:14 +08:00
raise typer.Exit()
2022-05-08 23:03:16 +08:00
def start_cli() -> None:
2022-05-15 19:18:10 +08:00
app(prog_name="cpv")