Skip to content
Myr

Merge branch 'pr/212'

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 · parents f1c2ce9, 271a651 · Browse files · 2 files+73 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

2 files+73 added−10 removed

  • tests:+58 added−0 removed
  • other:+15 added−10 removed

2 files+73 added−10 removed

modified:click/termui.py+15 added-10 removed
31 unmodified lines
323334def prompt(text, default=None, hide_input=False,35 confirmation_prompt=False, type=None,36 value_proc=None, prompt_suffix=': ', show_default=True):36 value_proc=None, prompt_suffix=': ',37 show_default=True, err=False):38 """Prompts a user for input. This is a convenience function that can39 be used to prompt a user for input later.4041 If the user aborts the input by sending a interrupt signal, this
10 unmodified lines
52 is invoked instead of the type conversion to53 convert a value.54 :param prompt_suffix: a suffix that should be added to the prompt.55 :param show_default: shows or hides the default value in the prompt.56 :param err: if set to true the file defaults to ``stderr`` instead of57 ``stdout``, the same as with echo.58 """59 result = None6061 def prompt_func(text):62 f = hide_input and hidden_prompt_func or visible_prompt_func63 try:64 # Write the prompt separately so that we get nice65 # coloring through colorama on Windows63 echo(text, nl=False)66 echo(text, nl=False, err=err)67 return f('')68 except (KeyboardInterrupt, EOFError):69 raise Abort()70
14 unmodified lines
85 return default86 try:87 result = value_proc(value)88 except UsageError as e:86 echo('Error: %s' % e.message)89 echo('Error: %s' % e.message, err=err)90 continue91 if not confirmation_prompt:92 return result93 while 1:
1 unmodified line
95 if value2:96 break97 if value == value2:98 return result96 echo('Error: the two entered values do not match')99 echo('Error: the two entered values do not match', err=err)100101102def confirm(text, default=False, abort=False, prompt_suffix=': ',100 show_default=True):103 show_default=True, err=False):104 """Prompts for confirmation (yes/no question).105106 If the user aborts the input by sending a interrupt signal this107 function will catch it and raise a :exc:`Abort` exception.
3 unmodified lines
111 :param abort: if this is set to `True` a negative answer aborts the112 exception by raising :exc:`Abort`.113 :param prompt_suffix: a suffix that should be added to the prompt.114 :param show_default: shows or hides the default value in the prompt.115 :param err: if set to true the file defaults to ``stderr`` instead of116 ``stdout``, the same as with echo.117 """118 prompt = _build_prompt(text, prompt_suffix, show_default,119 default and 'Y/n' or 'y/N')120 while 1:121 try:122 # Write the prompt separately so that we get nice123 # coloring through colorama on Windows119 echo(prompt, nl=False)124 echo(prompt, nl=False, err=err)125 value = visible_prompt_func('').lower().strip()126 except (KeyboardInterrupt, EOFError):127 raise Abort()128 if value in ('y', 'yes'):
2 unmodified lines
131 rv = False132 elif value == '':133 rv = default134 else:130 echo('Error: invalid input')135 echo('Error: invalid input', err=err)136 continue137 break138 if abort and not rv:139 raise Abort()
342 unmodified lines
482 from ._termui_impl import getchar as f483 return f(echo)484485481def pause(info='Press any key to continue ...'):486def pause(info='Press any key to continue ...', err=False):487 """This command stops execution and waits for the user to press any488 key to continue. This is similar to the Windows batch "pause"489 command. If the program is not run through a terminal, this command490 will instead do nothing.
5 unmodified lines
496 if not isatty(sys.stdin) or not isatty(sys.stdout):497 return498 try:499 if info:495 echo(info, nl=False)500 echo(info, nl=False, err=err)501 try:502 getchar()503 except (KeyboardInterrupt, EOFError):504 pass505 finally:506 if info:502 echo()507 echo(err=err)
modified:tests/test_utils.py+58 added
149 unmodified lines
150 out, err = capfd.readouterr()151 assert out == text + '\n'152153154def test_echo_writing_to_standard_error(capfd, monkeypatch):155 def emulate_input(text):156 """Emulate keyboard input."""157 if sys.version_info[0] == 2:158 from StringIO import StringIO159 else:160 from io import StringIO161 monkeypatch.setattr(sys, 'stdin', StringIO(text))162163 click.echo('Echo to standard output')164 out, err = capfd.readouterr()165 assert out == 'Echo to standard output\n'166 assert err == ''167168 click.echo('Echo to standard error', err=True)169 out, err = capfd.readouterr()170 assert out == ''171 assert err == 'Echo to standard error\n'172173 emulate_input('asdlkj\n')174 click.prompt('Prompt to stdin')175 out, err = capfd.readouterr()176 assert out == 'Prompt to stdin: '177 assert err == ''178179 emulate_input('asdlkj\n')180 click.prompt('Prompt to stderr', err=True)181 out, err = capfd.readouterr()182 assert out == ''183 assert err == 'Prompt to stderr: '184185 emulate_input('y\n')186 click.confirm('Prompt to stdin')187 out, err = capfd.readouterr()188 assert out == 'Prompt to stdin [y/N]: '189 assert err == ''190191 emulate_input('y\n')192 click.confirm('Prompt to stderr', err=True)193 out, err = capfd.readouterr()194 assert out == ''195 assert err == 'Prompt to stderr [y/N]: '196197 monkeypatch.setattr(click._compat, 'isatty', lambda x: True)198199 emulate_input(' ')200 click.pause('Pause to stdin')201 out, err = capfd.readouterr()202 assert out == 'Pause to stdin'203 assert err == ''204205 emulate_input(' ')206 click.pause('Pause to stderr', err=True)207 out, err = capfd.readouterr()208 assert out == ''209 assert err == 'Pause to stderr'210211212def test_open_file(runner):213 with runner.isolated_filesystem():214 with open('hello.txt', 'w') as f:215 f.write('Cool stuff')
15 unmodified lines