Add getpermission.py.
This commit is contained in:
parent
6691722e7c
commit
0e92201e06
1 changed files with 36 additions and 0 deletions
36
voussoirkit/getpermission.py
Normal file
36
voussoirkit/getpermission.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
YES_STRINGS = ['yes', 'y']
|
||||||
|
NO_STRINGS = ['no', 'n']
|
||||||
|
|
||||||
|
def getpermission(
|
||||||
|
prompt=None,
|
||||||
|
*,
|
||||||
|
yes_strings=YES_STRINGS,
|
||||||
|
no_strings=NO_STRINGS,
|
||||||
|
must_pick=False,
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Prompt the user with a yes or no question.
|
||||||
|
|
||||||
|
Return True for yes, False for no, and None if undecided.
|
||||||
|
|
||||||
|
You can customize the strings that mean yes or no. For example, you could
|
||||||
|
create a "type the name of the thing you're about to delete" prompt.
|
||||||
|
|
||||||
|
If `must_pick`, then undecided is not allowed and the input will repeat
|
||||||
|
until they choose an acceptable answer. Either way, the intended usage of
|
||||||
|
`if getpermission():` will always only accept in case of explicit yes.
|
||||||
|
'''
|
||||||
|
if prompt is not None:
|
||||||
|
print(prompt)
|
||||||
|
while True:
|
||||||
|
answer = input(f'{yes_strings[0]}/{no_strings[0]}> ').strip()
|
||||||
|
yes = answer.lower() in (option.lower() for option in yes_strings)
|
||||||
|
no = answer.lower() in (option.lower() for option in no_strings)
|
||||||
|
if yes or no or not must_pick:
|
||||||
|
break
|
||||||
|
|
||||||
|
if yes:
|
||||||
|
return True
|
||||||
|
if no:
|
||||||
|
return False
|
||||||
|
return None
|
Loading…
Reference in a new issue