From daf0de6885504ac9bc63f819f96c105d5e1cee89 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 17 Dec 2012 15:34:51 +0100
Subject: [PATCH] Implement fake completion support for multi-cmd binaries

This will allow build-bash-completion to understand what is the list
of commands that each binary supports, and then provide recursively
completion support for each command.

Note: by "fake", I mean we use hand-built output and hardcoded
"--help-completion" matching, as opposed to automatically doing it via
regular options. It could be possible in the future to use a
two-staged command line parser, but for now not sure if it's worth it.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michele Tartara <mtartara@google.com>
---
 htools/Ganeti/Common.hs | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/htools/Ganeti/Common.hs b/htools/Ganeti/Common.hs
index 1906b7f49..cd8288960 100644
--- a/htools/Ganeti/Common.hs
+++ b/htools/Ganeti/Common.hs
@@ -228,6 +228,19 @@ showCmdUsage prog personalities success = do
     then exitSuccess
     else exitWith $ ExitFailure C.exitFailure
 
+-- | Generates completion information for a multi-command binary.
+multiCmdCompletion :: (StandardOptions a) => PersonalityList a -> String
+multiCmdCompletion personalities =
+  unlines .
+  map argComplToText $
+  map (\(cmd, _) -> ArgCompletion (OptComplChoices [cmd]) 1 (Just 1))
+    personalities
+
+-- | Displays completion information for a multi-command binary and exits.
+showCmdCompletion :: (StandardOptions a) => PersonalityList a -> IO b
+showCmdCompletion personalities =
+  putStr (multiCmdCompletion personalities) >> exitSuccess
+
 -- | Command line parser, using a generic 'Options' structure.
 parseOpts :: (StandardOptions a) =>
              a                      -- ^ The default options
@@ -260,6 +273,7 @@ parseOptsCmds defaults argv progname personalities genopts = do
                   -- hardcoded option strings here!
                   "--version" -> putStrLn (versionInfo progname) >> exitSuccess
                   "--help"    -> usage True
+                  "--help-completion" -> showCmdCompletion personalities
                   _           -> return c
   (cmd, cmd_args) <- case argv of
                        cmd:cmd_args -> do
-- 
GitLab