|
1 | 1 | """
|
2 |
| -The shellcompletion command is a Django management command that installs and removes |
| 2 | +The shellcompletion command is a Django_ management command that installs and removes |
3 | 3 | shellcompletion scripts for supported shells (bash, fish, zsh, powershell). This
|
4 | 4 | command is also the entry point for running the completion logic and can be used to
|
5 | 5 | debug completer code.
|
6 | 6 |
|
7 |
| -It invokes typer's shell completion installation logic, but does have to patch the |
8 |
| -installed scripts. This is because there is only one installation for all django |
9 |
| -management commands, not each individual command. The completion logic here will |
10 |
| -failover to django's builtin autocomplete if the command in question is not a |
11 |
| -TyperCommand. To promote compatibility with other management command libraries or |
12 |
| -custom completion logic, a fallback completion function can also be specified. A |
13 |
| -needed refactoring here would be to provide root hooks for completion logic in django |
14 |
| -that base classes can register for. This would provide a coordinated way for libraries |
15 |
| -like django-typer to plug in their own completion logic. |
| 7 | +.. typer:: django_typer.management.commands.shellcompletion.Command:typer_app |
| 8 | + :prog: manage.py shellcompletion |
| 9 | + :width: 80 |
| 10 | + :convert-png: latex |
| 11 | +
|
| 12 | +:func:`~django_typer.management.commands.shellcompletion.Command.install` invokes typer's shell |
| 13 | +completion installation logic, but does have to patch the installed scripts. This is because |
| 14 | +there is only one installation for all Django_ management commands, not each individual command. |
| 15 | +The completion logic here will failover to Django_'s builtin autocomplete if the command in |
| 16 | +question is not a :class:`~django_typer.TyperCommand`. To promote compatibility with other |
| 17 | +management command libraries or custom completion logic, a fallback completion function can also |
| 18 | +be specified. |
16 | 19 | """
|
17 | 20 |
|
| 21 | +# A needed refactoring here would be to provide root hooks for completion logic in Django core that |
| 22 | +# base classes can register for. This would provide a coordinated way for libraries like |
| 23 | +# django-typer to plug in their own completion logic. |
| 24 | + |
18 | 25 | import contextlib
|
19 | 26 | import inspect
|
20 | 27 | import io
|
@@ -62,9 +69,9 @@ class Command(TyperCommand):
|
62 | 69 | This command installs autocompletion for the current shell. This command uses the typer/click
|
63 | 70 | autocompletion scripts to generate the autocompletion items, but monkey patches the scripts
|
64 | 71 | to invoke our bundled shell complete script which fails over to the django autocomplete
|
65 |
| - function when the command being completed is not a TyperCommand. When the django autocomplete |
66 |
| - function is used we also wrap it so that it works for any supported click/typer shell, not just |
67 |
| - bash. |
| 72 | + function when the command being completed is not a :class:`~django_typer.TyperCommand`. When |
| 73 | + the django autocomplete function is used we also wrap it so that it works for any supported |
| 74 | + click/typer shell, not just bash. |
68 | 75 |
|
69 | 76 | We also provide a remove command to easily remove the installed script.
|
70 | 77 |
|
@@ -290,7 +297,11 @@ def install(
|
290 | 297 | Install autocompletion for the given shell. If the shell is not specified, it will
|
291 | 298 | try to detect the shell. If the shell is not detected, it will fail.
|
292 | 299 |
|
293 |
| - We run the upstream typer installation routines. |
| 300 | + We run the upstream typer installation routines, with some augmentation. |
| 301 | +
|
| 302 | + .. typer:: django_typer.management.commands.shellcompletion.Command:typer_app:install |
| 303 | + :width: 85 |
| 304 | + :convert-png: latex |
294 | 305 | """
|
295 | 306 | # do not import this private stuff until we need it - avoids tanking the whole
|
296 | 307 | # library if these imports change
|
@@ -336,6 +347,12 @@ def remove(
|
336 | 347 |
|
337 | 348 | Since the installation routine is upstream we first run install to determine where the
|
338 | 349 | completion script is installed and then we remove it.
|
| 350 | +
|
| 351 | + .. typer:: django_typer.management.commands.shellcompletion.Command:typer_app:remove |
| 352 | + :prog: shellcompletion |
| 353 | + :width: 80 |
| 354 | + :convert-png: latex |
| 355 | +
|
339 | 356 | """
|
340 | 357 | # do not import this private stuff until we need it - avoids tanking the whole
|
341 | 358 | # library if these imports change
|
@@ -436,7 +453,22 @@ def complete(
|
436 | 453 | """
|
437 | 454 | We implement the shell complete generation script as a Django command because the
|
438 | 455 | Django environment needs to be bootstrapped for it to work. This also allows
|
439 |
| - us to test autocompletions in a platform agnostic way. |
| 456 | + us to test completion logic in a platform agnostic way. |
| 457 | +
|
| 458 | + .. tip:: |
| 459 | +
|
| 460 | + This command is super useful for debugging shell_complete logic. For example to |
| 461 | + enter into the debugger, we could set a breakpoint in our ``shell_complete`` function |
| 462 | + for the option parameter and then run the following command: |
| 463 | +
|
| 464 | + .. code-block:: bash |
| 465 | +
|
| 466 | + $ ./manage.py shellcompletion complete "./manage.py your_command --option " |
| 467 | +
|
| 468 | + .. typer:: django_typer.management.commands.shellcompletion.Command:typer_app:complete |
| 469 | + :prog: shellcompletion |
| 470 | + :width: 80 |
| 471 | + :convert-png: latex |
440 | 472 | """
|
441 | 473 | os.environ[self.COMPLETE_VAR] = (
|
442 | 474 | f"complete_{shell.value}"
|
|
0 commit comments