Browse Source

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.
Sebastian Lohff 5 months ago
parent
commit
d926606976
2 changed files with 11 additions and 1 deletions
  1. 2
    1
      clintermission/__init__.py
  2. 9
    0
      clintermission/climenu.py

+ 2
- 1
clintermission/__init__.py View File

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

+ 9
- 0
clintermission/climenu.py View File

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

Loading…
Cancel
Save