Skip to content

Commit

Permalink
Change to --version
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelldls authored and GDYendell committed Jan 27, 2025
1 parent 2f91987 commit cde6ee4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 18 additions & 8 deletions src/fastcs/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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


Expand Down
4 changes: 2 additions & 2 deletions tests/test_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ 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


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

Expand Down

0 comments on commit cde6ee4

Please sign in to comment.