Skip to content
Myr

Added list of choices to termui prompt for user data if the data requested is a Choice.

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

It was odd that the user had to give incorrect input in order to be presented with the list of available options, for example: ``` Group by: something Error: invalid choice: something. (choose from none, day, week, month) ``` In the above example the user is asked to give the program some interval to group by but offers no help as to what the valid inputs are until the user has incorrectly entered 'something'. With the modifications included in this commit the prompt would look as follows: ``` Group by (none, day, week, month): ``` It is now clear to the user that it has a limited set of options to chose from. This of course works in combination with default options. ``` Group by (none, day, week, month) [week]: ``` It is possible to disable the display of the choices by passing show_choices=False just like passing show_default=False works. Also added a test for the new behaviour.

authored by Rovanion Luckey · committed by Rovanion Luckey · parent d3f36e8 · Browse files · 4 files+39 added−10 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+39 added−10 removed

  • tests:+17 added−0 removed
  • other:+22 added−10 removed

4 files+39 added−10 removed

modified:click/core.py+4 added-3 removed
1459 unmodified lines
1460 def __init__(self, param_decls=None, show_default=False,1461 prompt=False, confirmation_prompt=False,1462 hide_input=False, is_flag=None, flag_value=None,1463 multiple=False, count=False, allow_from_autoenv=True,1464 type=None, help=None, hidden=False, **attrs):1464 type=None, help=None, hidden=False, show_choices=True, **attrs):1465 default_is_missing = attrs.get('default', _missing) is _missing1466 Parameter.__init__(self, param_decls, type=type, **attrs)14671468 if prompt is True:
37 unmodified lines
1506 self.multiple = multiple1507 self.allow_from_autoenv = allow_from_autoenv1508 self.help = help1509 self.show_default = show_default1510 self.show_choices = show_choices15111512 # Sanity check for stuff we don't support1513 if __debug__:1514 if self.nargs < 0:
143 unmodified lines
1658 # differently.1659 if self.is_bool_flag:1660 return confirm(self.prompt, default)16611661 return prompt(self.prompt, default=default,1662 hide_input=self.hide_input,1662 return prompt(self.prompt, default=default, type=self.type,1663 hide_input=self.hide_input, show_choices=self.show_choices,1664 confirmation_prompt=self.confirmation_prompt,1665 value_proc=lambda x: self.process_value(ctx, x))16661667 def resolve_envvar_value(self, ctx):
87 unmodified lines
modified:click/termui.py+15 added-7 removed
4 unmodified lines
5from ._compat import raw_input, text_type, string_types, \6 isatty, strip_ansi, get_winterm_size, DEFAULT_COLUMNS, WIN7from .utils import echo8from .exceptions import Abort, UsageError9from .types import convert_type9from .types import convert_type, Choice10from .globals import resolve_color_default111213# The prompt functions to use. The doc tools currently override these
9 unmodified lines
23 import getpass24 return getpass.getpass(prompt)252627def _build_prompt(text, suffix, show_default=False, default=None):27def _build_prompt(text, suffix, show_default=False, default=None, show_choices=True, type=None):28 prompt = text29 if type is not None and show_choices and isinstance(type, Choice):30 prompt += ' (' + ", ".join(map(str, type.choices)) + ')'31 if default is not None and show_default:32 prompt = '%s [%s]' % (prompt, default)33 return prompt + suffix343534def prompt(text, default=None, hide_input=False,35 confirmation_prompt=False, type=None,36 value_proc=None, prompt_suffix=': ',37 show_default=True, err=False):36def prompt(text, default=None, hide_input=False, confirmation_prompt=False,37 type=None, value_proc=None, prompt_suffix=': ', show_default=True,38 err=False, show_choices=True):39 """Prompts a user for input. This is a convenience function that can40 be used to prompt a user for input later.4142 If the user aborts the input by sending a interrupt signal, this43 function will catch it and raise a :exc:`Abort` exception.4445 .. versionadded:: 7.046 Added the show_choices parameter.4748 .. versionadded:: 6.049 Added unicode support for cmd.exe on Windows.5051 .. versionadded:: 4.0
12 unmodified lines
64 :param prompt_suffix: a suffix that should be added to the prompt.65 :param show_default: shows or hides the default value in the prompt.66 :param err: if set to true the file defaults to ``stderr`` instead of67 ``stdout``, the same as with echo.68 :param show_choices: Show or hide choices if the passed type is a Choice.69 For example if type is a Choice of either day or week,70 show_choices is true and text is "Group by" then the71 prompt will be "Group by (day, week): ".72 """73 result = None7475 def prompt_func(text):
13 unmodified lines
8990 if value_proc is None:91 value_proc = convert_type(type, default)9285 prompt = _build_prompt(text, prompt_suffix, show_default, default)93 prompt = _build_prompt(text, prompt_suffix, show_default, default, show_choices, type)9495 while 1:96 while 1:97 value = prompt_func(prompt)
452 unmodified lines
modified:tests/test_termui.py+17 added
42 unmodified lines
43 result = runner.invoke(cli, [])44 assert result.exception is None454647def test_choices_list_in_prompt(runner, monkeypatch):48 @click.option('-g', type=click.Choice(['none', 'day', 'week', 'month']), prompt=True)49 @click.command()50 def cli_with_choices():51 pass5253 @click.option('-g', type=click.Choice(['none', 'day', 'week', 'month']), prompt=True, show_choices=False)54 @click.command()55 def cli_without_choices():56 pass5758 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)5960 assert '(none, day, week, month)' in runner.invoke(cli_with_choices, [], input='none').output61 assert '(none, day, week, month)' not in runner.invoke(cli_without_choices, [], input='none').output626364def test_secho(runner):65 with runner.isolation() as out:66 click.secho(None, nl=False)67 bytes = out.getvalue()
1 unmodified line
modified:CHANGES+3 added
6 unmodified lines
7-----------89(upcoming release with new features, release date to be decided)1011- The user is now presented with the available choices if prompt=True and12 type=click.Choice in a click.option. The choices are listed within13 parentthesis like 'Choose fruit (apple, orange): '.14- The exception objects now store unicode properly.15- Added the ability to hide commands and options from help.16- Added Float Range in Types.17- `secho`'s first argument can now be `None`, like in `echo`.
349 unmodified lines