Skip to content

Commit 1a9caed

Browse files
authored
Merge pull request pallets#2765 from ThiefMaster/custom-cli-ignore-debug
Add option to not overwrite debug flag in cli
2 parents b34c717 + 50227f0 commit 1a9caed

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ Unreleased
1212
- :func:`send_file` encodes filenames as ASCII instead of Latin-1
1313
(ISO-8859-1). This fixes compatibility with Gunicorn, which is
1414
stricter about header encodings than PEP 3333. (`#2766`_)
15+
- Allow custom CLIs using ``FlaskGroup`` to set the debug flag without
16+
it always being overwritten based on environment variables. (`#2765`_)
1517

1618
.. _#2766: https://github.com/pallets/flask/issues/2766
19+
.. _#2765: https://github.com/pallets/flask/pull/2765
1720

1821

1922
Version 1.0.2

flask/cli.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ class ScriptInfo(object):
340340
onwards as click object.
341341
"""
342342

343-
def __init__(self, app_import_path=None, create_app=None):
343+
def __init__(self, app_import_path=None, create_app=None,
344+
set_debug_flag=True):
344345
#: Optionally the import path for the Flask application.
345346
self.app_import_path = app_import_path or os.environ.get('FLASK_APP')
346347
#: Optionally a function that is passed the script info to create
@@ -349,6 +350,7 @@ def __init__(self, app_import_path=None, create_app=None):
349350
#: A dictionary with arbitrary data that can be associated with
350351
#: this script info.
351352
self.data = {}
353+
self.set_debug_flag = set_debug_flag
352354
self._loaded_app = None
353355

354356
def load_app(self):
@@ -386,12 +388,10 @@ def load_app(self):
386388
'"app.py" module was not found in the current directory.'
387389
)
388390

389-
debug = get_debug_flag()
390-
391-
# Update the app's debug flag through the descriptor so that other
392-
# values repopulate as well.
393-
if debug is not None:
394-
app.debug = debug
391+
if self.set_debug_flag:
392+
# Update the app's debug flag through the descriptor so that
393+
# other values repopulate as well.
394+
app.debug = get_debug_flag()
395395

396396
self._loaded_app = app
397397
return app
@@ -459,14 +459,17 @@ class FlaskGroup(AppGroup):
459459
:param load_dotenv: Load the nearest :file:`.env` and :file:`.flaskenv`
460460
files to set environment variables. Will also change the working
461461
directory to the directory containing the first file found.
462+
:param set_debug_flag: Set the app's debug flag based on the active
463+
environment
462464
463465
.. versionchanged:: 1.0
464466
If installed, python-dotenv will be used to load environment variables
465467
from :file:`.env` and :file:`.flaskenv` files.
466468
"""
467469

468470
def __init__(self, add_default_commands=True, create_app=None,
469-
add_version_option=True, load_dotenv=True, **extra):
471+
add_version_option=True, load_dotenv=True,
472+
set_debug_flag=True, **extra):
470473
params = list(extra.pop('params', None) or ())
471474

472475
if add_version_option:
@@ -475,6 +478,7 @@ def __init__(self, add_default_commands=True, create_app=None,
475478
AppGroup.__init__(self, params=params, **extra)
476479
self.create_app = create_app
477480
self.load_dotenv = load_dotenv
481+
self.set_debug_flag = set_debug_flag
478482

479483
if add_default_commands:
480484
self.add_command(run_command)
@@ -550,7 +554,8 @@ def main(self, *args, **kwargs):
550554
obj = kwargs.get('obj')
551555

552556
if obj is None:
553-
obj = ScriptInfo(create_app=self.create_app)
557+
obj = ScriptInfo(create_app=self.create_app,
558+
set_debug_flag=self.set_debug_flag)
554559

555560
kwargs['obj'] = obj
556561
kwargs.setdefault('auto_envvar_prefix', 'FLASK')

tests/test_cli.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,28 @@ def test():
356356
assert result.output == 'flaskgroup\n'
357357

358358

359+
@pytest.mark.parametrize('set_debug_flag', (True, False))
360+
def test_flaskgroup_debug(runner, set_debug_flag):
361+
"""Test FlaskGroup debug flag behavior."""
362+
363+
def create_app(info):
364+
app = Flask("flaskgroup")
365+
app.debug = True
366+
return app
367+
368+
@click.group(cls=FlaskGroup, create_app=create_app, set_debug_flag=set_debug_flag)
369+
def cli(**params):
370+
pass
371+
372+
@cli.command()
373+
def test():
374+
click.echo(str(current_app.debug))
375+
376+
result = runner.invoke(cli, ['test'])
377+
assert result.exit_code == 0
378+
assert result.output == '%s\n' % str(not set_debug_flag)
379+
380+
359381
def test_print_exceptions(runner):
360382
"""Print the stacktrace if the CLI."""
361383

0 commit comments

Comments
 (0)