[utils] Add get_int_choice helper

This commit is contained in:
derrod 2021-12-29 12:55:35 +01:00
parent 3d877185b0
commit f0f4b545f5

View file

@ -13,6 +13,36 @@ def get_boolean_choice(prompt, default=True):
return False return False
def get_int_choice(prompt, default=None, min_choice=None, max_choice=None, return_on_invalid=False):
if default is not None:
prompt = f'{prompt} [{default}]: '
else:
prompt = f'{prompt}: '
while True:
try:
choice = int(input(prompt))
except ValueError:
if default is not None:
return default
else:
continue
else:
if min_choice is not None and choice < min_choice:
print(f'Number must be greater than {min_choice}')
if return_on_invalid:
return None
return_on_invalid = True
continue
if max_choice is not None and choice > max_choice:
print(f'Number must be less than {max_choice}')
if return_on_invalid:
return None
return_on_invalid = True
continue
return choice
def sdl_prompt(sdl_data, title): def sdl_prompt(sdl_data, title):
tags = [''] tags = ['']
if '__required' in sdl_data: if '__required' in sdl_data: