Add shortcut function to quickly get a selection result

cli_select_item() will instanciate a CliMenu and return a selection. If
the selection is aborted an exception of the given class will be thrown.
This commit is contained in:
Sebastian Lohff 2020-01-23 09:59:55 +01:00
parent 3d1d2d5acd
commit d926606976
2 changed files with 11 additions and 1 deletions

View File

@ -1,11 +1,12 @@
# Written by Sebastian Lohff <seba@someserver.de> # Written by Sebastian Lohff <seba@someserver.de>
# Licensed under Apache License 2.0 # Licensed under Apache License 2.0
from clintermission.climenu import CliMenu, CliMenuStyle, CliMenuCursor, CliMenuTheme from clintermission.climenu import CliMenu, CliMenuStyle, CliMenuCursor, CliMenuTheme, cli_select_item
__all__ = [ __all__ = [
CliMenu, CliMenu,
CliMenuStyle, CliMenuStyle,
CliMenuCursor, CliMenuCursor,
CliMenuTheme, CliMenuTheme,
cli_select_item,
] ]

View File

@ -293,3 +293,12 @@ class CliMenu:
app.run() app.run()
self._ran = True self._ran = True
def cli_select_item(options, header=None, abort_exc=ValueError, abort_text="Selection aborted.", style=None):
"""Helper function to quickly get a selection with just a few arguments"""
menu = CliMenu(header=header, options=options, style=style)
if not menu.success:
raise abort_exc(abort_text)
return menu.get_selection()