From 3cbe11648a336f137f2ed5d85515befc4ba11384 Mon Sep 17 00:00:00 2001 From: Christopher Krah <48338417+chrikrah@users.noreply.github.com> Date: Thu, 29 Oct 2026 16:05:23 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Omit=20hidden=20commands=20from?= =?UTF-8?q?=20`typer=20...=20utils=20docs`=20output?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com> --- tests/assets/cli/hidden_app.py | 28 ++++++++++++++++++++++++++++ tests/test_cli/test_doc.py | 24 ++++++++++++++++++++++++ typer/cli.py | 15 ++++++++------- 3 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 tests/assets/cli/hidden_app.py diff --git a/tests/assets/cli/hidden_app.py b/tests/assets/cli/hidden_app.py new file mode 100644 index 0000000000..411a61ff5f --- /dev/null +++ b/tests/assets/cli/hidden_app.py @@ -0,0 +1,28 @@ +import typer + +internal_app = typer.Typer() + + +@internal_app.command() +def rebuild(): + """ + Rebuild the index + """ + + +app = typer.Typer() +app.add_typer(internal_app, name="internal", hidden=True) + + +@app.command() +def public(): + """ + Public command + """ + + +@app.command(hidden=True) +def secret(): + """ + Secret command + """ diff --git a/tests/test_cli/test_doc.py b/tests/test_cli/test_doc.py index 1da8bb73ef..4f0529a674 100644 --- a/tests/test_cli/test_doc.py +++ b/tests/test_cli/test_doc.py @@ -56,6 +56,30 @@ def test_doc_output(tmp_path: Path): assert "Docs saved to:" in result.stdout +def test_doc_hidden_commands(): + result = subprocess.run( + [ + sys.executable, + "-m", + "coverage", + "run", + "-m", + "typer", + "tests.assets.cli.hidden_app", + "utils", + "docs", + "--name", + "hiddenapp", + ], + capture_output=True, + encoding="utf-8", + ) + assert "`hiddenapp public`" in result.stdout + assert "secret" not in result.stdout + assert "internal" not in result.stdout + assert "rebuild" not in result.stdout + + def test_doc_title_output(tmp_path: Path): out_file: Path = tmp_path / "out.md" result = subprocess.run( diff --git a/typer/cli.py b/typer/cli.py index 98f9f4ec77..974dd1ee87 100644 --- a/typer/cli.py +++ b/typer/cli.py @@ -243,21 +243,22 @@ def get_docs_for_click( docs += f"{obj.epilog}\n\n" if isinstance(obj, TyperGroup): group = obj - commands = group.list_commands(ctx) + commands = [] + for command in group.list_commands(ctx): + command_obj = group.get_command(ctx, command) + assert command_obj + if not command_obj.hidden: + commands.append(command_obj) if commands: docs += "**Commands**:\n\n" - for command in commands: - command_obj = group.get_command(ctx, command) - assert command_obj + for command_obj in commands: docs += f"* `{command_obj.name}`" command_help = command_obj.get_short_help_str() if command_help: docs += f": {_parse_html(to_parse, command_help)}" docs += "\n" docs += "\n" - for command in commands: - command_obj = group.get_command(ctx, command) - assert command_obj + for command_obj in commands: use_prefix = "" if command_name: use_prefix += f"{command_name}"