Skip to content

Commit 3683bf4

Browse files
committed
default cmd bug fix, how to work
1 parent 578f5da commit 3683bf4

File tree

5 files changed

+120
-27
lines changed

5 files changed

+120
-27
lines changed

django_typer/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,8 +3030,13 @@ def handle(self, option1: bool, option2: bool):
30303030
:param kwargs: the options to directly pass to handle()
30313031
"""
30323032
with self:
3033-
if getattr(self, "_handle", None) and callable(self._handle):
3034-
return self._handle(*args, **kwargs)
3033+
handle = getattr(self, "_handle", None) or getattr(
3034+
self.typer_app,
3035+
"handle",
3036+
None, # registered dynamically
3037+
)
3038+
if callable(handle):
3039+
return handle(*args, **kwargs)
30353040
raise NotImplementedError(
30363041
_(
30373042
"{cls} does not implement handle(), you must call the other command "

django_typer/tests/apps/test_app/management/commands/howto2.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
class Command(TyperCommand):
55
@command(name="subcommand1")
6-
def handle(self): ...
6+
def handle(self):
7+
return "handle"
78

89
@command()
9-
def subcommand2(self): ...
10+
def subcommand2(self):
11+
return "subcommand2"
1012

1113
@command()
12-
def subcommand3(self): ...
14+
def subcommand3(self):
15+
return "subcommand3"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django_typer import Typer
2+
3+
app = Typer()
4+
5+
6+
@app.command(name="subcommand1")
7+
def handle():
8+
return "handle"
9+
10+
11+
@app.command()
12+
def subcommand2():
13+
return "subcommand2"
14+
15+
16+
@app.command()
17+
def subcommand3():
18+
return "subcommand3"

django_typer/tests/test_howto.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from django.test import TestCase
2+
from django_typer import get_command
3+
4+
5+
class TestDefaultCmdHowto(TestCase):
6+
cmd = "howto2"
7+
8+
def test_default_howto(self):
9+
from django_typer.tests.apps.test_app.management.commands.howto2 import Command
10+
11+
command = get_command(self.cmd, Command)
12+
self.assertEqual(command(), "handle")
13+
self.assertEqual(command.subcommand2(), "subcommand2")
14+
self.assertEqual(command.subcommand3(), "subcommand3")
15+
16+
with self.assertRaises(Exception):
17+
command.handle()
18+
19+
self.assertEqual(command.subcommand1(), "handle")
20+
21+
22+
class TestDefaultCmdTyperHowto(TestDefaultCmdHowto):
23+
cmd = "howto2_typer"

doc/source/howto.rst

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,41 @@ Define Multiple Subcommands
9191

9292
Commands with a single executable function should simply implement handle(), but if you would
9393
like have multiple subcommands you can define any number of functions decorated with
94-
:func:`~django_typer.command`:
94+
:func:`~django_typer.command` or :func:`~django_typer.Typer.command`:
9595

96-
.. code-block:: python
96+
.. tabs::
9797

98-
from django_Typer import TyperCommand, command
98+
.. tab:: Django-style
9999

100-
class Command(TyperCommand):
100+
.. code-block:: python
101101
102-
@command()
103-
def subcommand1(self):
104-
...
102+
from django_typer import TyperCommand, command
105103
106-
@command()
107-
def subcommand2(self):
108-
...
104+
class Command(TyperCommand):
105+
106+
@command()
107+
def subcommand1(self):
108+
...
109+
110+
@command()
111+
def subcommand2(self):
112+
...
113+
114+
.. tab:: Typer-style
115+
116+
.. code-block:: python
117+
118+
from django_typer import Typer
119+
120+
app = Typer()
121+
122+
@app.command()
123+
def subcommand1():
124+
...
125+
126+
@app.command()
127+
def subcommand2():
128+
...
109129
110130
.. note::
111131

@@ -133,23 +153,47 @@ whatever we want the command to be. For example to define three subcommands but
133153
default we can do this:
134154

135155

136-
.. code-block:: python
156+
.. tabs::
137157

138-
from django_typer import TyperCommand, command
158+
.. tab:: Django-style
139159

140-
class Command(TyperCommand):
160+
.. code-block:: python
141161
142-
@command(name='subcommand1')
143-
def handle(self):
144-
...
162+
from django_typer import TyperCommand, command
145163
146-
@command()
147-
def subcommand2(self):
148-
...
164+
class Command(TyperCommand):
149165
150-
@command()
151-
def subcommand3(self):
152-
...
166+
@command(name='subcommand1')
167+
def handle(self):
168+
...
169+
170+
@command()
171+
def subcommand2(self):
172+
...
173+
174+
@command()
175+
def subcommand3(self):
176+
...
177+
178+
.. tab:: Typer-style
179+
180+
.. code-block:: python
181+
182+
from django_typer import Typer
183+
184+
app = Typer()
185+
186+
@app.command(name='subcommand1')
187+
def handle():
188+
...
189+
190+
@app.command()
191+
def subcommand2():
192+
...
193+
194+
@app.command()
195+
def subcommand3():
196+
...
153197
154198
155199
.. code-block:: python

0 commit comments

Comments
 (0)