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
Notice that now we added the sub-Typer without specifying a `name` nor a `help`.
95
-
96
-
They are now inferred from the callback function.
97
-
98
-
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,9 @@ Commands:
135
127
136
128
</div>
137
129
138
-
### Name and help from callback parameter in `typer.Typer()`
130
+
### Help from callback parameter in `typer.Typer()`
139
131
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.
132
+
If you pass a `callback` parameter while creating a `typer.Typer(callback=some_function)` it will be used to infer the help text.
141
133
142
134
This has the lowest priority, we'll see later what has a higher priority and can override it.
143
135
@@ -155,7 +147,7 @@ Check it:
155
147
// Check the main help
156
148
$ python main.py --help
157
149
158
-
// Notice the command name "users" and the help text "Manage users in the app."
150
+
// Notice the help text "Manage users in the app."
159
151
Usage: main.py [OPTIONS] COMMAND [ARGS]...
160
152
161
153
Options:
@@ -185,11 +177,11 @@ Commands:
185
177
186
178
### Override a callback set in `typer.Typer()` with `@app.callback()`
187
179
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:
180
+
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.`.
184
+
Now the help text will be `Manage users in the app.` instead of `Old callback help.`.
193
185
194
186
Check it:
195
187
@@ -199,7 +191,7 @@ Check it:
199
191
// Check the main help
200
192
$ python main.py --help
201
193
202
-
// Notice the command name "users" and the help text "Manage users in the app."
194
+
// Notice the help text "Manage users in the app."
203
195
Usage: main.py [OPTIONS] COMMAND [ARGS]...
204
196
205
197
Options:
@@ -227,17 +219,17 @@ Commands:
227
219
228
220
</div>
229
221
230
-
### Infer name and help from callback in `app.add_typer()`
222
+
### Help from callback in `app.add_typer()`
231
223
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.
224
+
If you override the callback in `app.add_typer()` when including a sub-app, the help will be inferred from this callback function.
233
225
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)`.
226
+
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.
232
+
The help text will be `I have the highland! Create some users.` instead of the previous ones.
241
233
242
234
Check it:
243
235
@@ -277,13 +269,13 @@ Commands:
277
269
278
270
### Enough inferring
279
271
280
-
So, when inferring a name and help text, the precedence order from lowest priority to highest is:
272
+
So, when inferring help text, the precedence order from lowest priority to highest is:
281
273
282
274
*`sub_app = typer.Typer(callback=some_function)`
283
275
*`@sub_app.callback()`
284
276
*`app.add_typer(sub_app, callback=new_function)`
285
277
286
-
That's for inferring the name and help text from functions.
278
+
That's for inferring the help text from functions.
287
279
288
280
But if you set the name and help text explicitly, that has a higher priority than these.
289
281
@@ -299,7 +291,7 @@ Setting the name and help text explicitly always has a higher precedence than in
299
291
300
292
### Name and help in `typer.Typer()`
301
293
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.
294
+
You could have all the callbacks and overrides we defined before, but the help text was inferred from the function docstring.
303
295
304
296
If you set it explicitly, that takes precedence over inferring.
0 commit comments