pferd/PFERD/utils.py

26 lines
616 B
Python
Raw Normal View History

2021-04-27 12:41:49 +02:00
from typing import Optional
2020-04-20 17:15:47 +02:00
2021-04-27 12:41:49 +02:00
def prompt_yes_no(query: str, default: Optional[bool]) -> bool:
2020-04-20 19:27:26 +02:00
"""
2021-04-27 12:41:49 +02:00
Asks the user a yes/no question and returns their choice.
2020-04-20 17:15:47 +02:00
"""
2020-04-20 14:29:28 +02:00
if default is True:
2021-04-27 12:41:49 +02:00
query += " [Y/n] "
2020-04-20 14:29:28 +02:00
elif default is False:
2021-04-27 12:41:49 +02:00
query += " [y/N] "
2020-04-20 14:29:28 +02:00
else:
2021-04-27 12:41:49 +02:00
query += " [y/n] "
2020-04-20 14:29:28 +02:00
while True:
2021-04-27 12:41:49 +02:00
response = input(query).strip().lower()
if response == "y":
2020-04-20 14:29:28 +02:00
return True
2021-04-27 12:41:49 +02:00
elif response == "n":
2020-04-20 14:29:28 +02:00
return False
2021-04-27 12:41:49 +02:00
elif response == "" and default is not None:
2020-04-20 17:15:47 +02:00
return default
2021-04-27 12:41:49 +02:00
print("Please answer with 'y' or 'n'.")