|
| 1 | +# django-typer |
| 2 | + |
| 3 | +(https://lbesson.mit-license.org/) |
| 4 | +(https://pypi.python.org/pypi/django-typer/) |
| 5 | +(https://pypi.python.org/pypi/django-typer/) |
| 6 | +(https://pypi.org/project/django-typer/) |
| 7 | +(https://pypi.python.org/pypi/django-typer) |
| 8 | +(http://django-typer.readthedocs.io/?badge=latest/) |
| 9 | +(https://codecov.io/gh/bckohan/django-typer) |
| 10 | +(https://github.com/bckohan/django-typer/actions/workflows/test.yml) |
| 11 | +(https://github.com/bckohan/django-typer/actions/workflows/lint.yml) |
| 12 | +(https://github.com/psf/black) |
| 13 | + |
| 14 | +Use Typer to define the CLI for your Django management commands. Provides a TyperCommand class that inherits from BaseCommand and allows typer-style annotated parameter types. All of the BaseCommand functionality is preserved, so that TyperCommand can be a drop-in replacement. |
| 15 | + |
| 16 | +**django-typer makes it easy to:** |
| 17 | + |
| 18 | +- Define your command CLI interface in a clear, DRY, and safe way using type hints. |
| 19 | +- Create subcommand and group command hierarchies. |
| 20 | +- Use the full power of Typer's parameter types to validate and parse command line inputs. |
| 21 | +- Create beautiful and information dense help outputs. |
| 22 | +- Configure the rendering of exception stack traces using rich. |
| 23 | +- Install shell tab-completion support for TyperCommands and normal Django commands for bash, zsh, fish, and powershell. |
| 24 | +- Create custom and portable shell tab-completions for your CLI parameters. |
| 25 | +- Refactor existing management commands into TyperCommands because TyperCommand is interface compatible with BaseCommand. |
| 26 | + |
| 27 | +Please refer to the [full documentation](https://django-typer.readthedocs.io/) for more information. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +1. Clone django-typer from GitHub or install a release off [PyPI](https://pypi.org/project/django-typer/): |
| 34 | + |
| 35 | + ```bash |
| 36 | + pip install django-typer |
| 37 | + ``` |
| 38 | + |
| 39 | + [rich](https://rich.readthedocs.io/en/latest/) is a powerful library for rich text and beautiful formatting in the terminal. It is not required but highly recommended for the best experience: |
| 40 | + |
| 41 | + ```bash |
| 42 | + pip install "django-typer[rich]" |
| 43 | + ``` |
| 44 | + |
| 45 | +2. Add `django_typer` to your `INSTALLED_APPS` setting: |
| 46 | + |
| 47 | + ```python |
| 48 | + INSTALLED_APPS = [ |
| 49 | + ... |
| 50 | + 'django_typer', |
| 51 | + ] |
| 52 | + ``` |
| 53 | + |
| 54 | +*You only need to install django_typer as an app if you want to use the shell completion command to enable tab-completion or if you would like django-typer to install rich traceback rendering for you - which it does by default if rich is also installed.* |
| 55 | + |
| 56 | +## Basic Example |
| 57 | + |
| 58 | +For example, TyperCommands can be a very simple drop-in replacement for BaseCommands. All of the documented features of BaseCommand work! |
| 59 | + |
| 60 | +```python |
| 61 | +from django_typer import TyperCommand |
| 62 | + |
| 63 | +class Command(TyperCommand): |
| 64 | + def handle(self, arg1: str, arg2: str, arg3: float = 0.5, arg4: int = 1): |
| 65 | + """ |
| 66 | + A basic command that uses Typer |
| 67 | + """ |
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +## Multiple Subcommands Example |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +Commands with multiple subcommands can be defined: |
| 77 | + |
| 78 | +```python |
| 79 | +import typing as t |
| 80 | +from django.utils.translation import gettext_lazy as _ |
| 81 | +from typer import Argument |
| 82 | +from django_typer import TyperCommand, command |
| 83 | + |
| 84 | +class Command(TyperCommand): |
| 85 | + @command() |
| 86 | + def create(self, name: t.Annotated[str, Argument(help=_("The name of the object to create."))]): |
| 87 | + """ |
| 88 | + Create an object. |
| 89 | + """ |
| 90 | + |
| 91 | + @command() |
| 92 | + def delete(self, id: t.Annotated[int, Argument(help=_("The id of the object to delete."))]): |
| 93 | + """ |
| 94 | + Delete an object. |
| 95 | + """ |
| 96 | +``` |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +## Grouping and Hierarchies Example |
| 101 | + |
| 102 | +More complex groups and subcommand hierarchies can be defined. For example, this command defines a group of commands called math, with subcommands divide and multiply: |
| 103 | + |
| 104 | +```bash |
| 105 | +./manage.py hierarchy math --precision 5 divide 10 2.1 |
| 106 | +./manage.py hierarchy math multiply 10 2 |
| 107 | +``` |
| 108 | + |
| 109 | +```python |
| 110 | +import typing as t |
| 111 | +from functools import reduce |
| 112 | +from django.utils.translation import gettext_lazy as _ |
| 113 | +from typer import Argument, Option |
| 114 | +from django_typer import TyperCommand, group |
| 115 | + |
| 116 | +class Command(TyperCommand): |
| 117 | + @group(help=_("Do some math at the given precision.")) |
| 118 | + def math(self, precision: t.Annotated[int, Option(help=_("The number of decimal places to output."))] = 2): |
| 119 | + self.precision = precision |
| 120 | + |
| 121 | + @math.command(help=_("Multiply the given numbers.")) |
| 122 | + def multiply(self, numbers: t.Annotated[t.List[float], Argument(help=_("The numbers to multiply"))]): |
| 123 | + return f"{reduce(lambda x, y: x * y, [1, *numbers]):.{self.precision}f}" |
| 124 | + |
| 125 | + @math.command() |
| 126 | + def divide(self, numerator: float, denominator: float, floor: bool = False): |
| 127 | + """ |
| 128 | + Divide the given numbers. |
| 129 | + """ |
| 130 | + if floor: |
| 131 | + return str(numerator // denominator) |
| 132 | + return f"{numerator / denominator:.{self.precision}f}" |
| 133 | +``` |
| 134 | + |
| 135 | + |
0 commit comments