Implement fake completion support for multi-cmd binaries
authorIustin Pop <iustin@google.com>
Mon, 17 Dec 2012 14:34:51 +0000 (15:34 +0100)
committerIustin Pop <iustin@google.com>
Wed, 19 Dec 2012 11:10:25 +0000 (12:10 +0100)
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

index 1906b7f..cd82889 100644 (file)
@@ -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