Commit 814e1e23 authored by Iustin Pop's avatar Iustin Pop
Browse files

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: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarHelga Velroyen <helgav@google.com>
parent 044fff06
......@@ -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.
......
......@@ -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."
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment