Skip to content

Commit ec48f79

Browse files
committed
add howto docs for command list order in helps fix #116
1 parent 7ae5519 commit ec48f79

File tree

7 files changed

+92
-41
lines changed

7 files changed

+92
-41
lines changed

django_typer/management/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,10 @@ class Command(TyperCommand):
588588
@command(cls=CustomCommand)
589589
def handle(self):
590590
...
591+
592+
593+
See `click commands api <https://click.palletsprojects.com/en/latest/api/#commands>`_
594+
for more information.
591595
"""
592596

593597

@@ -610,6 +614,10 @@ class Command(TyperCommand):
610614
@group(cls=CustomGroup)
611615
def grp(self):
612616
...
617+
618+
See `click docs on custom groups <https://click.palletsprojects.com/en/latest/commands/#custom-groups>`_
619+
and `advanced patterns <https://click.palletsprojects.com/en/latest/advanced/>`_ for more
620+
information.
613621
"""
614622

615623
def list_commands(self, ctx: click.Context) -> t.List[str]:

doc/source/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ v2.2.3 (2024-10-xx)
1212
* Completed `Open up vulnerability reporting and add security policy. <https://github.com/django-commons/django-typer/issues/124>`_
1313
* Completed `Move architecture in docs to ARCHITECTURE.md <https://github.com/django-commons/django-typer/issues/121>`_
1414
* Completed `Transfer to django-commons <https://github.com/django-commons/django-typer/issues/117>`_
15+
* Completed `Add howto for how to change the display order of commands in help. <https://github.com/django-commons/django-typer/issues/116>`_
1516

1617
v2.2.2 (2024-08-25)
1718
====================

