Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 6 additions & 0 deletions mkdocs_click/_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions mkdocs_click/_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
)


Expand Down
32 changes: 32 additions & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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