Skip to content
Myr

Implemented extra info for missing choice parameters. This fixes #120

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

authored by Armin Ronacher · committed by Armin Ronacher · parent a6136b4 · Browse files · 4 files+29 added−1 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+29 added−1 removed

  • tests:+13 added−0 removed
  • other:+16 added−1 removed

4 files+29 added−1 removed

modified:click/core.py+5 added-1 removed
898 unmodified lines
899900 return value901902 def get_missing_message(self, ctx):903 return 'Missing %s %s.' % (903 rv = 'Missing %s %s.' % (904 self.param_type_name,905 ' / '.join('"%s"' % x for x in chain(906 self.opts, self.secondary_opts)),907 )908 extra = self.type.get_missing_message(self)909 if extra:910 rv += ' ' + extra911 return rv912913 def resolve_envvar_value(self, ctx):914 if self.envvar is None:915 return
352 unmodified lines
modified:click/types.py+10 added
37 unmodified lines
3839 def get_metavar(self, param):40 """Returns the metavar default for this param if it provides one."""4142 def get_missing_message(self, param):43 """Optionally might return extra information about a missing44 parameter.4546 .. versionadded:: 2.047 """4849 def convert(self, param, ctx, value):50 """Converts the value. This is not invoked for values that are51 `None` (the missing value).52 """
65 unmodified lines
118119 def get_metavar(self, param):120 return '[%s]' % '|'.join(self.choices)121122 def get_missing_message(self, param):123 return 'Choose from %s.' % ', '.join(self.choices)124125 def convert(self, value, param, ctx):126 if value in self.choices:127 return value128 self.fail('invalid choice: %s. (choose from %s)' %
313 unmodified lines
modified:tests/test_options.py+13 added
154 unmodified lines
155 result = runner.invoke(cmd, ['-whatever', '42'])156 assert result.output == '42\n'157 result = runner.invoke(cmd, ['-whatever=23'])158 assert result.output == '23\n'159160161def test_missing_choice(runner):162 @click.command()163 @click.option('--foo', type=click.Choice(['foo', 'bar']),164 required=True)165 def cmd(foo):166 click.echo(foo)167168 result = runner.invoke(cmd)169 assert result.exit_code == 2170 assert 'Error: Missing option "--foo". Choose from foo, bar.' \171 in result.output
modified:CHANGES+1 added
37 unmodified lines
38 style arguments.39- added :func:`pause` which works similar to the Windows ``pause`` cmd40 built-in but becomes an automatic noop if the application is not run41 through a terminal.42- added a bit of extra information about missing choice parameters.4344Version 1.145-----------46
11 unmodified lines