From a742b658d68f1c62ba137cb944b0f396dfd7efc6 Mon Sep 17 00:00:00 2001 From: Thibaud Gambier Date: Wed, 25 Jun 2025 23:30:02 +0200 Subject: [PATCH] Add no_subcommands option --- README.md | 1 + mkdocs_click/_docs.py | 6 ++++++ mkdocs_click/_extension.py | 2 ++ tests/test_docs.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/README.md b/README.md index 501e931..a46ef24 100644 --- a/README.md +++ b/README.md @@ -142,3 +142,4 @@ Options: - `show_hidden`: _(Optional, default: `False`)_ Show commands and options that are marked as hidden. - `list_subcommands`: _(Optional, default: `False`)_ List subcommands of a given command. If _attr_list_ is installed, add links to subcommands also. +- `no_subcommands`: _(Optional, default: `False`)_ Whether subcommands should also be shown. diff --git a/mkdocs_click/_docs.py b/mkdocs_click/_docs.py index fd84b46..7749632 100644 --- a/mkdocs_click/_docs.py +++ b/mkdocs_click/_docs.py @@ -25,6 +25,7 @@ def make_command_docs( show_hidden: bool = False, list_subcommands: bool = False, has_attr_list: bool = False, + no_subcommands: bool = False, ) -> Iterator[str]: """Create the Markdown lines for a command and its sub-commands.""" for line in _recursively_make_command_docs( @@ -36,6 +37,7 @@ def make_command_docs( show_hidden=show_hidden, list_subcommands=list_subcommands, has_attr_list=has_attr_list, + no_subcommands=no_subcommands, ): if line.strip() == "\b": continue @@ -53,6 +55,7 @@ def _recursively_make_command_docs( show_hidden: bool = False, list_subcommands: bool = False, has_attr_list: bool = False, + no_subcommands: bool = False, ) -> Iterator[str]: """Create the raw Markdown lines for a command and its sub-commands.""" ctx = _build_command_context(prog_name=prog_name, command=command, parent=parent) @@ -71,6 +74,9 @@ def _recursively_make_command_docs( subcommands.sort(key=lambda cmd: str(cmd.name)) + if no_subcommands: + return + if list_subcommands: yield from _make_subcommands_links( subcommands, diff --git a/mkdocs_click/_extension.py b/mkdocs_click/_extension.py index 887f030..4ad4e01 100644 --- a/mkdocs_click/_extension.py +++ b/mkdocs_click/_extension.py @@ -31,6 +31,7 @@ def replace_command_docs(has_attr_list: bool = False, **options: Any) -> Iterato remove_ascii_art = options.get("remove_ascii_art", False) show_hidden = options.get("show_hidden", False) list_subcommands = options.get("list_subcommands", False) + no_subcommands = options.get("no_subcommands", False) command_obj = load_command(module, command) @@ -45,6 +46,7 @@ def replace_command_docs(has_attr_list: bool = False, **options: Any) -> Iterato show_hidden=show_hidden, list_subcommands=list_subcommands, has_attr_list=has_attr_list, + no_subcommands=no_subcommands, ) diff --git a/tests/test_docs.py b/tests/test_docs.py index 3bfe38c..bb4148c 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -407,3 +407,35 @@ def _test_cmd(hidden, normal): assert opt_hidden.hidden assert not opt_normal.hidden + + +def test_no_subcommands(): + @click.group() + def _test_group(name="group"): + """Test group.""" + + @_test_group.command(name="hello") + def _test_cmd(): + """Test cmd.""" + + expected = dedent( + """ + # group + + Test group. + + **Usage:** + + ```text + group [OPTIONS] COMMAND [ARGS]... + ``` + + **Options:** + + ```text + --help Show this message and exit. + ``` + """ + ).lstrip() + output = "\n".join(make_command_docs("group", _test_group, no_subcommands=True)) + assert output == expected