From 632d5090e9caffc8ceb6ff0dac24027679389c94 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 27 Aug 2009 17:36:22 +0200
Subject: [PATCH] Bash completion: Support for --foo=bar option format

That is with the equal sign.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 autotools/build-bash-completion | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/autotools/build-bash-completion b/autotools/build-bash-completion
index 497db89c1..65414d70a 100755
--- a/autotools/build-bash-completion
+++ b/autotools/build-bash-completion
@@ -183,8 +183,8 @@ def WritePreamble(sw):
   sw.Write("}")
 
 
-def WriteCompReply(sw, args):
-  sw.Write("""COMPREPLY=( $(compgen %s -- "$cur") )""", args)
+def WriteCompReply(sw, args, cur="\"$cur\""):
+  sw.Write("""COMPREPLY=( $(compgen %s -- %s) )""", args, cur)
   sw.Write("return")
 
 
@@ -257,9 +257,9 @@ class CompletionWriter:
     sw.Write("if [[ $COMP_CWORD -gt %s ]]; then", self.arg_offset + 1)
     sw.IncIndent()
     try:
+      # --foo value
       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:
@@ -272,6 +272,28 @@ class CompletionWriter:
       sw.DecIndent()
     sw.Write("""fi""")
 
+    # --foo=value
+    values_longopts = {}
+
+    for (choices, names) in values.iteritems():
+      longnames = [i for i in names if i.startswith("--")]
+      if longnames:
+        values_longopts[choices] = longnames
+
+    if values_longopts:
+      sw.Write("""case "$cur" in""")
+      for (choices, names) in values_longopts.iteritems():
+        sw.Write("%s)", "|".join([utils.ShellQuote(i) + "=*" for i in names]))
+        sw.IncIndent()
+        try:
+          # Shell expression to get option value
+          cur="\"${cur#--*=}\""
+          WriteCompReply(sw, "-W %s" % utils.ShellQuote(choices), cur=cur)
+        finally:
+          sw.DecIndent()
+        sw.Write(";;")
+      sw.Write("""esac""")
+
   def _CompleteArguments(self, sw):
     if not (self.opts or self.args):
       return
@@ -394,7 +416,7 @@ def WriteCompletion(sw, scriptname, funcname,
   sw.IncIndent()
   try:
     sw.Write("local "
-             ' cur="${COMP_WORDS[$COMP_CWORD]}"'
+             ' cur="${COMP_WORDS[COMP_CWORD]}"'
              ' prev="${COMP_WORDS[COMP_CWORD-1]}"'
              ' i first_arg_idx choices compgenargs arg_idx')
 
-- 
GitLab