Skip to content
Myr

Merge pull request #709 from sirosen/bugfix/error-hints/let-param-decide

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

Derive "param_hint" in errors from param itself

authored by LokiHokie · committed by GitHub · parents 9c456ec, f99c8d5 · Browse files · 4 files+63 added−12 removed
This commit

State

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

Diff stats

4 files+63 added−12 removed

  • tests:+42 added−6 removed
  • other:+21 added−6 removed

4 files+63 added−12 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 '"%s"' % 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
modified:tests/test_arguments.py+2 added-2 removed
186 unmodified lines
187 click.echo('arg:' + '|'.join(arg))188189 result = runner.invoke(cmd2, [])190 assert result.exit_code == 2191 assert 'Missing argument "arg"' in result.output191 assert 'Missing argument "ARG..."' in result.output192193194def test_missing_arg(runner):195 @click.command()
2 unmodified lines
198 click.echo('arg:' + arg)199200 result = runner.invoke(cmd, [])201 assert result.exit_code == 2202 assert 'Missing argument "arg".' in result.output202 assert 'Missing argument "ARG".' in result.output203204205def test_implicit_non_required(runner):206 @click.command()
83 unmodified lines
modified:tests/test_formatting.py+40 added-4 removed
157 unmodified lines
158 assert result.output.splitlines() == [159 'Usage: cmd [OPTIONS] ARG',160 'Try "cmd --help" for help.',161 '',162 'Error: Missing argument "arg".'162 'Error: Missing argument "ARG".'163 ]164165166def test_formatting_usage_error_metavar_missing_arg(runner):167 """168 :author: @r-m-n169 Including attribution to #612170 """171 @click.command()172 @click.argument('arg', metavar='metavar')173 def cmd(arg):174 pass175176 result = runner.invoke(cmd, [])177 assert result.exit_code == 2178 assert result.output.splitlines() == [179 'Usage: cmd [OPTIONS] metavar',180 'Try "cmd --help" for help.',181 '',182 'Error: Missing argument "metavar".'183 ]184185186def test_formatting_usage_error_metavar_bad_arg(runner):187 @click.command()188 @click.argument('arg', type=click.INT, metavar='metavar')189 def cmd(arg):190 pass191192 result = runner.invoke(cmd, ['3.14'])193 assert result.exit_code == 2194 assert result.output.splitlines() == [195 'Usage: cmd [OPTIONS] metavar',196 'Try "cmd --help" for help.',197 '',198 'Error: Invalid value for "metavar": 3.14 is not a valid integer'199 ]200201202def test_formatting_usage_error_nested(runner):203 @click.group()204 def cmd():205 pass
8 unmodified lines
214 assert result.output.splitlines() == [215 'Usage: cmd foo [OPTIONS] BAR',216 'Try "cmd foo --help" for help.',217 '',182 'Error: Missing argument "bar".'218 'Error: Missing argument "BAR".'219 ]220221222def test_formatting_usage_error_no_help(runner):
6 unmodified lines
229 assert result.exit_code == 2230 assert result.output.splitlines() == [231 'Usage: cmd [OPTIONS] ARG',232 '',197 'Error: Missing argument "arg".'233 'Error: Missing argument "ARG".'234 ]235236237def test_formatting_usage_custom_help(runner):
7 unmodified lines
245 assert result.output.splitlines() == [246 'Usage: cmd [OPTIONS] ARG',247 'Try "cmd --man" for help.',248 '',213 'Error: Missing argument "arg".'249 'Error: Missing argument "ARG".'250 ]