From b33cad4a485869cb783cb73249a0cfa85791bc1b Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Wed, 17 Aug 2011 12:57:07 +0200
Subject: [PATCH] bash completion: Group commands by arguments and options

This grouping, which was a TODO for a long time, reduces the script size
by about 5kB.

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

diff --git a/autotools/build-bash-completion b/autotools/build-bash-completion
index bfea35174..6e708538f 100755
--- a/autotools/build-bash-completion
+++ b/autotools/build-bash-completion
@@ -553,20 +553,23 @@ def WriteCompletion(sw, scriptname, funcname,
         sw.DecIndent()
       sw.Write("fi")
 
-      # We're doing options and arguments to commands
-      sw.Write("""case "${COMP_WORDS[1]}" in""")
+      # Group commands by arguments and options
+      grouped_cmds = {}
       for cmd, (_, argdef, optdef, _, _) in commands.iteritems():
         if not (argdef or optdef):
           continue
+        grouped_cmds.setdefault((tuple(argdef), tuple(optdef)), set()).add(cmd)
 
-        # TODO: Group by arguments and options
-        sw.Write("%s)", utils.ShellQuote(cmd))
+      # We're doing options and arguments to commands
+      sw.Write("""case "${COMP_WORDS[1]}" in""")
+      for ((argdef, optdef), cmds) in grouped_cmds.items():
+        assert argdef or optdef
+        sw.Write("%s)", "|".join(map(utils.ShellQuote, sorted(cmds))))
         sw.IncIndent()
         try:
           CompletionWriter(1, optdef, argdef).WriteTo(sw)
         finally:
           sw.DecIndent()
-
         sw.Write(";;")
       sw.Write("esac")
   finally:
-- 
GitLab