From 52d287207c123f3327866bce90411cc764b21221 Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Sat, 4 Jan 2025 11:33:53 -0800 Subject: [PATCH] CLI: `version` -> `--version` (#43) * CLI: `version` -> `--version` * Test the CLI --- ptah/cli/__init__.py | 28 ++++++++++++++++++++-------- pyproject.toml | 2 +- tests/cli/test_cli.py | 12 ++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/ptah/cli/__init__.py b/ptah/cli/__init__.py index ff7f9ef..e815de8 100644 --- a/ptah/cli/__init__.py +++ b/ptah/cli/__init__.py @@ -24,6 +24,26 @@ app = typer.Typer(pretty_exceptions_enable=False) +def version(value: bool): + """ + Current version of the Ptah CLI. + """ + if value: + print(get(Version).version()) + raise typer.Exit() + + +@app.callback() +def common( + ctx: typer.Context, + version: bool = typer.Option(None, "--version", callback=version), +): + """ + https://stackoverflow.com/a/71008105 + """ + pass + + @app.command() def project(output: Serialization = Serialization.yaml): """ @@ -39,14 +59,6 @@ def project(output: Serialization = Serialization.yaml): print(serialized) -@app.command() -def version(): - """ - Current version of the Ptah CLI. - """ - print(get(Version).version()) - - @app.command(name="build") def _build(): """ diff --git a/pyproject.toml b/pyproject.toml index 792913e..ff4e5f1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "ptah-cli" readme = "README.md" -version = "0.5.2" +version = "0.6.0" authors = [ { name = "Dan Miller", email = "daniel.keegan.miller@gmail.com" } ] diff --git a/tests/cli/test_cli.py b/tests/cli/test_cli.py index 4557767..3798175 100644 --- a/tests/cli/test_cli.py +++ b/tests/cli/test_cli.py @@ -1,7 +1,19 @@ +import re + from click import BaseCommand +from typer.testing import CliRunner + +from ptah.cli import app def test_cli_click_object(): from ptah.cli import ptah assert isinstance(ptah, BaseCommand) + + +def test_cli_version(): + runner = CliRunner() + result = runner.invoke(app, ["--version"]) + assert result.exit_code == 0 + assert re.match(r"\d+\.\d+\.\d+", result.stdout)