Skip to content

Commit

Permalink
feat: add version options (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
lamchau authored Sep 19, 2024
1 parent 71863a1 commit 4ea82e8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/goose/cli/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)


Expand All @@ -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")
Expand Down
30 changes: 30 additions & 0 deletions tests/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4ea82e8

Please sign in to comment.