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` |
| load_dotenv | bool | Load the nearest .env and .flaskenv files to set environment variables. |
| set_debug_flag | bool | Set the app's debug flag. |
| _loaded_plugin_commands | bool | This attribute is a boolean flag indicating whether plugin commands have been loaded, preventing redundant loading. |
Constructor
Signature
def FlaskGroup(
add_default_commands: bool = True,
create_app: t.Callable[..., [Flask](../app/flask.md?sid=flask_app_flask)]| None = None,
add_version_option: bool = True,
load_dotenv: bool = True,
set_debug_flag: bool = True,
extra: t.Any = null
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| add_default_commands | bool = True | if this is True then the default run and shell commands will be added. |
| create_app | `t.Callable[..., Flask] | None` = None |
| add_version_option | bool = True | adds the --version option. |
| load_dotenv | bool = True | Load the nearest :file:.env and :file:.flaskenv files to set environment variables. Will also change the working directory to the directory containing the first file found. |
| set_debug_flag | bool = True | Set the app's debug flag. |
| extra | t.Any = null | Additional keyword arguments. |
Methods
get_command()
@classmethod
def get_command(
ctx: click.Context,
name: str
) - > click.Command | None
Retrieves a command by its name, first checking built-in and plugin commands, then commands provided by the Flask application. This allows the CLI to find and execute specific commands.
Parameters
| Name | Type | Description |
|---|---|---|
| ctx | click.Context | The Click context object for the current command invocation. |
| 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]
Lists all available commands, including built-in, plugin, and application-defined commands. This is used to display available commands to the user, for example, when running flask --help.
Parameters
| Name | Type | Description |
|---|---|---|
| ctx | click.Context | The Click context object for the current command invocation. |
Returns
| Type | Description |
|---|---|
list[str] | A sorted list of command names. |
make_context()
@classmethod
def make_context(
info_name: str | None,
args: list[str],
parent: click.Context | None = None,
extra: t.Any
) - > click.Context
Creates a Click context for the command invocation, setting up environment variables and script information. This ensures that the Flask application can be loaded correctly within the CLI environment.
Parameters
| Name | Type | Description |
|---|---|---|
| info_name | `str | None` |
| args | list[str] | A list of command-line arguments. |
| parent | `click.Context | None` = None |
| extra | t.Any | Additional keyword arguments to pass to the context. |
Returns
| Type | Description |
|---|---|
click.Context | The created Click context object. |
parse_args()
@classmethod
def parse_args(
ctx: click.Context,
args: list[str]
) - > list[str]
Parses command-line arguments, with special handling for --env-file and --app options to ensure they are processed early. This allows the CLI to correctly load the Flask application based on user-provided environment and app settings.
Parameters
| Name | Type | Description |
|---|---|---|
| ctx | click.Context | The Click context object for the current command invocation. |
| args | list[str] | A list of command-line arguments to parse. |
Returns
| Type | Description |
|---|---|
list[str] | The remaining command-line arguments after parsing. |