[virt-tools-list] [virt-manager] [RFC PATCH 2/9] cli: Add the generic completer function and validator function

Lin Ma lma at suse.com
Wed Nov 14 08:12:51 UTC 2018


The patch adds the generic completer and validator, Further patches use
them as completer/validator. The completer won't add already specified
options to the list.

Signed-off-by: Lin Ma <lma at suse.com>
---
 virtinst/cli.py | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/virtinst/cli.py b/virtinst/cli.py
index 21467a4d..d46155f7 100644
--- a/virtinst/cli.py
+++ b/virtinst/cli.py
@@ -454,6 +454,43 @@ def get_meter():
     return util.make_meter(quiet=quiet)
 
 
+###########################
+# Common Completer        #
+###########################
+
+def completer(prefix, **kwargs):
+    sub_options = []
+    for parserclass in VIRT_PARSERS:
+        if kwargs['action'].dest == parserclass.cli_arg_name:
+            for arg in sorted(parserclass._virtargs, key=lambda p: p.cliname):
+                sub_options.append(arg.cliname + "=")
+    entered_options = prefix.split(",")
+    for option in entered_options:
+        pos = option.find("=")
+        if pos > 0 and option[: pos + 1] in sub_options:
+            sub_options.remove(option[: pos + 1])
+    return sub_options
+
+
+###########################
+# Common Validator        #
+###########################
+
+def completer_validator(current_input, keyword_to_check_against):
+    entered_options = keyword_to_check_against.split(",")
+
+    # e.g. for: --disk <TAB><TAB>
+    if keyword_to_check_against == "":
+        return True
+    # e.g. for: --disk bu<TAB><TAB> or --disk bus=ide,<TAB><TAB>
+    #                               or --disk bus=ide,pa<TAB><TAB>
+    if (len(entered_options) >= 1 and "=" not in entered_options[-1]):
+        if entered_options[-1] == "":
+            return True
+        else:
+            return current_input.startswith(entered_options[-1])
+
+
 ###########################
 # Common CLI option/group #
 ###########################
-- 
2.19.0




More information about the virt-tools-list mailing list