Add option to return instantly on single item in cli_select_item()

cli_select_item() allows to quickly launch a menu from code, but if only
a single item is given a selection is not necessary in most cases.
Therefore we now instantly return the given value in these cases. This
can be turned off by setting return_single=False
This commit is contained in:
Sebastian Lohff 2020-03-11 17:46:16 +01:00
parent d926606976
commit 9fe596bc01
1 changed files with 7 additions and 1 deletions

View File

@ -295,9 +295,15 @@ class CliMenu:
self._ran = True
def cli_select_item(options, header=None, abort_exc=ValueError, abort_text="Selection aborted.", style=None):
def cli_select_item(options, header=None, abort_exc=ValueError, abort_text="Selection aborted.", style=None,
return_single=True):
"""Helper function to quickly get a selection with just a few arguments"""
menu = CliMenu(header=header, options=options, style=style)
if return_single and menu.num_options == 1:
item = menu.get_options()[0]
return item.num, item.item
if not menu.success:
raise abort_exc(abort_text)