-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.py
78 lines (63 loc) · 2.44 KB
/
prompt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""
Module for prompting user input and asking yes/no questions.
Author: antonio.ventilii
"""
from __future__ import annotations
import sys
import easygui
def dialog_prompt(message: str, title: str = '', open_question: bool = False) -> bool | str:
"""
Display a dialog prompt and return the user's answer.
Args:
message (str): The message to display in the dialog.
title (str, optional): The title of the dialog. Defaults to an empty string.
open_question (bool, optional): If True, an open-ended question dialog will be displayed.
If False, a yes/no question dialog will be displayed. Defaults to False.
Returns:
bool | str: The user's answer.
"""
if open_question:
answer = easygui.enterbox(msg=message, title=title)
else:
answer = easygui.ynbox(msg=message, title=title)
return answer
def query_yes_no(question: str, default: str = None, use_prompt: bool = False) -> bool:
"""
Ask a yes/no question and return the user's answer.
Args:
question (str): The question to ask the user.
default (str, optional): The default answer if the user just hits <Enter>.
It can be "yes", "no", or None. Defaults to None.
use_prompt (bool, optional): If True, a dialog prompt will be displayed instead of printing to stdout.
Defaults to False.
Returns:
bool: True for "yes" or False for "no".
Raises:
ValueError: If the default answer is invalid.
"""
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)
while True:
if use_prompt:
choice = dialog_prompt(message=question + prompt)
else:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
elif isinstance(choice, bool):
return choice
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")