Added optional err parameter to prompt(), confirm() and pause().
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This is the same as with echo(). If False, writes to standard output, if True writes to standard error. Useful for some redirect operations.
authored by Kiss György · committed by Kiss György · parent 4507fd5 · Browse files · 1 file·+15 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
1 file+15 added−10 removed
- other:+15 added−10 removed
Provenance
- Commit
8aebc8b- Tree
776ac95- Author
Kiss György <kissgyorgy@me.com>- Committer
Kiss György <kissgyorgy@me.com>- Parent
4507fd5
1 file changed+15 added−10 removed
modified:click/termui.py+15 added-10 removed
31 unmodified lines32 33 34 def 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.40 41 If the user aborts the input by sending a interrupt signal, this10 unmodified lines52 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 = None60 61 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 lines85 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 line95 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)100 101 102 def 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).105 106 If the user aborts the input by sending a interrupt signal this107 function will catch it and raise a :exc:`Abort` exception.3 unmodified lines111 :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 lines131 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 lines482 from ._termui_impl import getchar as f483 return f(echo)484 485 481-def pause(info='Press any key to continue ...'):486+def 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 lines496 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)