Added docstrings, removed ny.

This commit is contained in:
Jeremy Fincher 2003-10-04 13:07:15 +00:00
parent 325e751149
commit 72af23bb00

View File

@ -29,9 +29,15 @@
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
"""Handles interactive questions; useful for wizards and whatnot."""
import textwrap import textwrap
def expect(prompt, possibilities, recursed=False): def expect(prompt, possibilities, recursed=False):
"""Prompt the user with prompt, allow them to choose from possibilities.
If possibilities is empty, allow anything.
"""
originalPrompt = prompt originalPrompt = prompt
indent = ' ' * ((len(originalPrompt)%68) + 2) indent = ' ' * ((len(originalPrompt)%68) + 2)
if recursed: if recursed:
@ -56,6 +62,7 @@ def expect(prompt, possibilities, recursed=False):
return s.strip() return s.strip()
def expectWithDefault(prompt, possibilities, default): def expectWithDefault(prompt, possibilities, default):
"""Same as expect, except with a default."""
indent = ' ' * ((len(prompt)%68) + 2) indent = ' ' * ((len(prompt)%68) + 2)
prompt = '%s [%s] (default: %s) ' % \ prompt = '%s [%s] (default: %s) ' % \
(prompt.strip(), '/'.join(possibilities), default) (prompt.strip(), '/'.join(possibilities), default)
@ -71,9 +78,11 @@ def expectWithDefault(prompt, possibilities, default):
return default return default
def anything(prompt): def anything(prompt):
"""Allow anything from the user."""
return expect(prompt, []) return expect(prompt, [])
def something(prompt): def something(prompt):
"""Allow anything *except* nothing from the user."""
s = expect(prompt, []) s = expect(prompt, [])
while not s: while not s:
print 'Sorry, you must enter a value.' print 'Sorry, you must enter a value.'
@ -81,10 +90,8 @@ def something(prompt):
return s return s
def yn(prompt): def yn(prompt):
"""Allow only 'y' or 'n' from the user."""
return expect(prompt, ['y', 'n']) return expect(prompt, ['y', 'n'])
def ny(prompt):
return expect(prompt, ['n', 'y'])
def quit(prompt): # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
return anything(prompt).lower() in ('q', 'quit')