Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/test_suggest_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import pytest
import typer
import typer.core
from typer.testing import CliRunner

from tests.utils import needs_rich

runner = CliRunner()


Expand Down Expand Up @@ -96,3 +100,35 @@ def delete(): # pragma: no cover
assert result.exit_code != 0
assert "No such command" in result.output
assert "Did you mean" not in result.output


@pytest.mark.parametrize(
"use_rich",
[
pytest.param(False),
pytest.param(True, marks=needs_rich),
],
)
def test_typo_suggestion_excludes_hidden_commands(
monkeypatch: pytest.MonkeyPatch, use_rich: bool
) -> None:
monkeypatch.setattr(typer.core, "HAS_RICH", use_rich)
app = typer.Typer()

@app.command()
def secret_agent(): # pragma: no cover
typer.echo("Visible command")

@app.command(hidden=True)
def secret_admin():
typer.echo("Hidden command")

result = runner.invoke(app, ["secret-admi"])
assert result.exit_code != 0
assert "No such command" in result.output
assert "'secret-agent'" in result.output
assert "'secret-admin'" not in result.output

result = runner.invoke(app, ["secret-admin"])
assert result.exit_code == 0
assert "Hidden command" in result.output
6 changes: 5 additions & 1 deletion typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,11 @@ def resolve_command(
return self._click_resolve_command(ctx, args)
except _click.exceptions.UsageError as e:
if self.suggest_commands:
available_commands = list(self.commands.keys())
available_commands = [
name
for name, command in self.commands.items()
if not command.hidden
]
if available_commands and args:
typo = args[0]
matches = get_close_matches(typo, available_commands)
Expand Down
Loading