Skip to content

Commit

Permalink
Display a custom error message whenever an attempt to use -A or --app…
Browse files Browse the repository at this point in the history
… as a sub-command option was made.

Fixes #6363
  • Loading branch information
thedrow committed Sep 30, 2020
1 parent f05e82a commit 9939ba8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
5 changes: 2 additions & 3 deletions celery/bin/base.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
30 changes: 29 additions & 1 deletion celery/bin/celery.py
Expand Up @@ -2,6 +2,7 @@
import os

import click
import click.exceptions
from click.types import ParamType
from click_didyoumean import DYMGroup

Expand Down Expand Up @@ -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', []))
Expand Down Expand Up @@ -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 <...>
This 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.
Expand Down

0 comments on commit 9939ba8

Please sign in to comment.