- sw.Write("if [[ $COMP_CWORD -gt %s ]]; then", self.arg_offset + 1)
- sw.IncIndent()
- try:
- sw.Write("""case "$prev" in""")
- for (choices, names) in values.iteritems():
- # TODO: Implement completion for --foo=bar form
- sw.Write("%s)", "|".join([utils.ShellQuote(i) for i in names]))
- sw.IncIndent()
- try:
- WriteCompReply(sw, "-W %s" % utils.ShellQuote(choices))
- finally:
- sw.DecIndent()
- sw.Write(";;")
- sw.Write("""esac""")
- finally:
- sw.DecIndent()
- sw.Write("""fi""")
+ cur = "\"$optcur\""
+
+ wrote_opt = False
+
+ for (suggest, allnames) in values.iteritems():
+ longnames = [i for i in allnames if i.startswith("--")]
+
+ if wrote_opt:
+ condcmd = "elif"
+ else:
+ condcmd = "if"
+
+ sw.Write("%s _ganeti_checkopt %s %s; then", condcmd,
+ utils.ShellQuote("|".join(["%s=*" % i for i in longnames])),
+ utils.ShellQuote("|".join(allnames)))
+ sw.IncIndent()
+ try:
+ 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)
+ elif suggest == cli.OPT_COMPL_INST_ADD_NODES:
+ sw.Write("local tmp= node1= pfx= curvalue=\"${optcur#*:}\"")
+
+ sw.Write("if [[ \"$optcur\" == *:* ]]; then")
+ sw.IncIndent()
+ try:
+ sw.Write("node1=\"${optcur%%:*}\"")
+
+ sw.Write("if [[ \"$COMP_WORDBREAKS\" != *:* ]]; then")
+ sw.IncIndent()
+ try:
+ sw.Write("pfx=\"$node1:\"")
+ finally:
+ sw.DecIndent()
+ sw.Write("fi")
+ finally:
+ sw.DecIndent()
+ sw.Write("fi")
+
+ sw.Write("_ganeti_dbglog pfx=\"'$pfx'\" curvalue=\"'$curvalue'\""
+ " node1=\"'$node1'\"")
+
+ sw.Write("for i in $(_ganeti_nodes); do")
+ sw.IncIndent()
+ try:
+ sw.Write("if [[ -z \"$node1\" ]]; then")
+ sw.IncIndent()
+ try:
+ sw.Write("tmp=\"$tmp $i $i:\"")
+ finally:
+ sw.DecIndent()
+ sw.Write("elif [[ \"$i\" != \"$node1\" ]]; then")
+ sw.IncIndent()
+ try:
+ sw.Write("tmp=\"$tmp $i\"")
+ finally:
+ sw.DecIndent()
+ sw.Write("fi")
+ finally:
+ sw.DecIndent()
+ sw.Write("done")
+
+ WriteCompReply(sw, "-P \"$pfx\" -W \"$tmp\"", cur="\"$curvalue\"")
+ else:
+ WriteCompReply(sw, "-W %s" % utils.ShellQuote(suggest), cur=cur)
+ finally:
+ sw.DecIndent()
+
+ wrote_opt = True
+
+ if wrote_opt:
+ sw.Write("fi")
+
+ return