Files
wizard/terminal_ui.py
2022-08-13 16:12:52 +02:00

44 lines
1.3 KiB
Python

def query(text):
return input(f"{text} \n> ")
def query_int(text):
try:
ans = query(text)
return int(ans)
except ValueError:
print("Please enter a valid number!")
return query_int(text)
def query_yes_no(question, default="no"):
"""Ask a yes/no question via raw_input() and return their answer.
https://stackoverflow.com/questions/3041986/apt-command-line-interface-like-yes-no-input
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
"""
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:
choice = query(question + prompt).lower()
if default is not None and choice == "":
return valid[default]
elif choice in valid:
return valid[choice]
else:
print("Please respond with 'yes' or 'no' (or 'y' or 'n').")