From 5ba00b5f58bec270feaa04a46883bc29b60bc67e Mon Sep 17 00:00:00 2001 From: Seto Balian Date: Sun, 27 Apr 2025 12:48:31 +0100 Subject: [PATCH] Add version flag --- ape_linux.py | 19 +++++++++++++++++++ tests/test_app.py | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/ape_linux.py b/ape_linux.py index 9522211..25ddadf 100644 --- a/ape_linux.py +++ b/ape_linux.py @@ -2,15 +2,24 @@ import os import subprocess +from importlib.metadata import version from typing import Annotated import openai import rich.console import typer +__version__ = version("ape_linux") + app = typer.Typer(add_completion=False, pretty_exceptions_enable=False) +def version_callback(value: bool) -> None: + if value: + typer.echo(__version__) + raise typer.Exit() + + def call_llm( api_key: str, model: str, system_prompt: str, user_prompt: str ) -> str | None: @@ -49,6 +58,16 @@ def main( help="Run the command if suggested. Dangerous!", ), ] = False, + version: Annotated[ + bool | None, + typer.Option( + "--version", + "-v", + callback=version_callback, + help="Show the version and exit.", + is_eager=True, + ), + ] = None, ): """Suggest a command for a Linux task described in QUERY. diff --git a/tests/test_app.py b/tests/test_app.py index ce22ea7..5540d80 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -52,3 +52,10 @@ def test_app_for_suggestion_with_execute(mockenv, monkeypatch): assert result.stdout == "ls\n" # careful, this will actually run assert result.stderr == "" assert result.exit_code == 0 + + +def test_app_for_version(mockenv): + result = runner.invoke(ape_linux.app, ["--version"]) + assert result.stdout == f"{ape_linux.__version__}\n" + assert result.stderr == "" + assert result.exit_code == 0