FlaskGroup can be nested
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 4f03a76 · Browse files · 3 files·+36 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+36 added−10 removed
- tests:+13 added−0 removed
- other:+23 added−10 removed
Provenance
- Commit
aa801c4- Tree
3750d44- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
4f03a76
3 files changed+36 added−10 removed
modified:src/flask/cli.py+20 added-10 removed
3 unmodified lines4 import platform5 import re6 import sys7 import traceback8+import typing as t9 from functools import update_wrapper10 from operator import attrgetter11 from threading import Lock12 from threading import Thread465 unmodified lines478 479 if add_version_option:480 params.append(version_option)481 481- AppGroup.__init__(self, params=params, **extra)482+ if "context_settings" not in extra:483+ extra["context_settings"] = {}484+485+ extra["context_settings"].setdefault("auto_envvar_prefix", "FLASK")486+487+ super().__init__(params=params, **extra)488+489 self.create_app = create_app490 self.load_dotenv = load_dotenv491 self.set_debug_flag = set_debug_flag492 59 unmodified lines552 click.secho(f"{traceback.format_exc()}\n", err=True, fg="red")553 554 return sorted(rv)555 549- def main(self, *args, **kwargs):556+ def make_context(557+ self,558+ info_name: t.Optional[str],559+ args: t.List[str],560+ parent: t.Optional[click.Context] = None,561+ **extra: t.Any,562+ ) -> click.Context:563 if get_load_dotenv(self.load_dotenv):564 load_dotenv()565 553- obj = kwargs.get("obj")554-555- if obj is None:556- obj = ScriptInfo(566+ if "obj" not in extra and "obj" not in self.context_settings:567+ extra["obj"] = ScriptInfo(568 create_app=self.create_app, set_debug_flag=self.set_debug_flag569 )570 560- kwargs["obj"] = obj561- kwargs.setdefault("auto_envvar_prefix", "FLASK")562- return super().main(*args, **kwargs)571+ return super().make_context(info_name, args, parent=parent, **extra)572 573 574 def _path_is_ancestor(path, other):575 """Take ``other`` and remove the length of ``path`` from it. Then join it390 unmodified lines966 click.echo(row.format(rule.endpoint, methods, rule.rule).rstrip())967 968 969 cli = FlaskGroup(970+ name="flask",971 help="""\972 A general utility script for Flask applications.973 974 Provides commands from Flask, extensions, and the application. Loads the7 unmodified lines982 {prefix}flask run983 """.format(984 cmd="export" if os.name == "posix" else "set",985 prefix="$ " if os.name == "posix" else "> ",976- )986+ ),987 )988 989 990 def main() -> None:5 unmodified linesmodified:tests/test_cli.py+13 added
386 unmodified lines387 assert result.exit_code == 0388 assert result.output == f"{not set_debug_flag}\n"389 390 391+def test_flaskgroup_nested(app, runner):392+ cli = click.Group("cli")393+ flask_group = FlaskGroup(name="flask", create_app=lambda: app)394+ cli.add_command(flask_group)395+396+ @flask_group.command()397+ def show():398+ click.echo(current_app.name)399+400+ result = runner.invoke(cli, ["flask", "show"])401+ assert result.output == "flask_test\n"402+403+404 def test_no_command_echo_loading_error():405 from flask.cli import cli406 407 runner = CliRunner(mix_stderr=False)276 unmodified linesmodified:CHANGES.rst+3 added
23 unmodified lines24 the application instead. :issue:`4605`25 - Added the ``View.init_every_request`` class attribute. If a view26 subclass sets this to ``False``, the view will not create a new27 instance on every request. :issue:`2520`.28+- A ``flask.cli.FlaskGroup`` Click group can be nested as a29+ sub-command in a custom CLI. :issue:`3263`30 31+32 Version 2.1.333 -------------34 35 Unreleased1289 unmodified lines