@@ -16,7 +16,7 @@ For example to define an integer positional argument we could simply do:
16
16
...
17
17
18
18
You will likely want to add additional meta information to your arguments for Typer _ to render
19
- things like helps and usage strings. You can do this by annotating the type hint with
19
+ things like helps and usage strings. You can do this by annotating the type hint with
20
20
the `typer.Argument ` class:
21
21
22
22
.. code-block ::
@@ -25,7 +25,7 @@ the `typer.Argument` class:
25
25
from typer import Argument
26
26
27
27
# ...
28
-
28
+
29
29
def handle(self, int_arg: t.Annotated[int, Argument(help="An integer argument")]):
30
30
...
31
31
@@ -75,12 +75,12 @@ To add meta information, we annotate with the `typer.Option` class:
75
75
from typer import Option
76
76
77
77
# ...
78
-
78
+
79
79
def handle(self, name: t.Annotated[str, Option(help="The name of the thing")]):
80
80
...
81
81
82
82
.. tip ::
83
-
83
+
84
84
Refer to the Typer _ docs on options _ for more details.
85
85
86
86
@@ -248,11 +248,11 @@ This is like defining a group at the command root and is an extension of the
248
248
Call TyperCommands from Code
249
249
----------------------------
250
250
251
- There are two options for invoking a :class: `~django_typer.TyperCommand ` from code without spawning off
252
- a subprocess. The first is to use Django _'s builtin call_command _ function. This function will work exactly
253
- as it does for normal BaseCommand _ derived commands. django-typer _ however adds another mechanism that
254
- can be more efficient, especially if your options and arguments are already of the correct type and require
255
- no parsing:
251
+ There are two options for invoking a :class: `~django_typer.TyperCommand ` from code without spawning
252
+ off a subprocess. The first is to use Django _'s builtin call_command _ function. This function will
253
+ work exactly as it does for normal BaseCommand _ derived commands. django-typer _ however adds
254
+ another mechanism that can be more efficient, especially if your options and arguments are already
255
+ of the correct type and require no parsing:
256
256
257
257
Say we have this command, called ``mycommand ``:
258
258
@@ -287,8 +287,8 @@ Say we have this command, called ``mycommand``:
287
287
The rule of them is this:
288
288
289
289
- Use call_command _ if your options and arguments need parsing.
290
- - Use :func: `~django_typer.get_command ` and invoke the command functions directly if your options
291
- and arguments are already of the correct type.
290
+ - Use :func: `~django_typer.get_command ` and invoke the command functions directly if your
291
+ options and arguments are already of the correct type.
292
292
293
293
.. tip ::
294
294
@@ -300,13 +300,14 @@ Change Default Django Options
300
300
-----------------------------
301
301
302
302
:class: `~django_typer.TyperCommand ` classes preserve all of the functionality of BaseCommand _ derivatives.
303
- This means that you can still use class members like `suppressed_base_arguments
303
+ This means that you can still use class members like `suppressed_base_arguments
304
304
<https://docs.djangoproject.com/en/5.0/howto/custom-management-commands/#django.core.management.BaseCommand.suppressed_base_arguments> `_
305
305
to suppress default options.
306
306
307
- By default :class: `~django_typer.TyperCommand ` suppresses ``--verbosity ``. You can add it back by setting
308
- ``suppressed_base_arguments `` to an empty list. If you want to use verbosity you can simply redefine it or
309
- use one of django-typer _'s :ref: `provided type hints <types >` for the default BaseCommand _ options:
307
+ By default :class: `~django_typer.TyperCommand ` suppresses ``--verbosity ``. You can add it back by
308
+ setting ``suppressed_base_arguments `` to an empty list. If you want to use verbosity you can
309
+ simply redefine it or use one of django-typer _'s :ref: `provided type hints <types >` for the default
310
+ BaseCommand _ options:
310
311
311
312
.. code-block :: python
312
313
@@ -326,7 +327,7 @@ Configure the Typer_ Application
326
327
327
328
Typer _ apps can be configured using a number of parameters. These parameters are usually passed
328
329
to the Typer class constructor when the application is created. django-typer _ provides a way to
329
- pass these options upstream to Typer _ by supplying them as keyword arguments to the
330
+ pass these options upstream to Typer _ by supplying them as keyword arguments to the
330
331
:class: `~django_typer.TyperCommand ` class inheritance:
331
332
332
333
.. code-block :: python
@@ -358,12 +359,12 @@ See the section on :ref:`defining shell completions.<define-shellcompletions>`
358
359
Configure rich _ Stack Traces
359
360
----------------------------
360
361
361
- When rich _ is installed it may be `configured to display rendered stack traces
362
+ When rich _ is installed it may be `configured to display rendered stack traces
362
363
<https://rich.readthedocs.io/en/stable/traceback.html> `_ for unhandled exceptions.
363
- These stack traces are information dense and can be very helpful for debugging. By default, if rich _
364
- is installed django-typer _ will configure it to render stack traces. You can disable this behavior by
365
- setting the ``DT_RICH_TRACEBACK_CONFIG `` config to ``False ``. You may also set
366
- ``DT_RICH_TRACEBACK_CONFIG `` to a dictionary holding the parameters to pass to
364
+ These stack traces are information dense and can be very helpful for debugging. By default, if
365
+ rich _ is installed django-typer _ will configure it to render stack traces. You can disable
366
+ this behavior by setting the ``DT_RICH_TRACEBACK_CONFIG `` config to ``False ``. You may also
367
+ set ``DT_RICH_TRACEBACK_CONFIG `` to a dictionary holding the parameters to pass to
367
368
`rich.traceback.install `.
368
369
369
370
This provides a common hook for configuring rich _ that you can control on a per-deployment basis:
@@ -397,6 +398,30 @@ This provides a common hook for configuring rich_ that you can control on a per-
397
398
the Typer _ application. It is best to not set these and allow users to configure tracebacks
398
399
via the ``DT_RICH_TRACEBACK_CONFIG `` setting.
399
400
401
+ Add Help Text to Commands
402
+ -------------------------
403
+
404
+ There are multiple places to add help text to your commands. There is however a precedence order,
405
+ and while lazy translation is supported in help texts, if you use docstrings as the helps they will
406
+ not be translated.
407
+
408
+ The precedence order, for a simple command is as follows:
409
+
410
+ .. code-block :: python
411
+
412
+ from django_typer import TyperCommand, command
413
+ from django.utils.translation import gettext_lazy as _
414
+
415
+ class Command (TyperCommand , help = _ (' 2' )):
416
+
417
+ help = _(" 3" )
418
+
419
+ @command (help = _(" 1" ))
420
+ def handle (self ):
421
+ """
422
+ Docstring is last priority and is not subject to translation.
423
+ """
424
+
400
425
401
426
Document Commands w/Sphinx
402
427
--------------------------
0 commit comments