Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maxdepth option to limit subcommand nesting #109

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 17 additions & 3 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ class ClickDirective(rst.Directive):
option_spec = {
'prog': directives.unchanged_required,
'nested': nested,
'maxdepth': directives.unchanged_required,
'commands': directives.unchanged,
'show-nested': directives.flag,
}
Expand Down Expand Up @@ -432,8 +433,10 @@ def _generate_nodes(
command: click.Command,
parent: ty.Optional[click.Context],
nested: str,
maxdepth: int,
commands: ty.Optional[ty.List[str]] = None,
semantic_group: bool = False,
current_depth: int = 0,
) -> ty.List[nodes.section]:
"""Generate the relevant Sphinx nodes.

Expand All @@ -443,6 +446,7 @@ def _generate_nodes(
:param command: Instance of `click.Group` or `click.Command`
:param parent: Instance of `click.Context`, or None
:param nested: The granularity of subcommand details.
:param maxdepth: The maximum depth of recursive subcommands to display.
:param commands: Display only listed commands or skip the section if
empty
:param semantic_group: Display command as title and description for
Expand Down Expand Up @@ -480,7 +484,7 @@ def _generate_nodes(

# Subcommands

if nested == NESTED_FULL:
if (maxdepth < 1 or current_depth < maxdepth) and nested == NESTED_FULL:
if isinstance(command, click.CommandCollection):
for source in command.sources:
section.extend(
Expand All @@ -489,7 +493,9 @@ def _generate_nodes(
source,
parent=ctx,
nested=nested,
maxdepth=maxdepth,
semantic_group=True,
current_depth=current_depth + 1,
)
)
else:
Expand All @@ -498,7 +504,12 @@ def _generate_nodes(
parent = ctx if not semantic_group else ctx.parent
section.extend(
self._generate_nodes(
command.name, command, parent=parent, nested=nested
command.name,
command,
parent=parent,
nested=nested,
maxdepth=maxdepth,
current_depth=current_depth + 1,
)
)

Expand All @@ -515,6 +526,7 @@ def run(self) -> ty.Iterable[nodes.section]:
prog_name = self.options.get('prog')
show_nested = 'show-nested' in self.options
nested = self.options.get('nested')
maxdepth = int(self.options.get('maxdepth') or 0)

if show_nested:
if nested:
Expand All @@ -534,7 +546,9 @@ def run(self) -> ty.Iterable[nodes.section]:
command.strip() for command in self.options.get('commands').split(',')
]

return self._generate_nodes(prog_name, command, None, nested, commands)
return self._generate_nodes(
prog_name, command, None, nested, maxdepth, commands
)


def setup(app: application.Sphinx) -> ty.Dict[str, ty.Any]:
Expand Down