Skip to content

Commit 909cdce

Browse files
committed
util: console: helpers for user input
Add a helper function for requesting confirmation from the user before performing some action and requesting a response in a loop. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent 9dbbcc8 commit 909cdce

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/infuse_iot/util/console.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,27 @@ def choose_one(title: str, options: list[str]) -> tuple[int, str]:
8888
pass
8989

9090
return idx, options[idx]
91+
92+
93+
def user_confirm(question: str) -> bool:
94+
"""Ask a question of the user and get confirmation"""
95+
96+
yes_options = ["Y", "y", "Yes", "yes"]
97+
no_options = ["N", "n", "No", "no"]
98+
99+
print(question)
100+
while True:
101+
rsp = input("Y/N:")
102+
if rsp in yes_options:
103+
return True
104+
elif rsp in no_options:
105+
return False
106+
107+
108+
def user_response(prompt: str) -> str:
109+
"""Get a response from the user"""
110+
rsp = ""
111+
112+
while rsp == "":
113+
rsp = input(prompt)
114+
return rsp

0 commit comments

Comments
 (0)