Browse Source

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

+ 7
- 1
clintermission/climenu.py View File

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

Loading…
Cancel
Save