@@ -32,7 +32,7 @@ as well as a way to install them in your shell.
32
32
Installation
33
33
============
34
34
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
36
36
by how different shells are installed and configured on different platforms. All shells
37
37
have the same basic process. Completion logic needs to be registered with the shell that will be
38
38
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:
41
41
42
42
1. Ensure that your shell is configured to support completions.
43
43
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.
45
47
46
48
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 >`_
52
53
53
54
Windows
54
55
-------
@@ -148,6 +149,56 @@ hooks for implementing libraries to provide completions for their own commands.*
148
149
Defining Custom Completions
149
150
===========================
150
151
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