From 322993f0a799c835045123fbab7967018988fc4e Mon Sep 17 00:00:00 2001 From: Omer Katz Date: Wed, 30 Sep 2020 15:16:20 +0300 Subject: [PATCH] Display a custom error message whenever an attempt to use -A or --app as a sub-command option was made. Fixes #6363 --- celery/bin/base.py | 5 ++--- celery/bin/celery.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/celery/bin/base.py b/celery/bin/base.py index 9429900a95..662ba728ae 100644 --- a/celery/bin/base.py +++ b/celery/bin/base.py @@ -39,8 +39,7 @@ def __init__(self, app, no_color, workdir, quiet=False): @cached_property def OK(self): - return self.style("OK", fg="green", bold=True) \ - + return self.style("OK", fg="green", bold=True) @cached_property def ERROR(self): @@ -72,7 +71,7 @@ def error(self, message=None, **kwargs): kwargs['color'] = False click.echo(message, **kwargs) else: - click.echo(message, **kwargs) + click.secho(message, **kwargs) def pretty(self, n): if isinstance(n, list): diff --git a/celery/bin/celery.py b/celery/bin/celery.py index 4f7c95d065..9f4fa0cbe4 100644 --- a/celery/bin/celery.py +++ b/celery/bin/celery.py @@ -2,6 +2,7 @@ import os import click +import click.exceptions from click.types import ParamType from click_didyoumean import DYMGroup @@ -104,7 +105,8 @@ def celery(ctx, app, broker, result_backend, loader, config, workdir, os.environ['CELERY_RESULT_BACKEND'] = result_backend if config: os.environ['CELERY_CONFIG_MODULE'] = config - ctx.obj = CLIContext(app=app, no_color=no_color, workdir=workdir, quiet=quiet) + ctx.obj = CLIContext(app=app, no_color=no_color, workdir=workdir, + quiet=quiet) # User options worker.params.extend(ctx.obj.app.user_options.get('worker', [])) @@ -139,6 +141,32 @@ def report(ctx): celery.add_command(shell) celery.add_command(multi) +# Monkey-patch click to display a custom error +# when -A or --app are used as sub-command options instead of as options +# of the global command. + +previous_show_implementation = click.exceptions.NoSuchOption.show + +WRONG_APP_OPTION_USAGE_MESSAGE = """You are using `{option_name}` as an option of the {info_name} sub-command: +celery {info_name} {option_name} celeryapp <...> + +The support for this usage was removed in Celery 5.0. Instead you should use `{option_name}` as a global option: +celery {option_name} celeryapp {info_name} <...>""" + + +def _show(self, file=None): + if self.option_name in ('-A', '--app'): + self.ctx.obj.error( + WRONG_APP_OPTION_USAGE_MESSAGE.format( + option_name=self.option_name, + info_name=self.ctx.info_name), + fg='red' + ) + previous_show_implementation(self, file=file) + + +click.exceptions.NoSuchOption.show = _show + def main() -> int: """Start celery umbrella command.