You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/context.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,12 @@
1
1
# Context
2
2
3
-
When you create a Typer application it uses Click underneath.
4
-
And every Click application has a special object called a [`Context`](https://click.palletsprojects.com/en/stable/api/#click.Context) that is normally hidden.
5
-
But you can access the context by declaring a function parameter of type `typer.Context`.
3
+
Every app has a special internal object that keeps track of state relevant to the script's execution.
4
+
For some advanced use-cases, you may want to access it directly.
5
+
This can be done by declaring a function parameter of type `typer.Context`.
6
+
7
+
Similarly, you can also declare a function parameter with type `typer.CallbackParam` in case a callback could be used
8
+
by several CLI parameters, and you need to figure out which one it was.
6
9
7
-
The same way you can access the context by declaring a function parameter with its value,
8
-
you can declare another function parameter with type `typer.CallbackParam` to get the specific Click [`Parameter`](https://click.palletsprojects.com/en/stable/api/#click.Parameter) object.
Copy file name to clipboardExpand all lines: docs/tutorial/arguments/help.md
+3-29Lines changed: 3 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,14 +112,6 @@ Options:
112
112
113
113
</div>
114
114
115
-
/// note | Technical Details
116
-
117
-
In Click applications the default values are hidden by default. 🙈
118
-
119
-
In **Typer** these default values are shown by default. 👀
120
-
121
-
///
122
-
123
115
## Custom default string
124
116
125
117
You can use the same `show_default` to pass a custom string (instead of a `bool`) to customize the default value to be shown in the help text:
@@ -266,28 +258,10 @@ But it won't show up in the main help text under the `Arguments` section.
266
258
267
259
///
268
260
269
-
### Help text for *CLI arguments* in Click
270
-
271
-
Click itself doesn't support adding help for *CLI arguments*, and it doesn't generate help for them as in the "`Arguments:`" sections in the examples above.
272
-
273
-
Not supporting `help` in *CLI arguments* is an intentional <ahref="https://click.palletsprojects.com/en/7.x/documentation/#documenting-arguments"class="external-link"target="_blank">design decision in Click</a>:
274
-
275
-
> This is to follow the general convention of Unix tools of using arguments for only the most necessary things, and to document them in the command help text by referring to them by name.
261
+
### Help text for *CLI arguments*
276
262
277
-
So, in Click applications, you are expected to write all the documentation for *CLI arguments* by hand in the <abbrtitle="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr>.
278
-
279
-
---
280
-
281
-
Nevertheless, **Typer supports `help` for *CLI arguments***. ✨ 🤷♂
282
-
283
-
**Typer** doesn't follow that convention and instead supports `help` to make it easier to have consistent help texts with a consistent format for your CLI programs. 🎨
263
+
**Typer supports `help` for *CLI arguments*** to make it easier to have consistent help texts with a consistent format for your CLI programs. 🎨
284
264
285
265
This is also to help you create CLI programs that are ✨ awesome ✨ *by default*. With very little code.
286
266
287
-
If you want to keep Click's convention in a **Typer** app, you can do it with the `hidden` parameter as described above.
288
-
289
-
/// note | Technical Details
290
-
291
-
To support `help` in *CLI arguments***Typer** does a lot of internal work in its own sub-classes of Click's internal classes.
292
-
293
-
///
267
+
If you don't want the CLI Argument to be shown in help outputs, you can set the `hidden` parameter to `True`.
Copy file name to clipboardExpand all lines: docs/tutorial/commands/callback.md
-24Lines changed: 0 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -159,27 +159,3 @@ Creating user: Camila
159
159
```
160
160
161
161
</div>
162
-
163
-
## Click Group
164
-
165
-
If you come from Click, this **Typer** callback is the equivalent of the function in a <ahref="https://click.palletsprojects.com/en/7.x/quickstart/#nesting-commands"class="external-link"target="_blank">Click Group</a>.
166
-
167
-
For example:
168
-
169
-
```Python
170
-
import click
171
-
172
-
@click.group()
173
-
defcli():
174
-
pass
175
-
```
176
-
177
-
The original function `cli` would be the equivalent of a Typer callback.
178
-
179
-
/// note | Technical Details
180
-
181
-
When using Click, it converts that `cli` variable to a Click `Group` object. And then the original function no longer exists in that variable.
182
-
183
-
**Typer** doesn't do that, the callback function is not modified, only registered in the `typer.Typer` app. This is intentional, it's part of **Typer**'s design, to allow having editor auto completion and type checks.
Copy file name to clipboardExpand all lines: docs/tutorial/commands/context.md
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Using the Context
2
2
3
-
When you create a **Typer** application it uses Click underneath. And every Click application has a specialobject called a <ahref="https://click.palletsprojects.com/en/8.1.x/commands/#nested-handling-and-contexts"class="external-link"target="_blank">"Context"</a> that is normally hidden.
3
+
When you create a **Typer** application it always has a special, hidden object underneath called the "Context".
4
4
5
5
But you can access the context by declaring a function parameter of type `typer.Context`.
6
6
@@ -99,8 +99,6 @@ Creating user: Camila
99
99
100
100
You can pass configurations for the context when creating a command or callback.
101
101
102
-
To read more about the available configurations check the docs for <ahref="https://click.palletsprojects.com/en/7.x/api/#context"class="external-link"target="_blank">Click's `Context`</a>.
103
-
104
102
For example, you could keep additional *CLI parameters* not declared in your CLI program with `ignore_unknown_options` and `allow_extra_args`.
105
103
106
104
Then you can access those extra raw *CLI parameters* as a `list` of `str` in `ctx.args`:
Copy file name to clipboardExpand all lines: docs/tutorial/commands/index.md
-20Lines changed: 0 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -162,30 +162,10 @@ Commands:
162
162
163
163
</div>
164
164
165
-
## Click Group
166
-
167
-
If you come from Click, a `typer.Typer` app with subcommands is more or less the equivalent of a <ahref="https://click.palletsprojects.com/en/7.x/quickstart/#nesting-commands"class="external-link"target="_blank">Click Group</a>.
168
-
169
-
/// note | Technical Details
170
-
171
-
A `typer.Typer` app is *not* a Click Group, but it provides the equivalent functionality. And it creates a Click Group when calling it.
172
-
173
-
It is not directly a Group because **Typer** doesn't modify the functions in your code to convert them to another type of object, it only registers them.
174
-
175
-
///
176
-
177
165
## Decorator Technical Details
178
166
179
167
When you use `@app.command()` the function under the decorator is registered in the **Typer** application and is then used later by the application.
180
168
181
169
But Typer doesn't modify that function itself, the function is left as is.
182
170
183
171
That means that if your function is simple enough that you could create it without using `typer.Option()` or `typer.Argument()`, you could use the same function for a **Typer** application and a **FastAPI** application putting both decorators on top, or similar tricks.
184
-
185
-
/// note | Click Technical Details
186
-
187
-
This behavior is a design difference with Click.
188
-
189
-
In Click, when you add a `@click.command()` decorator it actually modifies the function underneath and replaces it with an object.
Copy file name to clipboardExpand all lines: docs/tutorial/multiple-values/options-with-multiple-values.md
-8Lines changed: 0 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,14 +48,6 @@ coins = user[1]
48
48
is_wizard = user[2]
49
49
```
50
50
51
-
/// tip
52
-
53
-
Notice that the default is a tuple with `(None, None, None)`.
54
-
55
-
You cannot simply use `None` here as the default because <ahref="https://github.com/pallets/click/issues/472"class="external-link"target="_blank">Click doesn't support it</a>.
Copy file name to clipboardExpand all lines: docs/tutorial/options-autocompletion.md
+4-16Lines changed: 4 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,9 +124,7 @@ So, in the end, we return a `list` of `tuples` of `str`:
124
124
125
125
If you want to have help text for each item, make sure each item in the list is a `tuple`. Not a `list`.
126
126
127
-
Click checks specifically for a `tuple` when extracting the help text.
128
-
129
-
So in the end, the return will be a `list` (or other iterable) of `tuples` of 2 `str`.
127
+
In the end, the return will be a `list` (or other iterable) of `tuples` of 2 `str`.
130
128
131
129
///
132
130
@@ -157,7 +155,7 @@ Sebastian -- The type hints guy.
157
155
158
156
Instead of creating and returning a list with values (`str` or `tuple`), we can use `yield` with each value that we want in the completion.
159
157
160
-
That way our function will be a <ahref="https://docs.python.org/3.8/glossary.html#index-19"class="external-link"target="_blank">generator</a> that **Typer**(actually Click) can iterate:
158
+
That way our function will be a <ahref="https://docs.python.org/3.8/glossary.html#index-19"class="external-link"target="_blank">generator</a> that **Typer** can iterate:
And the same way as before, we want to provide **completion** for those names. But we don't want to provide the **same names** for completion if they were already given in previous parameters.
216
214
217
-
For that, we will access and use the "Context". When you create a **Typer** application it uses Click underneath. And every Click application has a special object called a <ahref="https://click.palletsprojects.com/en/7.x/commands/#nested-handling-and-contexts"class="external-link"target="_blank">"Context"</a> that is normally hidden.
215
+
For that, we will access and use the "Context". Every Typer application has a special object called a "Context" that is normally hidden.
218
216
219
217
But you can access the context by declaring a function parameter of type `typer.Context`.
220
218
@@ -308,7 +306,7 @@ We get all the *CLI parameters* as a raw `list` of `str` by declaring a paramete
308
306
309
307
/// tip
310
308
311
-
Here we name the list of all the raw *CLI parameters*`args` because that's the convention with Click.
309
+
Here we name the list of all the raw *CLI parameters*`args` because that's the usual convention.
312
310
313
311
But it doesn't contain only *CLI arguments*, it has everything, including *CLI options* and values, as a raw `list` of `str`.
314
312
@@ -384,13 +382,3 @@ You can declare function parameters of these types:
384
382
*`list[str]`: for the raw *CLI parameters*.
385
383
386
384
It doesn't matter how you name them, in which order, or which ones of the 3 options you declare. It will all "**just work**" ✨
387
-
388
-
## Comparison to Click functionality
389
-
390
-
Note that Click 7 had a similar [`autocompletion` function](https://click.palletsprojects.com/en/7.x/bashcomplete/), but it worked slightly differently.
391
-
392
-
It required the callback function to take exactly the 3 arguments `ctx`, `args` and `incomplete` in that exact order, instead of matching them dynamically based on types, as Typer does.
393
-
394
-
Since Click 8, this functionality has been replaced by [`shell_complete`](https://click.palletsprojects.com/en/8.1.x/api/#click.ParamType.shell_complete), which still depends on the exact order of arguments for the callback function.
395
-
396
-
However, Typer continues to use the `autocompletion` functionality as described on this page.
0 commit comments