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` = None
load_dotenvbool = TrueLoad the nearest .env and .flaskenv files to set environment variables.
set_debug_flagbool = TrueSet the app's debug flag.
_loaded_plugin_commandsbool = FalseInternal 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

NameTypeDescription
add_default_commandsbool = TrueWhether to add the default run, shell, and routes commands.
create_app`t.Callable[..., Flask]None` = None
add_version_optionbool = TrueWhether to add the --version option to the group.
load_dotenvbool = TrueWhether to load environment variables from .env and .flaskenv files.
set_debug_flagbool = TrueWhether to set the application's debug flag based on CLI options.
**extrat.AnyAdditional 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

NameTypeDescription
add_default_commandsbool = TrueWhether to automatically register the default 'run', 'shell', and 'routes' commands.
create_app`t.Callable[..., Flask]None` = None
add_version_optionbool = TrueWhether to add the '--version' option to the command group.
load_dotenvbool = TrueWhether to load environment variables from .env and .flaskenv files.
set_debug_flagbool = TrueWhether 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

NameTypeDescription
ctxclick.ContextThe current Click execution context.
namestrThe name of the command to retrieve.

Returns

TypeDescription
`click.CommandNone`

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

NameTypeDescription
ctxclick.ContextThe current Click execution context.

Returns

TypeDescription
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

NameTypeDescription
info_name`strNone`
argslist[str]The list of arguments passed to the command.
parent`click.ContextNone`

Returns

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

NameTypeDescription
ctxclick.ContextThe current Click execution context.
argslist[str]The raw command line arguments to parse.

Returns

TypeDescription
list[str]The remaining unparsed arguments.