diff --git a/src/specify_cli/__init__.py b/src/specify_cli/__init__.py index 5651ac722..d9bfe0627 100644 --- a/src/specify_cli/__init__.py +++ b/src/specify_cli/__init__.py @@ -487,8 +487,21 @@ def show_banner(): console.print(Align.center(Text(TAGLINE, style="italic bright_yellow"))) console.print() +def _version_callback(value: bool): + """Print CLI version and exit.""" + if value: + ver = get_speckit_version() + print(f"specify {ver}") + raise typer.Exit() + + @app.callback() -def callback(ctx: typer.Context): +def callback( + ctx: typer.Context, + version: Optional[bool] = typer.Option( + None, "--version", "-V", help="Show version and exit.", callback=_version_callback, is_eager=True + ), +): """Show banner when no subcommand is provided.""" if ctx.invoked_subcommand is None and "--help" not in sys.argv and "-h" not in sys.argv: show_banner() diff --git a/tests/test_ai_skills.py b/tests/test_ai_skills.py index 3eec4a419..26a3cbf0c 100644 --- a/tests/test_ai_skills.py +++ b/tests/test_ai_skills.py @@ -632,6 +632,49 @@ def test_ai_skills_flag_appears_in_help(self): assert "agent skills" in plain.lower() +class TestVersionFlag: + """Test --version flag (GitHub issue #486).""" + + def test_version_flag_exits_zero(self): + """--version should exit with code 0.""" + from typer.testing import CliRunner + + runner = CliRunner() + result = runner.invoke(app, ["--version"]) + + assert result.exit_code == 0 + + def test_version_flag_prints_version(self): + """--version should print 'specify '.""" + from typer.testing import CliRunner + + runner = CliRunner() + result = runner.invoke(app, ["--version"]) + + assert result.output.strip().startswith("specify ") + + def test_short_version_flag(self): + """-V should behave the same as --version.""" + from typer.testing import CliRunner + + runner = CliRunner() + result = runner.invoke(app, ["-V"]) + + assert result.exit_code == 0 + assert result.output.strip().startswith("specify ") + + def test_version_flag_appears_in_help(self): + """--version and -V should appear in the help output.""" + from typer.testing import CliRunner + + runner = CliRunner() + result = runner.invoke(app, ["--help"]) + + plain = re.sub(r'\x1b\[[0-9;]*m', '', result.output) + assert "--version" in plain + assert "-V" in plain + + class TestParameterOrderingIssue: """Test fix for GitHub issue #1641: parameter ordering issues."""