From 814e1e23a1e75d8adbb3f7477474a61d7ab1aa3e Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Wed, 19 Dec 2012 23:29:30 +0100
Subject: [PATCH] Display command description in htools usage message

By abstracting the command/personality formatting, we can easily
switch htools to the same output as the rest of the commands.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Helga Velroyen <helgav@google.com>
---
 htools/Ganeti/Common.hs | 17 +++++++++++------
 htools/htools.hs        |  5 +++--
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/htools/Ganeti/Common.hs b/htools/Ganeti/Common.hs
index cd8288960..cf80caa8f 100644
--- a/htools/Ganeti/Common.hs
+++ b/htools/Ganeti/Common.hs
@@ -38,6 +38,7 @@ module Ganeti.Common
   , oShowComp
   , usageHelp
   , versionInfo
+  , formatCommands
   , reqWithConversion
   , parseYesNo
   , parseOpts
@@ -199,20 +200,24 @@ reqWithConversion conversion_fn updater_fn =
 maxCmdLen :: Int
 maxCmdLen = 60
 
+-- | Formats the description of various commands.
+formatCommands :: (StandardOptions a) => PersonalityList a -> [String]
+formatCommands personalities =
+  -- FIXME: add wrapping of descriptions
+  map (\(cmd, (_, _, _, desc)) -> printf " %-*s - %s" mlen cmd desc::String) $
+  sortBy (comparing fst) personalities
+    where mlen = min maxCmdLen . maximum $ map (length . fst) personalities
+
 -- | Formats usage for a multi-personality program.
 formatCmdUsage :: (StandardOptions a) => String -> PersonalityList a -> String
 formatCmdUsage prog personalities =
-  let mlen = min maxCmdLen . maximum $ map (length . fst) personalities
-      sorted = sortBy (comparing fst) personalities
-      header = [ printf "Usage: %s {command} [options...] [argument...]" prog
+  let header = [ printf "Usage: %s {command} [options...] [argument...]" prog
                , printf "%s <command> --help to see details, or man %s"
                    prog prog
                , ""
                , "Commands:"
                ]
-      rows = map (\(cmd, (_, _, _, desc)) ->
-                    -- FIXME: not wrapped here
-                    printf " %-*s - %s" mlen cmd desc::String) sorted
+      rows = formatCommands personalities
   in unlines $ header ++ rows
 
 -- | Displays usage for a program and exits.
diff --git a/htools/htools.hs b/htools/htools.hs
index a25221a1f..e23fcd395 100644
--- a/htools/htools.hs
+++ b/htools/htools.hs
@@ -32,9 +32,10 @@ import System.Environment
 import System.IO
 import System.IO.Error (isDoesNotExistError)
 
-import Ganeti.Utils
+import Ganeti.Common (formatCommands)
 import Ganeti.HTools.CLI (parseOpts, genericOpts)
 import Ganeti.HTools.Program (personalities)
+import Ganeti.Utils
 
 -- | Display usage and exit.
 usage :: String -> IO ()
@@ -42,7 +43,7 @@ usage name = do
   hPutStrLn stderr $ "Unrecognised personality '" ++ name ++ "'."
   hPutStrLn stderr "This program must be installed under one of the following\
                    \ names:"
-  mapM_ (hPutStrLn stderr . ("  - " ++) . fst) personalities
+  hPutStrLn stderr . unlines $ formatCommands personalities
   exitErr "Please either rename/symlink the program or set\n\
           \the environment variable HTOOLS to the desired role."
 
-- 
GitLab