Commit 519edd9f authored by Iustin Pop's avatar Iustin Pop
Browse files

htools: add a machine-readable CLI flag



This will be used in hspace to toggle between "human" readable
and machine readable output formats.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent d7731f51
...@@ -51,6 +51,7 @@ module Ganeti.HTools.CLI ...@@ -51,6 +51,7 @@ module Ganeti.HTools.CLI
, oIVcpus , oIVcpus
, oInstMoves , oInstMoves
, oLuxiSocket , oLuxiSocket
, oMachineReadable
, oMaxCpu , oMaxCpu
, oMaxSolLength , oMaxSolLength
, oMinDisk , oMinDisk
...@@ -115,6 +116,7 @@ data Options = Options ...@@ -115,6 +116,7 @@ data Options = Options
, optSelInst :: [String] -- ^ Instances to be excluded , optSelInst :: [String] -- ^ Instances to be excluded
, optISpec :: RSpec -- ^ Requested instance specs , optISpec :: RSpec -- ^ Requested instance specs
, optLuxi :: Maybe FilePath -- ^ Collect data from Luxi , optLuxi :: Maybe FilePath -- ^ Collect data from Luxi
, optMachineReadable :: Bool -- ^ Output machine-readable format
, optMaster :: String -- ^ Collect data from RAPI , optMaster :: String -- ^ Collect data from RAPI
, optMaxLength :: Int -- ^ Stop after this many steps , optMaxLength :: Int -- ^ Stop after this many steps
, optMcpu :: Double -- ^ Max cpu ratio for nodes , optMcpu :: Double -- ^ Max cpu ratio for nodes
...@@ -154,6 +156,7 @@ defaultOptions = Options ...@@ -154,6 +156,7 @@ defaultOptions = Options
, optSelInst = [] , optSelInst = []
, optISpec = RSpec 1 4096 102400 , optISpec = RSpec 1 4096 102400
, optLuxi = Nothing , optLuxi = Nothing
, optMachineReadable = False
, optMaster = "" , optMaster = ""
, optMaxLength = -1 , optMaxLength = -1
, optMcpu = defVcpuRatio , optMcpu = defVcpuRatio
...@@ -277,6 +280,15 @@ oLuxiSocket = Option "L" ["luxi"] ...@@ -277,6 +280,15 @@ oLuxiSocket = Option "L" ["luxi"]
fromMaybe defaultLuxiSocket) "SOCKET") fromMaybe defaultLuxiSocket) "SOCKET")
"collect data via Luxi, optionally using the given SOCKET path" "collect data via Luxi, optionally using the given SOCKET path"
oMachineReadable :: OptType
oMachineReadable = Option "" ["machine-readable"]
(OptArg (\ f opts -> do
flag <- parseYesNo True f
return $ opts { optMachineReadable = flag }) "CHOICE")
"enable machine readable output (pass either 'yes' or 'no' to\
\ explicitely control the flag, or without an argument defaults to\
\ yes"
oMaxCpu :: OptType oMaxCpu :: OptType
oMaxCpu = Option "" ["max-cpu"] oMaxCpu = Option "" ["max-cpu"]
(ReqArg (\ n opts -> Ok opts { optMcpu = read n }) "RATIO") (ReqArg (\ n opts -> Ok opts { optMcpu = read n }) "RATIO")
...@@ -416,6 +428,16 @@ oVerbose = Option "v" ["verbose"] ...@@ -416,6 +428,16 @@ oVerbose = Option "v" ["verbose"]
-- * Functions -- * Functions
-- | Helper for parsing a yes\/no command line flag.
parseYesNo :: Bool -- ^ Default whalue (when we get a @Nothing@)
-> Maybe String -- ^ Parameter value
-> Result Bool -- ^ Resulting boolean value
parseYesNo v Nothing = return v
parseYesNo _ (Just "yes") = return True
parseYesNo _ (Just "no") = return False
parseYesNo _ (Just s) = fail $ "Invalid choice '" ++ s ++
"', pass one of 'yes' or 'no'"
-- | Usage info. -- | Usage info.
usageHelp :: String -> [OptType] -> String usageHelp :: String -> [OptType] -> String
usageHelp progname = usageHelp progname =
......
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