Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display a custom error message whenever an attempt to use -A or --app as a sub-command option was made #6378

Merged
merged 1 commit into from Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 <...>

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.
Expand Down