userquery.py 1.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Based on https://stackoverflow.com/a/3041990
def query_yes_no(question, default='yes'):
    '''
    Ask a yes/no question via input() and return the user's answer.

    :param str question: Prompt given to the user.
    :param str default: The assumed answer if th user just hits **Enter**. It
        must be ``'yes'`` (the default if no default is given), ``'no'``, or
        ``None`` (meaning an answer is required from the user).
    :returns: ``True`` if we ended up with a 'yes' answer, otherwise
        ``False``.
    '''
    valid = {'yes': True, 'y': True, 'ye': True, 'no': False, 'n': False}
    if default is None:
        prompt = ' [y/n] '
    elif default == 'yes':
        prompt = ' [Y/n] '
    elif default == 'no':
        prompt = ' [y/N] '
    else:
        raise ValueError('invalid default answer: "%s"' % default)
22
23
    prompt = question + prompt
    first_loop = True
24
    while True:
25
        choice = input(prompt).lower()
26
27
28
29
        if default is not None and choice == '':
            return valid[default]
        elif choice in valid:
            return valid[choice]
30
31
32
33
        elif first_loop:
            prompt = 'Please respond with "yes" or "no" (or y or n).\n' +\
                prompt
            first_loop = False