-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt_yn.py
More file actions
31 lines (26 loc) · 885 Bytes
/
prompt_yn.py
File metadata and controls
31 lines (26 loc) · 885 Bytes
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
##
# Simple Yes/No prompt function for shell scripts
def prompt_yn(prompt: str = 'Yes or no?', default: str = 'y') -> bool:
"""
Prompt the user with a Yes/No question and return their response as a boolean.
Args:
prompt (str): The question to present to the user.
default (str, optional): The default answer if the user just presses Enter.
Must be 'y' or 'n'. Defaults to 'y'.
Returns:
bool: True if the user answered 'yes', False if 'no'.
"""
valid = {'y': True, 'n': False}
if default not in valid:
raise ValueError("Invalid default answer: must be 'y' or 'n'")
prompt += " [Y/n]: " if default == "y" else " [y/N]: "
while True:
choice = input(prompt).strip().lower()
if choice == "":
return valid[default]
elif choice in ['y', 'yes']:
return True
elif choice in ['n', 'no']:
return False
else:
print("Please respond with 'y' or 'n'.")