Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to --version for controller CLI #114

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading