diff --git a/src/goose/cli/main.py b/src/goose/cli/main.py index 0e266dd70..e4e112384 100644 --- a/src/goose/cli/main.py +++ b/src/goose/cli/main.py @@ -1,6 +1,6 @@ from datetime import datetime from pathlib import Path -from typing import Dict, Optional +from typing import Optional import click from rich import print @@ -17,8 +17,8 @@ def goose_cli() -> None: pass -@goose_cli.command() -def version() -> None: +@goose_cli.command(name="version") +def get_version() -> None: """Lists the version of goose and any plugins""" from importlib.metadata import entry_points, version @@ -112,7 +112,7 @@ def session_clear(keep: int) -> None: session_file.unlink() -def get_session_files() -> Dict[str, Path]: +def get_session_files() -> dict[str, Path]: return list_sorted_session_files(SESSIONS_PATH) @@ -121,9 +121,14 @@ def get_session_files() -> Dict[str, Path]: name="goose", help="AI-powered tool to assist in solving programming and operational tasks", ) +@click.option("-V", "--version", is_flag=True, help="List the version of goose and any plugins") @click.pass_context -def cli(_: click.Context, **kwargs: Dict) -> None: - pass +def cli(ctx: click.Context, version: bool, **kwargs: dict) -> None: + if version: + ctx.invoke(get_version) + ctx.exit() + elif ctx.invoked_subcommand is None: + click.echo(ctx.get_help()) all_cli_group_options = load_plugins("goose.cli.group_option") diff --git a/tests/cli/test_main.py b/tests/cli/test_main.py index 617b3d5c1..b885f7f9f 100644 --- a/tests/cli/test_main.py +++ b/tests/cli/test_main.py @@ -123,3 +123,33 @@ def test_combined_group_commands(mock_session): runner.invoke(cli, ["session", "resume", "session1", "--profile", "default"]) mock_session_class.assert_called_once_with(name="session1", profile="default") mock_session_instance.run.assert_called_once() + + +def test_version_long_option(): + runner = CliRunner() + result = runner.invoke(cli, ["--version"]) + assert result.exit_code == 0 + assert "version" in result.output.lower() + + +def test_version_short_option(): + runner = CliRunner() + result = runner.invoke(cli, ["-V"]) + assert result.exit_code == 0 + assert "version" in result.output.lower() + + +def test_version_subcommand(): + runner = CliRunner() + result = runner.invoke(cli, ["version"]) + assert result.exit_code == 0 + assert "version" in result.output.lower() + + +def test_goose_no_args_print_help(): + runner = CliRunner() + result = runner.invoke(cli, []) + assert result.exit_code == 0 + assert "Usage:" in result.output + assert "Options:" in result.output + assert "Commands:" in result.output