diff --git a/htools/Ganeti/Common.hs b/htools/Ganeti/Common.hs index cd82889603d3426cbd78d9176b63a613856968ca..cf80caa8ff3a805d4910be9eab18dd4460791d4f 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 a25221a1fba7968d6454d4f120ec79bab246ed89..e23fcd3953250abc284d5080055689818cfecf52 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."