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
The command name will be the same callback function's name: `users`.
99
-
100
-
And the help text for that `users` command will be the callback function's docstring: `Manage users in the app.`.
92
+
The help text for that command will be the callback function's docstring: `Manage users in the app.`.
101
93
102
94
Check it:
103
95
@@ -107,7 +99,7 @@ Check it:
107
99
// Check the main help
108
100
$ python main.py --help
109
101
110
-
// Notice the command name "users" and the help text "Manage users in the app."
102
+
// Notice the help text "Manage users in the app."
111
103
Usage: main.py [OPTIONS] COMMAND [ARGS]...
112
104
113
105
Options:
@@ -135,9 +127,15 @@ Commands:
135
127
136
128
</div>
137
129
138
-
### Name and help from callback parameter in `typer.Typer()`
130
+
/// note
131
+
132
+
Before Typer 0.14.0, in addition to the help text, the command name was also inferred from the callback function name, this is no longer the case.
133
+
134
+
///
135
+
136
+
### Help from callback parameter in `typer.Typer()`
139
137
140
-
If you pass a `callback` parameter while creating a `typer.Typer(callback=some_function)` it will be used to infer the name and help text.
138
+
If you pass a `callback` parameter while creating a `typer.Typer(callback=some_function)` it will be used to infer the help text.
141
139
142
140
This has the lowest priority, we'll see later what has a higher priority and can override it.
143
141
@@ -155,7 +153,7 @@ Check it:
155
153
// Check the main help
156
154
$ python main.py --help
157
155
158
-
// Notice the command name "users" and the help text "Manage users in the app."
156
+
// Notice the help text "Manage users in the app."
159
157
Usage: main.py [OPTIONS] COMMAND [ARGS]...
160
158
161
159
Options:
@@ -185,11 +183,11 @@ Commands:
185
183
186
184
### Override a callback set in `typer.Typer()` with `@app.callback()`
187
185
188
-
The same as with normal **Typer** apps, if you pass a `callback` to `typer.Typer(callback=some_function)` and then override it with `@app.callback()`, the name and help text will be inferred from the new callback:
186
+
The same as with normal **Typer** apps, if you pass a `callback` to `typer.Typer(callback=some_function)` and then override it with `@app.callback()`, the help text will be inferred from the new callback:
Now the name of the command will be `users` instead of `old-callback`, and the help text will be `Manage users in the app.` instead of `Old callback help.`.
190
+
Now the help text will be `Manage users in the app.` instead of `Old callback help.`.
193
191
194
192
Check it:
195
193
@@ -199,7 +197,7 @@ Check it:
199
197
// Check the main help
200
198
$ python main.py --help
201
199
202
-
// Notice the command name "users" and the help text "Manage users in the app."
200
+
// Notice the help text "Manage users in the app."
203
201
Usage: main.py [OPTIONS] COMMAND [ARGS]...
204
202
205
203
Options:
@@ -227,17 +225,17 @@ Commands:
227
225
228
226
</div>
229
227
230
-
### Infer name and help from callback in `app.add_typer()`
228
+
### Help from callback in `app.add_typer()`
231
229
232
-
If you override the callback in `app.add_typer()` when including a sub-app, the name and help will be inferred from this callback function.
230
+
If you override the callback in `app.add_typer()` when including a sub-app, the help will be inferred from this callback function.
233
231
234
-
This takes precedence over inferring the name and help from a callback set in `@sub_app.callback()` and `typer.Typer(callback=sub_app_callback)`.
232
+
This takes precedence over inferring the help from a callback set in `@sub_app.callback()` and `typer.Typer(callback=sub_app_callback)`.
Now the command will be `new-users` instead of `users`. And the help text will be `I have the highland! Create some users.` instead of the previous ones.
238
+
The help text will be `I have the highland! Create some users.` instead of the previous ones.
241
239
242
240
Check it:
243
241
@@ -277,29 +275,29 @@ Commands:
277
275
278
276
### Enough inferring
279
277
280
-
So, when inferring a name and help text, the precedence order from lowest priority to highest is:
278
+
So, when inferring help text, the precedence order from lowest priority to highest is:
281
279
282
280
*`sub_app = typer.Typer(callback=some_function)`
283
281
*`@sub_app.callback()`
284
282
*`app.add_typer(sub_app, callback=new_function)`
285
283
286
-
That's for inferring the name and help text from functions.
284
+
That's for inferring the help text from functions.
287
285
288
-
But if you set the name and help text explicitly, that has a higher priority than these.
286
+
But if you set the help text explicitly, that has a higher priority than these.
289
287
290
288
## Set the name and help
291
289
292
290
Let's now see the places where you can set the command name and help text, from lowest priority to highest.
293
291
294
292
/// tip
295
293
296
-
Setting the name and help text explicitly always has a higher precedence than inferring from a callback function.
294
+
Setting the help text explicitly always has a higher precedence than inferring from a callback function.
297
295
298
296
///
299
297
300
298
### Name and help in `typer.Typer()`
301
299
302
-
You could have all the callbacks and overrides we defined before, but the name and help text was inferred from the function name and docstring.
300
+
You could have all the callbacks and overrides we defined before, but the help text was inferred from the function docstring.
303
301
304
302
If you set it explicitly, that takes precedence over inferring.
305
303
@@ -313,7 +311,7 @@ The rest of the callbacks and overrides are there only to show you that they don
313
311
314
312
///
315
313
316
-
We set an explicit name `exp-users`, and an explicit help `Explicit help.`.
314
+
We set an explicit help `Explicit help.`.
317
315
318
316
So that will take precedence now.
319
317
@@ -353,15 +351,15 @@ Commands:
353
351
354
352
</div>
355
353
356
-
### Name and help in `@app.callback()`
354
+
### Help text in `@app.callback()`
357
355
358
-
Any parameter that you use when creating a `typer.Typer()` app can be overridden in the parameters of `@app.callback()`.
356
+
Many parameters that you use when creating a `typer.Typer()` app can be overridden in the parameters of `@app.callback()`.
359
357
360
-
Continuing with the previous example, we now override the values in `@user_app.callback()`:
358
+
Continuing with the previous example, we now override the `help` in `@user_app.callback()`:
0 commit comments