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
Provenance
- Commit
3cb9973- Tree
0fbe41c- Author
LokiHokie <wpifer3@gatech.edu>- Committer
GitHub <noreply@github.com>- Parent 1
9c456ec- Parent 2
f99c8d5
4 files changed+63 added−12 removed
modified:click/core.py+10 added
1429 unmodified lines1430 1431 def get_usage_pieces(self, ctx):1432 return []1433 1434+ 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)1440 1441+1442 class 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 lines1761 1762 def get_usage_pieces(self, ctx):1763 return [self.make_metavar()]1764 1765+ def get_error_hint(self, ctx):1766+ return '"%s"' % self.make_metavar()1767+1768 def add_to_parser(self, parser, ctx):1769 parser.add_argument(dest=self.name, nargs=self.nargs,1770 obj=self)1771 3 unmodified linesmodified:click/exceptions.py+11 added-6 removed
1 from ._compat import PY2, filename_to_ui, get_text_stderr2 from .utils import echo3 4 5+def _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_hint9+10+11 class ClickException(Exception):12 """An exception that Click can handle and show to the user."""13 14 #: The exit code for this exception82 unmodified lines97 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)105+106 return 'Invalid value for %s: %s' % (param_hint, self.message)107 108 109 class MissingParameter(BadParameter):16 unmodified lines126 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)134 135 param_type = self.param_type136 if param_type is None and self.param is not None:137 param_type = self.param.param_type_name88 unmodified linesmodified:tests/test_arguments.py+2 added-2 removed
186 unmodified lines187 click.echo('arg:' + '|'.join(arg))188 189 result = runner.invoke(cmd2, [])190 assert result.exit_code == 2191- assert 'Missing argument "arg"' in result.output191+ assert 'Missing argument "ARG..."' in result.output192 193 194 def test_missing_arg(runner):195 @click.command()2 unmodified lines198 click.echo('arg:' + arg)199 200 result = runner.invoke(cmd, [])201 assert result.exit_code == 2202- assert 'Missing argument "arg".' in result.output202+ assert 'Missing argument "ARG".' in result.output203 204 205 def test_implicit_non_required(runner):206 @click.command()83 unmodified linesmodified:tests/test_formatting.py+40 added-4 removed
157 unmodified lines158 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 ]164 165 166+def 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+ pass175+176+ 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+ ]184+185+186+def 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+ pass191+192+ 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+ ]200+201+202 def test_formatting_usage_error_nested(runner):203 @click.group()204 def cmd():205 pass8 unmodified lines214 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 ]220 221 222 def test_formatting_usage_error_no_help(runner):6 unmodified lines229 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 ]235 236 237 def test_formatting_usage_custom_help(runner):7 unmodified lines245 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 ]