Skip to content

Commit

Permalink
Derive "param_hint" in errors from param itself
Browse files Browse the repository at this point in the history
Rather than trying to apply the same logic to all param types, trying to
extract a meaningful value from Parameter.opts even for Arguments, make
this a function computed against the Parameter itself. Then, when it's
time to specialize it to use the metavar for Arguments, we just need to
override the function.
Also unify a small amount of logic being shared among exception classes.
Fully backwards compatible, except for the change to error text on
Arguments, which is the intended effect.

Fixes pallets#704, pallets#598
  • Loading branch information
sirosen committed Dec 30, 2016
1 parent e6f22c6 commit fe58ed3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
10 changes: 10 additions & 0 deletions click/core.py
Expand Up @@ -1421,6 +1421,13 @@ def get_help_record(self, ctx):
def get_usage_pieces(self, ctx):
return []

def get_error_hint(self, ctx):
"""Get a stringified version of the param for use in error messages to
indicate which param caused the error.
"""
hint_list = self.opts or [self.human_readable_name]
return ' / '.join('"%s"' % x for x in hint_list)


class Option(Parameter):
"""Options are usually optional values on the command line and
Expand Down Expand Up @@ -1745,6 +1752,9 @@ def _parse_decls(self, decls, expose_value):
def get_usage_pieces(self, ctx):
return [self.make_metavar()]

def get_error_hint(self, ctx):
return self.make_metavar()

def add_to_parser(self, parser, ctx):
parser.add_argument(dest=self.name, nargs=self.nargs,
obj=self)
Expand Down
17 changes: 11 additions & 6 deletions click/exceptions.py
Expand Up @@ -2,6 +2,12 @@
from .utils import echo


def _join_param_hints(param_hint):
if isinstance(param_hint, (tuple, list)):
return ' / '.join('"%s"' % x for x in param_hint)
return param_hint


class ClickException(Exception):
"""An exception that Click can handle and show to the user."""

Expand Down Expand Up @@ -89,11 +95,11 @@ def format_message(self):
if self.param_hint is not None:
param_hint = self.param_hint
elif self.param is not None:
param_hint = self.param.opts or [self.param.human_readable_name]
param_hint = self.param.get_error_hint(self.ctx)
else:
return 'Invalid value: %s' % self.message
if isinstance(param_hint, (tuple, list)):
param_hint = ' / '.join('"%s"' % x for x in param_hint)
param_hint = _join_param_hints(param_hint)

return 'Invalid value for %s: %s' % (param_hint, self.message)


Expand All @@ -118,11 +124,10 @@ def format_message(self):
if self.param_hint is not None:
param_hint = self.param_hint
elif self.param is not None:
param_hint = self.param.opts or [self.param.human_readable_name]
param_hint = self.param.get_error_hint(self.ctx)
else:
param_hint = None
if isinstance(param_hint, (tuple, list)):
param_hint = ' / '.join('"%s"' % x for x in param_hint)
param_hint = _join_param_hints(param_hint)

param_type = self.param_type
if param_type is None and self.param is not None:
Expand Down

0 comments on commit fe58ed3

Please sign in to comment.