Skip to content

Commit d66095a

Browse files
committed
finish shell completion docs
1 parent 0089584 commit d66095a

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

django_typer/completers.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@ def __call__(
333333
]
334334

335335

336-
def complete_app_label(ctx: Context, param: Parameter, incomplete: str):
336+
def complete_app_label(
337+
ctx: Context, param: Parameter, incomplete: str
338+
) -> t.List[CompletionItem]:
337339
"""
338340
A case-sensitive completer for Django app labels or names. The completer
339341
prefers labels but names will also work.
@@ -369,13 +371,13 @@ def handle(
369371
"""
370372
present = [app.label for app in (ctx.params.get(param.name or "") or [])]
371373
ret = [
372-
app.label
374+
CompletionItem(app.label)
373375
for app in apps.get_app_configs()
374376
if app.label.startswith(incomplete) and app.label not in present
375377
]
376378
if not ret and incomplete:
377379
ret = [
378-
app.name
380+
CompletionItem(app.name)
379381
for app in apps.get_app_configs()
380382
if app.name.startswith(incomplete)
381383
and app.name not in present

doc/source/reference.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ completers
4949
.. automodule:: django_typer.completers
5050
:members:
5151

52+
53+
utils
54+
-----
55+
56+
.. automodule:: django_typer.utils
57+
:members: traceback_config, get_current_command
58+
5259
.. _shellcompletion:
5360

5461
shellcompletion

doc/source/shell_completion.rst

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ as well as a way to install them in your shell.
3232
Installation
3333
============
3434

35-
Each shell has its own mechanism for enabling completions and this is further exacerbated
35+
Each shell has its own mechanism for enabling completions and this is further complicated
3636
by how different shells are installed and configured on different platforms. All shells
3737
have the same basic process. Completion logic needs to be registered with the shell that will be
3838
invoked when tabs are pressed for a specific command or script. To install tab completions
@@ -41,14 +41,15 @@ the shell. This process has two phases:
4141

4242
1. Ensure that your shell is configured to support completions.
4343
2. Use the :mod:`~django_typer.management.commands.shellcompletion` command to install the completion
44-
hook for your Django manage script.
44+
hook for your Django manage script. This usually entails adding a specifically named script to
45+
a certain directory or adding lines to an existing script. The
46+
:mod:`~django_typer.management.commands.shellcompletion` command will handle this for you.
4547

4648

47-
It can be frustrating to debug why completions are not working as expected. The goal of this
48-
guide is not to be an exhaustive list of how to enable completions for each supported shell on
49-
all possible platforms, but rather to provide general guidance on how to enable completions for
50-
the most common platforms and environments. If you encounter issues or have solutions, please
51-
`report them on our issues page <https://github.com/bckohan/django-typer>`_
49+
The goal of this guide is not to be an exhaustive list of how to enable completions for each
50+
supported shell on all possible platforms, but rather to provide general guidance on how to
51+
enable completions for the most common platforms and environments. If you encounter issues
52+
or have solutions, please `report them on our issues page <https://github.com/bckohan/django-typer>`_
5253

5354
Windows
5455
-------
@@ -148,6 +149,56 @@ hooks for implementing libraries to provide completions for their own commands.*
148149
Defining Custom Completions
149150
===========================
150151

151-
.. todo::
152-
153-
Check back soon!
152+
To define custom completion logic for your arguments_ and options_ pass the ``shell_completion``
153+
parameter in your type hint annotations. django-typer_ comes with a
154+
:ref:`few provided completers <completers>` for common Django_ types. One of the provided completers
155+
completes Django_ app labels and names. We might build a similar completer that only works for
156+
Django_ app labels like this:
157+
158+
.. code-block:: python
159+
:linenos:
160+
161+
import typing as t
162+
import typer
163+
from click import Context, Parameter
164+
from click.shell_completion import CompletionItem
165+
from django.apps import apps
166+
167+
from django_typer import TyperCommand
168+
169+
170+
# the completer function signature must match this exactly
171+
def complete_app_label(
172+
ctx: Context,
173+
param: Parameter,
174+
incomplete: str
175+
) -> t.List[CompletionItem]:
176+
177+
# don't offer apps that are already present as completion suggestions
178+
present = [app.label for app in (ctx.params.get(param.name or "") or [])]
179+
return [
180+
CompletionItem(app.label)
181+
for app in apps.get_app_configs()
182+
if app.label.startswith(incomplete) and app.label not in present
183+
]
184+
185+
186+
class MyCommand(TyperCommand):
187+
188+
@command()
189+
def handle(
190+
self,
191+
apps: t.Annotated[
192+
t.List[str],
193+
typer.Argument(
194+
help="The app label",
195+
shell_complete=complete_app_label # pass the completer function here
196+
)
197+
]
198+
):
199+
pass
200+
201+
.. tip::
202+
203+
See the :class:`~django_typer.completers.ModelObjectCompleter` for a completer that works
204+
for many Django_ model field types.

0 commit comments

Comments
 (0)