-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
62 lines (48 loc) · 1.93 KB
/
utils.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
from typing import Optional
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def ask_yes_no_question(
prompt: str, *, default_answer: Optional[bool] = None
) -> bool:
"""
Asks the user a yes/no question using the given prompt.
As an example, for prompt="Do you love python?";
will ask the user 'Do you love python? y/n ', without ''.
Will continue asking until the user answers a valid answer.
:param prompt: The prompt to ask the user.
:param default_answer:
Specifies what happens when user doesn't input anything or
inputs spaces.
True=>'Y', False=>'N'.
Capitalizes the default choice.
When none specified (default), will not capitalize.
:return: True if yes, False if no
:raises TypeError: When default_answer is not bool and not None.
:type prompt: str
:type default_answer: Optional[bool]
:rtype: bool
"""
# Will be used as not case sensitive
VALID_YES = {"yes", "ye", "y", "true", "t", "1"}
VALID_NO = {"no", "n", "false", "f", "0"}
POSSIBLE_DISPLAYED_CHOICES = {True: "Y/n", False: "y/N", None: "y/n"}
if default_answer not in POSSIBLE_DISPLAYED_CHOICES:
raise TypeError(f"default_answer expected to be bool/None, but got {type(default_answer)}.")
displayed_choice = POSSIBLE_DISPLAYED_CHOICES[default_answer]
full_prompt = f"{prompt} {bcolors.BOLD}[{displayed_choice}]{bcolors.ENDC} "
while True:
user_answer = input(full_prompt).lower().strip()
if not user_answer and default_answer is not None:
return default_answer
if user_answer in VALID_YES:
return True
elif user_answer in VALID_NO:
return False