Wraps a callback so that it's guaranteed to be executed with the script's application context. Custom commands (and their options) registered under ``app.cli`` or ``blueprint.cli`` will always have an app context available, this decorator is not required in that case. .. versio
(f: F)
| 378 | |
| 379 | |
| 380 | def with_appcontext(f: F) -> F: |
| 381 | """Wraps a callback so that it's guaranteed to be executed with the |
| 382 | script's application context. |
| 383 | |
| 384 | Custom commands (and their options) registered under ``app.cli`` or |
| 385 | ``blueprint.cli`` will always have an app context available, this |
| 386 | decorator is not required in that case. |
| 387 | |
| 388 | .. versionchanged:: 2.2 |
| 389 | The app context is active for subcommands as well as the |
| 390 | decorated callback. The app context is always available to |
| 391 | ``app.cli`` command and parameter callbacks. |
| 392 | """ |
| 393 | |
| 394 | @click.pass_context |
| 395 | def decorator(ctx: click.Context, /, *args: t.Any, **kwargs: t.Any) -> t.Any: |
| 396 | if not current_app: |
| 397 | app = ctx.ensure_object(ScriptInfo).load_app() |
| 398 | ctx.with_resource(app.app_context()) |
| 399 | |
| 400 | return ctx.invoke(f, *args, **kwargs) |
| 401 | |
| 402 | return update_wrapper(decorator, f) # type: ignore[return-value] |
| 403 | |
| 404 | |
| 405 | class AppGroup(click.Group): |