doc/source/howto.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,43 @@ value:
650650
Docstring will be used as help.
651651
"""
652652
653+
Order Commands in Help Text
654+
---------------------------
655+
656+
**By default commands are listed in the order they appear in the class**. You can override
657+
this by
658+
`using a custom click group <https://click.palletsprojects.com/en/latest/commands/#custom-groups>`_.
659+
660+
For example, to change the order of commands to be in reverse alphabetical order you could define
661+
a custom group and override the ``list_commands`` method. Custom group and command classes may be
662+
provided like below, but they must extend from django-typer's classes:
663+
664+
* For groups: :class:`~django_typer.management.DTGroup`
665+
* For commands: :class:`~django_typer.management.DTCommand`
666+
667+
.. tabs::
668+
669+
.. tab:: Django-style
670+
671+
.. literalinclude:: ../../tests/apps/howto/management/commands/order.py
672+
673+
.. tab:: Typer-style
674+
675+
.. literalinclude:: ../../tests/apps/howto/management/commands/order_typer.py
676+
677+
.. tabs::
678+
679+
.. tab:: Default Order
680+
681+
.. typer:: tests.apps.howto.management.commands.order_default.Command:typer_app
682+
:prog: ./manage.py order
683+
:width: 80
684+
685+
.. tab:: Alphabetized
653686

687+
.. typer:: tests.apps.howto.management.commands.order.Command:typer_app
688+
:prog: ./manage.py order
689+
:width: 80
654690

655691
Document Commands w/Sphinx
656692
--------------------------

tests/apps/howto/management/commands/order.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33
from click import Context
44

55

6-
class ReverseAlphaCommands(DTGroup):
6+
class AlphabetizeCommands(DTGroup):
77
def list_commands(self, ctx: Context) -> t.List[str]:
8-
return list(sorted(self.commands.keys(), reverse=True))
8+
return list(sorted(self.commands.keys()))
99

1010

11-
class Command(TyperCommand, cls=ReverseAlphaCommands):
12-
@command()
13-
def a(self):
14-
print("a")
15-
11+
class Command(TyperCommand, cls=AlphabetizeCommands):
1612
@command()
1713
def b(self):
1814
print("b")
1915

2016
@command()
21-
def c(self):
22-
print("c")
17+
def a(self):
18+
print("a")
2319

24-
@group(cls=ReverseAlphaCommands)
20+
@group(cls=AlphabetizeCommands)
2521
def d(self):
2622
print("d")
2723

24+
@d.command()
25+
def f(self):
26+
print("f")
27+
2828
@d.command()
2929
def e(self):
3030
print("e")
3131

32-
@d.command()
33-
def f(self):
34-
print("f")
32+
@command()
33+
def c(self):
34+
print("c")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .order import Command as Order
2+
3+
4+
class Command(Order):
5+
pass
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11
from django_typer.management import Typer, DTGroup
22
from click import Context
3+
import typing as t
34

45

5-
class ReverseAlphaCommands(DTGroup):
6-
def list_commands(self, ctx: Context) -> list[str]:
7-
return list(sorted(self.commands.keys(), reverse=True))
6+
class AlphabetizeCommands(DTGroup):
7+
def list_commands(self, ctx: Context) -> t.List[str]:
8+
return list(sorted(self.commands.keys()))
89

910

10-
app = Typer(cls=ReverseAlphaCommands)
11+
app = Typer(cls=AlphabetizeCommands)
1112

12-
d_app = Typer(cls=ReverseAlphaCommands)
13+
d_app = Typer(cls=AlphabetizeCommands)
1314
app.add_typer(d_app)
1415

1516

16-
@app.command()
17-
def a():
18-
print("a")
19-
20-
2117
@app.command()
2218
def b():
2319
print("b")
2420

2521

2622
@app.command()
27-
def c():
28-
print("c")
23+
def a():
24+
print("a")
2925

3026

31-
@d_app.callback(cls=ReverseAlphaCommands)
27+
@d_app.callback()
3228
def d():
3329
print("d")
3430

3531

32+
@d_app.command()
33+
def f():
34+
print("f")
35+
36+
3637
@d_app.command()
3738
def e():
3839
print("e")
3940

4041

41-
@d_app.command()
42-
def f():
43-
print("f")
42+
@app.command()
43+
def c():
44+
print("c")

tests/test_howto.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ class TestPrintingTyperHowto(TestPrintingHowto):
313313
class TestOrderHowTo(TestCase):
314314
cmd = "order"
315315

316-
from tests.apps.howto.management.commands.order import ReverseAlphaCommands
316+
from tests.apps.howto.management.commands.order import AlphabetizeCommands
317317

318-
grp_cls = ReverseAlphaCommands
318+
grp_cls = AlphabetizeCommands
319319

320320
def test_howto_order(self):
321321
from tests.apps.howto.management.commands.order import Command as OrderCommand
@@ -331,18 +331,18 @@ def test_howto_order(self):
331331

332332
if rich_installed:
333333
self.assertTrue(
334-
hlp.index("│ d")
335-
< hlp.index("│ c")
334+
hlp.index("│ a")
336335
< hlp.index("│ b")
337-
< hlp.index("│ a")
336+
< hlp.index("│ c")
337+
< hlp.index("│ d")
338338
)
339339
else:
340340
cmd_idx = hlp.index("Commands")
341341
self.assertTrue(
342-
hlp.index(" d", cmd_idx)
343-
< hlp.index(" c", cmd_idx)
342+
hlp.index(" a", cmd_idx)
344343
< hlp.index(" b", cmd_idx)
345-
< hlp.index(" a", cmd_idx)
344+
< hlp.index(" c", cmd_idx)
345+
< hlp.index(" d", cmd_idx)
346346
)
347347

348348
buffer.seek(0)
@@ -352,15 +352,15 @@ def test_howto_order(self):
352352
hlp = buffer.getvalue()
353353

354354
if rich_installed:
355-
self.assertTrue(hlp.index("│ f") < hlp.index("│ e"))
355+
self.assertTrue(hlp.index("│ e") < hlp.index("│ f"))
356356
else:
357357
cmd_idx = hlp.index("Commands")
358-
self.assertTrue(hlp.index(" f", cmd_idx) < hlp.index(" e", cmd_idx))
358+
self.assertTrue(hlp.index(" e", cmd_idx) < hlp.index(" f", cmd_idx))
359359

360360

361361
class TestPrintingTyperHowto(TestOrderHowTo):
362362
cmd = "order_typer"
363363

364-
from tests.apps.howto.management.commands.order_typer import ReverseAlphaCommands
364+
from tests.apps.howto.management.commands.order_typer import AlphabetizeCommands
365365

366-
grp_cls = ReverseAlphaCommands
366+
grp_cls = AlphabetizeCommands

0 commit comments

Comments
 (0)