Skip to content
Myr

Derive "param_hint" in errors from param itself

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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 #704, #598

authored by Stephen Rosen · committed by LokiHokie · parent 9c456ec · Browse files · 2 files+21 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+21 added−6 removed

  • other:+21 added−6 removed

2 files+21 added−6 removed

modified:click/core.py+10 added
1429 unmodified lines
14301431 def get_usage_pieces(self, ctx):1432 return []14331434 def get_error_hint(self, ctx):1435 """Get a stringified version of the param for use in error messages to1436 indicate which param caused the error.1437 """1438 hint_list = self.opts or [self.human_readable_name]1439 return ' / '.join('"%s"' % x for x in hint_list)144014411442class Option(Parameter):1443 """Options are usually optional values on the command line and1444 have some extra features that arguments don't have.1445
315 unmodified lines
17611762 def get_usage_pieces(self, ctx):1763 return [self.make_metavar()]17641765 def get_error_hint(self, ctx):1766 return self.make_metavar()17671768 def add_to_parser(self, parser, ctx):1769 parser.add_argument(dest=self.name, nargs=self.nargs,1770 obj=self)1771
3 unmodified lines
modified:click/exceptions.py+11 added-6 removed
1from ._compat import PY2, filename_to_ui, get_text_stderr2from .utils import echo345def _join_param_hints(param_hint):6 if isinstance(param_hint, (tuple, list)):7 return ' / '.join('"%s"' % x for x in param_hint)8 return param_hint91011class ClickException(Exception):12 """An exception that Click can handle and show to the user."""1314 #: The exit code for this exception
82 unmodified lines
97 def format_message(self):98 if self.param_hint is not None:99 param_hint = self.param_hint100 elif self.param is not None:95 param_hint = self.param.opts or [self.param.human_readable_name]101 param_hint = self.param.get_error_hint(self.ctx)102 else:103 return 'Invalid value: %s' % self.message98 if isinstance(param_hint, (tuple, list)):99 param_hint = ' / '.join('"%s"' % x for x in param_hint)104 param_hint = _join_param_hints(param_hint)105106 return 'Invalid value for %s: %s' % (param_hint, self.message)107108109class MissingParameter(BadParameter):
16 unmodified lines
126 def format_message(self):127 if self.param_hint is not None:128 param_hint = self.param_hint129 elif self.param is not None:124 param_hint = self.param.opts or [self.param.human_readable_name]130 param_hint = self.param.get_error_hint(self.ctx)131 else:132 param_hint = None127 if isinstance(param_hint, (tuple, list)):128 param_hint = ' / '.join('"%s"' % x for x in param_hint)133 param_hint = _join_param_hints(param_hint)134135 param_type = self.param_type136 if param_type is None and self.param is not None:137 param_type = self.param.param_type_name
88 unmodified lines