FlaskGroup
Special subclass of the :class:AppGroup group that supports loading more commands from the configured Flask app. Normally a developer does not have to interface with this class but there are some very advanced use cases for which it makes sense to create an instance of this. see :ref:custom-scripts.
Attributes
| Attribute | Type | Description |
|---|---|---|
| create_app | `t.Callable[..., Flask] | None` = None |
| load_dotenv | bool = True | Load the nearest .env and .flaskenv files to set environment variables. |
| set_debug_flag | bool = True | Set the app's debug flag. |
| _loaded_plugin_commands | bool = False | Internal flag used to track whether entry point commands from the 'flask.commands' group have already been loaded. |
Constructor
Signature
def FlaskGroup(
add_default_commands: bool = True,
create_app: t.Callable[..., [Flask](../app/flask.md?sid=src_flask_app_flask)]| None = None,
add_version_option: bool = True,
load_dotenv: bool = True,
set_debug_flag: bool = True,
**extra: t.Any
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| add_default_commands | bool = True | Whether to add the default run, shell, and routes commands. |
| create_app | `t.Callable[..., Flask] | None` = None |
| add_version_option | bool = True | Whether to add the --version option to the group. |
| load_dotenv | bool = True | Whether to load environment variables from .env and .flaskenv files. |
| set_debug_flag | bool = True | Whether to set the application's debug flag based on CLI options. |
| **extra | t.Any | Additional keyword arguments passed to the parent AppGroup class. |
Signature
def FlaskGroup(
add_default_commands: bool = True,
create_app: t.Callable[..., [Flask](../app/flask.md?sid=src_flask_app_flask)]| None = None,
add_version_option: bool = True,
load_dotenv: bool = True,
set_debug_flag: bool = True
)
Parameters
| Name | Type | Description |
|---|---|---|
| add_default_commands | bool = True | Whether to automatically register the default 'run', 'shell', and 'routes' commands. |
| create_app | `t.Callable[..., Flask] | None` = None |
| add_version_option | bool = True | Whether to add the '--version' option to the command group. |
| load_dotenv | bool = True | Whether to load environment variables from .env and .flaskenv files. |
| set_debug_flag | bool = True | Whether to automatically set the application's debug flag based on CLI options. |
Methods
get_command()
@classmethod
def get_command(
ctx: click.Context,
name: str
) - > click.Command | None
Retrieves a command by name, searching built-in commands, plugins, and then commands defined on the Flask application.
Parameters
| Name | Type | Description |
|---|---|---|
| ctx | click.Context | The current Click execution context. |
| name | str | The name of the command to retrieve. |
Returns
| Type | Description |
|---|---|
| `click.Command | None` |
list_commands()
@classmethod
def list_commands(
ctx: click.Context
) - > list[str]
Returns a sorted list of all available command names from built-ins, plugins, and the Flask application.
Parameters
| Name | Type | Description |
|---|---|---|
| ctx | click.Context | The current Click execution context. |
Returns
| Type | Description |
|---|---|
list[str] | A sorted list of unique command names available in this group. |
make_context()
@classmethod
def make_context(
info_name: str | None,
args: list[str],
parent: click.Context | None
) - > click.Context
Creates a new Click context and ensures a ScriptInfo object is initialized to manage application loading.
Parameters
| Name | Type | Description |
|---|---|---|
| info_name | `str | None` |
| args | list[str] | The list of arguments passed to the command. |
| parent | `click.Context | None` |
Returns
| Type | Description |
|---|---|
click.Context | The initialized Click context containing the ScriptInfo object. |
parse_args()
@classmethod
def parse_args(
ctx: click.Context,
args: list[str]
) - > list[str]
Parses CLI arguments and ensures environment and application options are handled early for help display.
Parameters
| Name | Type | Description |
|---|---|---|
| ctx | click.Context | The current Click execution context. |
| args | list[str] | The raw command line arguments to parse. |
Returns
| Type | Description |
|---|---|
list[str] | The remaining unparsed arguments. |