Merge branch 'Rovanion-master'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Merges #496
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 5a441c1, 13e045b · Browse files · 4 files·+42 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+42 added−10 removed
- tests:+20 added−0 removed
- other:+22 added−10 removed
Provenance
- Commit
67f39e4- Tree
3a74676- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
5a441c1- Parent 2
13e045b
4 files changed+42 added−10 removed
modified:click/core.py+4 added-3 removed
1459 unmodified lines1460 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)1467 1468 if prompt is True:37 unmodified lines1506 self.multiple = multiple1507 self.allow_from_autoenv = allow_from_autoenv1508 self.help = help1509 self.show_default = show_default1510+ self.show_choices = show_choices1511 1512 # Sanity check for stuff we don't support1513 if __debug__:1514 if self.nargs < 0:143 unmodified lines1658 # differently.1659 if self.is_bool_flag:1660 return confirm(self.prompt, default)1661 1661- 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))1666 1667 def resolve_envvar_value(self, ctx):87 unmodified linesmodified:click/termui.py+15 added-7 removed
4 unmodified lines5 from ._compat import raw_input, text_type, string_types, \6 isatty, strip_ansi, get_winterm_size, DEFAULT_COLUMNS, WIN7 from .utils import echo8 from .exceptions import Abort, UsageError9-from .types import convert_type9+from .types import convert_type, Choice10 from .globals import resolve_color_default11 12 13 # The prompt functions to use. The doc tools currently override these9 unmodified lines23 import getpass24 return getpass.getpass(prompt)25 26 27-def _build_prompt(text, suffix, show_default=False, default=None):27+def _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 + suffix34 35 34-def 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):36+def 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.41 42 If the user aborts the input by sending a interrupt signal, this43 function will catch it and raise a :exc:`Abort` exception.44 45+ .. versionadded:: 7.046+ Added the show_choices parameter.47+48 .. versionadded:: 6.049 Added unicode support for cmd.exe on Windows.50 51 .. versionadded:: 4.012 unmodified lines64 :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 = None74 75 def prompt_func(text):13 unmodified lines89 90 if value_proc is None:91 value_proc = convert_type(type, default)92 85- prompt = _build_prompt(text, prompt_suffix, show_default, default)93+ prompt = _build_prompt(text, prompt_suffix, show_default, default, show_choices, type)94 95 while 1:96 while 1:97 value = prompt_func(prompt)452 unmodified linesmodified:tests/test_termui.py+20 added
42 unmodified lines43 result = runner.invoke(cli, [])44 assert result.exception is None45 46 47+def test_choices_list_in_prompt(runner, monkeypatch):48+ @click.command()49+ @click.option('-g', type=click.Choice(['none', 'day', 'week', 'month']),50+ prompt=True)51+ def cli_with_choices(g):52+ pass53+54+ @click.command()55+ @click.option('-g', type=click.Choice(['none', 'day', 'week', 'month']),56+ prompt=True, show_choices=False)57+ def cli_without_choices(g):58+ pass59+60+ result = runner.invoke(cli_with_choices, [], input='none')61+ assert '(none, day, week, month)' in result.output62+63+ result = runner.invoke(cli_without_choices, [], input='none')64+ assert '(none, day, week, month)' not in result.output65+66+67 def test_secho(runner):68 with runner.isolation() as out:69 click.secho(None, nl=False)70 bytes = out.getvalue()1 unmodified linemodified:CHANGES+3 added
6 unmodified lines7 -----------8 9 (upcoming release with new features, release date to be decided)10 11+- 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