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