Files
custom-debian-iso/udib/userinput.py
2022-04-20 22:45:44 +02:00

49 lines
1.4 KiB
Python

#!/usr/bin/env python3
""" This module is responsible for (interactive) user input handling. """
import re
from printmsg import pinput
def prompt_yes_or_no(question, ask_until_valid = False):
""" Prompts the user with the specified yes/no question.
If 'ask_until_valid' is True, keeps repeating the prompt until the user
provides recognizable input to answer the question.
Parameters
----------
question : str
The question to ask the user while prompting. The string is suffixed
with " (Yes/No) ".
ask_until_valid : bool
If this is set to True and the user provides an ambiguous answer, keeps
prompting indefinitely until an unambiguous answer is read.
Returns
-------
True : bool
If the user has answered 'yes'.
False : bool
If the user has answered 'no'.
None : None
If 'ask_until_valid' is False and the user has provided an ambiguous
response to the prompt.
"""
user_input_is_valid = False
regex_yes = re.compile(r"^(y)$|^(Y)$|^(YES)$|^(Yes)$|^(yes)$")
regex_no = re.compile(r"^(n)$|^(N)$|^(NO)$|^(No)$|^(no)$")
while(not user_input_is_valid):
user_input = pinput(f"{question} (Yes/No): ")
if (regex_yes.match(user_input)):
return True
elif (regex_no.match(user_input)):
return False
elif (not ask_until_valid):
return None