Skip to content
Snippets Groups Projects
Commit 63d44c55 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Bash completion: Implement dynamic option value completion


Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarOlivier Tharan <olive@google.com>
parent 30d44392
No related branches found
No related tags found
No related merge requests found
......@@ -273,12 +273,15 @@ class CompletionWriter:
if not suggest:
suggest = opt.choices
if suggest:
suggest_text = " ".join(sorted(suggest))
if (isinstance(suggest, (int, long)) and
suggest in cli.OPT_COMPL_ALL):
key = suggest
elif suggest:
key = " ".join(sorted(suggest))
else:
suggest_text = ""
key = ""
values.setdefault(suggest_text, []).extend(opt.all_names)
values.setdefault(key, []).extend(opt.all_names)
# Don't write any code if there are no option values
if not values:
......@@ -301,7 +304,19 @@ class CompletionWriter:
utils.ShellQuote("|".join(allnames)))
sw.IncIndent()
try:
WriteCompReply(sw, "-W %s" % utils.ShellQuote(suggest), cur=cur)
if suggest == cli.OPT_COMPL_MANY_NODES:
# TODO: Implement comma-separated values
WriteCompReply(sw, "-W ''", cur=cur)
elif suggest == cli.OPT_COMPL_ONE_NODE:
WriteCompReply(sw, "-W \"$(_ganeti_nodes)\"", cur=cur)
elif suggest == cli.OPT_COMPL_ONE_INSTANCE:
WriteCompReply(sw, "-W \"$(_ganeti_instances)\"", cur=cur)
elif suggest == cli.OPT_COMPL_ONE_OS:
WriteCompReply(sw, "-W \"$(_ganeti_os)\"", cur=cur)
elif suggest == cli.OPT_COMPL_ONE_IALLOCATOR:
WriteCompReply(sw, "-W \"$(_ganeti_iallocator)\"", cur=cur)
else:
WriteCompReply(sw, "-W %s" % utils.ShellQuote(suggest), cur=cur)
finally:
sw.DecIndent()
......
......@@ -56,6 +56,9 @@ __all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain",
"ArgInstance", "ArgNode", "ArgChoice", "ArgHost",
"ARGS_NONE", "ARGS_ONE_INSTANCE", "ARGS_ONE_NODE",
"ARGS_MANY_INSTANCES", "ARGS_MANY_NODES",
"OPT_COMPL_ONE_NODE", "OPT_COMPL_ONE_INSTANCE",
"OPT_COMPL_MANY_NODES",
"OPT_COMPL_ONE_OS", "OPT_COMPL_ONE_IALLOCATOR",
]
NO_PREFIX = "no_"
......@@ -330,6 +333,23 @@ def check_key_val(option, opt, value):
return _SplitKeyVal(opt, value)
# completion_suggestion is normally a list. Using numeric values not evaluating
# to False for dynamic completion.
(OPT_COMPL_MANY_NODES,
OPT_COMPL_ONE_NODE,
OPT_COMPL_ONE_INSTANCE,
OPT_COMPL_ONE_OS,
OPT_COMPL_ONE_IALLOCATOR) = range(100, 105)
OPT_COMPL_ALL = frozenset([
OPT_COMPL_MANY_NODES,
OPT_COMPL_ONE_NODE,
OPT_COMPL_ONE_INSTANCE,
OPT_COMPL_ONE_OS,
OPT_COMPL_ONE_IALLOCATOR,
])
class CliOption(Option):
"""Custom option class for optparse.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment