Skip to main content

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

AttributeTypeDescription
create_app`t.Callable[..., Flask]None`
load_dotenvboolLoad the nearest .env and .flaskenv files to set environment variables.
set_debug_flagboolSet the app's debug flag.
_loaded_plugin_commandsboolThis 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

NameTypeDescription
add_default_commandsbool = Trueif this is True then the default run and shell commands will be added.
create_app`t.Callable[..., Flask]None` = None
add_version_optionbool = Trueadds the --version option.
load_dotenvbool = TrueLoad 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_flagbool = TrueSet the app's debug flag.
extrat.Any = nullAdditional 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

NameTypeDescription
ctxclick.ContextThe Click context object for the current command invocation.
namestrThe name of the command to retrieve.

Returns

TypeDescription
`click.CommandNone`

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

NameTypeDescription
ctxclick.ContextThe Click context object for the current command invocation.

Returns

TypeDescription
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

NameTypeDescription
info_name`strNone`
argslist[str]A list of command-line arguments.
parent`click.ContextNone` = None
extrat.AnyAdditional keyword arguments to pass to the context.

Returns

TypeDescription
click.ContextThe 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

NameTypeDescription
ctxclick.ContextThe Click context object for the current command invocation.
argslist[str]A list of command-line arguments to parse.

Returns

TypeDescription
list[str]The remaining command-line arguments after parsing.