From cde6ee40287d92c089256b71abae0d384a327027 Mon Sep 17 00:00:00 2001 From: Marcell Nagy Date: Fri, 24 Jan 2025 16:50:00 +0000 Subject: [PATCH] Change to --version --- src/fastcs/launch.py | 26 ++++++++++++++++++-------- tests/test_launch.py | 4 ++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/fastcs/launch.py b/src/fastcs/launch.py index 7d4d93c..6535437 100644 --- a/src/fastcs/launch.py +++ b/src/fastcs/launch.py @@ -2,7 +2,7 @@ import inspect import json from pathlib import Path -from typing import Annotated, TypeAlias, get_type_hints +from typing import Annotated, Optional, TypeAlias, get_type_hints import typer from pydantic import BaseModel, create_model @@ -134,8 +134,24 @@ def __init__(self, controller_class, fastcs_options): self.controller_class = controller_class self.fastcs_options = fastcs_options + def version_callback(value: bool): + if value: + if version: + print(f"{controller_class.__name__}: {version}") + print(f"FastCS: {__version__}") + raise typer.Exit() + @launch_typer.callback() - def create_context(ctx: typer.Context): + def main( + ctx: typer.Context, + version: Optional[bool] = typer.Option( # noqa (Optional required for typer) + None, + "--version", + callback=version_callback, + is_eager=True, + help=f"Display the {controller_class.__name__} version.", + ), + ): ctx.obj = LaunchContext( controller_class, fastcs_options, @@ -180,12 +196,6 @@ def run( instance.create_docs() instance.run() - @launch_typer.command(name="version", help=f"{controller_class.__name__} version") - def version_command(): - if version: - print(f"{controller_class.__name__}: {version}") - print(f"FastCS: {__version__}") - return launch_typer diff --git a/tests/test_launch.py b/tests/test_launch.py index c2732c8..8c0b928 100644 --- a/tests/test_launch.py +++ b/tests/test_launch.py @@ -108,7 +108,7 @@ def test_version(): impl_version = "0.0.1" expected = f"SingleArg: {impl_version}\nFastCS: {__version__}\n" app = _launch(SingleArg, version=impl_version) - result = runner.invoke(app, ["version"]) + result = runner.invoke(app, ["--version"]) assert result.exit_code == 0 assert result.stdout == expected @@ -116,7 +116,7 @@ def test_version(): def test_no_version(): expected = f"FastCS: {__version__}\n" app = _launch(SingleArg) - result = runner.invoke(app, ["version"]) + result = runner.invoke(app, ["--version"]) assert result.exit_code == 0 assert result.stdout == expected