Refactored prompts a bit
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 f69bc7b · Browse files · 1 file·+17 added−7 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+17 added−7 removed
- other:+17 added−7 removed
Provenance
- Commit
277d410- Tree
dc4fd03- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
f69bc7b
1 file changed+17 added−7 removed
modified:click/termui.py+17 added-7 removed
16 unmodified lines17 import getpass18 return getpass.getpass(prompt)19 20 21+def _build_prompt(text, suffix, show_default=False, default=None):22+ prompt = text23+ if default is not None and show_default:24+ prompt = '%s [%s]' % (prompt, default)25+ return prompt + suffix26+27+28 def prompt(text, default=None, hide_input=False,29 confirmation_prompt=False, type=None,23- value_proc=None):30+ value_proc=None, prompt_suffix=': ', show_default=True):31 """Prompts a user for input. This is a convenience function that can32 be used to prompt a user for input later.33 34 If the user aborts the input by sending a interrupt signal this8 unmodified lines43 :param type: the type to use to check the value against.44 :param value_proc: if this parameter is provided it's a function that45 is invoked instead of the type conversion to46 convert a value.47+ :param prompt_suffix: a suffix that should be added to the prompt.48+ :param show_default: shows or hides the default value in the prompt.49 """50 result = None51 52 def prompt_func(text):5 unmodified lines58 59 if value_proc is None:60 value_proc = convert_type(type, default)61 53- prompt = text54- if default is not None:55- prompt = '%s [%s]' % (prompt, default)56- prompt += ': '62+ prompt = _build_prompt(text, prompt_suffix, show_default, default)63 64 while 1:65 while 1:66 value = prompt_func(prompt)19 unmodified lines86 return result87 echo('Error: the two entered values do not match')88 89 84-def confirm(text, default=False, abort=False):90+def confirm(text, default=False, abort=False, prompt_suffix=': ',91+ show_default=True):92 """Prompts for confirmation (yes/no question).93 94 If the user aborts the input by sending a interrupt signal this95 function will catch it and raise a :exc:`Abort` exception.1 unmodified line97 :param text: the question to ask.98 :param default: the default for the prompt.99 :param abort: if this is set to `True` a negative answer aborts the100 exception by raising :exc:`Abort`.101+ :param prompt_suffix: a suffix that should be added to the prompt.102+ :param show_default: shows or hides the default value in the prompt.103 """95- prompt = '%s [%s]: ' % (text, default and 'Yn' or 'yN')104+ prompt = _build_prompt(text, prompt_suffix, show_default,105+ default and 'Yn' or 'yN')106 while 1:107 try:108 value = visible_prompt_func(prompt).lower().strip()109 except (KeyboardInterrupt, EOFError):73 unmodified lines