Skip to content

Commit

Permalink
Merge pull request #33 from jpmckinney/fix-list-commands-order
Browse files Browse the repository at this point in the history
fix: Do not override the command order of a custom group's list_commands()
  • Loading branch information
bckohan authored Aug 15, 2024
2 parents 48a55f4 + b2566b2 commit 40e6cbb
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 26 deletions.
5 changes: 5 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Change Log
==========

v0.3.4 (15-AUG-2024)
====================
* `list_commands order should be honored when generated nested sections for subcommands. <https://github.com/sphinx-contrib/typer/issues/36>`_


v0.3.3 (15-JUL-2024)
====================

Expand Down
22 changes: 3 additions & 19 deletions sphinxcontrib/typer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
__version__ = ".".join(str(i) for i in VERSION)
__author__ = "Brian Kohan"
__license__ = "MIT"
__copyright__ = "Copyright 2023 Brian Kohan"
__copyright__ = "Copyright 2023-2024 Brian Kohan"


SELENIUM_DEFAULT_WINDOW_WIDTH = 1920
Expand All @@ -69,24 +69,8 @@ def get_function(function: t.Union[str, t.Callable[..., t.Any]]):
return getattr(import_module(".".join(parts[0:-1])), parts[-1])


def _filter_commands(ctx: click.Context, cmd_filter: t.Optional[t.List[str]] = None):
return sorted(
[
cmd
for name, cmd in getattr(
ctx.command,
"commands",
{
name: ctx.command.get_command(ctx, name)
for name in getattr(ctx.command, "list_commands", lambda _: [])(ctx)
or cmd_filter
or []
},
).items()
if not cmd_filter or name in cmd_filter
],
key=lambda item: item.name,
)
def _filter_commands(ctx: click.Context, cmd_filter: t.List[str]):
return [ctx.command.commands[cmd_name] for cmd_name in cmd_filter]


class RenderTarget(str, Enum):
Expand Down
3 changes: 3 additions & 0 deletions tests/click/aliases/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class AliasedGroup(click.Group):
file and with a bit of magic.
"""

def list_commands(self, ctx):
return reversed(sorted(super().list_commands(ctx)))

def get_command(self, ctx, cmd_name):
# Step one: bulitin commands as normal
rv = click.Group.get_command(self, ctx, cmd_name)
Expand Down
7 changes: 6 additions & 1 deletion tests/click/completion/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from click.shell_completion import CompletionItem


@click.group()
class AlphOrderedGroup(click.Group):
def list_commands(self, ctx):
return sorted(super().list_commands(ctx))


@click.group(cls=AlphOrderedGroup)
def cli():
pass

Expand Down
7 changes: 6 additions & 1 deletion tests/click/imagepipe/imagepipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import click


@click.group(chain=True)
class AlphOrderedGroup(click.Group):
def list_commands(self, ctx):
return sorted(super().list_commands(ctx))


@click.group(cls=AlphOrderedGroup, chain=True)
def cli():
"""This script processes a bunch of images through pillow in a unix
pipe. One commands feeds into the next.
Expand Down
9 changes: 7 additions & 2 deletions tests/click/naval/naval.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import click


@click.group()
class AlphOrderedGroup(click.Group):
def list_commands(self, ctx):
return sorted(super().list_commands(ctx))


@click.group(cls=AlphOrderedGroup)
@click.version_option()
def cli():
"""Naval Fate.
Expand All @@ -12,7 +17,7 @@ def cli():
"""


@cli.group()
@cli.group(cls=AlphOrderedGroup)
def ship():
"""Manages ships."""

Expand Down
7 changes: 6 additions & 1 deletion tests/click/repo/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import click


class AlphOrderedGroup(click.Group):
def list_commands(self, ctx):
return sorted(super().list_commands(ctx))


class Repo:
def __init__(self, home):
self.home = home
Expand All @@ -23,7 +28,7 @@ def __repr__(self):
pass_repo = click.make_pass_decorator(Repo)


@click.group()
@click.group(cls=AlphOrderedGroup)
@click.option(
"--repo-home",
envvar="REPO_HOME",
Expand Down
7 changes: 6 additions & 1 deletion tests/click/termui/termui.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import click


@click.group()
class AlphOrderedGroup(click.Group):
def list_commands(self, ctx):
return sorted(super().list_commands(ctx))


@click.group(cls=AlphOrderedGroup)
def cli():
"""This script showcases different terminal UI helpers in Click."""
pass
Expand Down
3 changes: 2 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,8 @@ def test_click_ex_aliases():

bld_dir, html = build_click_example("aliases", "html")

subcommands = ["alias", "clone", "commit", "pull", "push", "status"]
# we test that list_commands order is honored
subcommands = reversed(["alias", "clone", "commit", "pull", "push", "status"])
helps = [
get_click_ex_help("aliases"),
*[get_click_ex_help("aliases", *cmd.split()) for cmd in subcommands],
Expand Down

0 comments on commit 40e6cbb

Please sign in to comment.