From 580ef58d723fa359e477f675ea5ca40abaf3d9b4 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 27 Aug 2009 17:35:36 +0200
Subject: [PATCH] Bash completion: Small optimizations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

$2 doesn't contain the correct value when completing something like
"--disk-template=…". Getting it via COMP_WORDS is better.

Short options (e.g. -I) can't have an equal sign.

Also add useful debugging commands for development.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Olivier Tharan <olive@google.com>
---
 autotools/build-bash-completion | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/autotools/build-bash-completion b/autotools/build-bash-completion
index ca3ed4ff6..497db89c1 100755
--- a/autotools/build-bash-completion
+++ b/autotools/build-bash-completion
@@ -208,7 +208,8 @@ class CompletionWriter:
       if opt.takes_value():
         # Ignore value
         for i in opt.all_names:
-          ignore.append("%s=*" % utils.ShellQuote(i))
+          if i.startswith("--"):
+            ignore.append("%s=*" % utils.ShellQuote(i))
           skip_one.append(utils.ShellQuote(i))
       else:
         ignore.extend([utils.ShellQuote(i) for i in opt.all_names])
@@ -392,8 +393,14 @@ def WriteCompletion(sw, scriptname, funcname,
   sw.Write("%s() {", funcname)
   sw.IncIndent()
   try:
-    sw.Write('local cur="$2" prev="$3"')
-    sw.Write("local i first_arg_idx choices compgenargs arg_idx")
+    sw.Write("local "
+             ' cur="${COMP_WORDS[$COMP_CWORD]}"'
+             ' prev="${COMP_WORDS[COMP_CWORD-1]}"'
+             ' i first_arg_idx choices compgenargs arg_idx')
+
+    # Useful for debugging:
+    #sw.Write("echo cur=\"$cur\" prev=\"$prev\"")
+    #sw.Write("set | grep ^COMP_")
 
     sw.Write("COMPREPLY=()")
 
-- 
GitLab