Revision 519edd9f htools/Ganeti/HTools/CLI.hs

b/htools/Ganeti/HTools/CLI.hs
51 51
    , oIVcpus
52 52
    , oInstMoves
53 53
    , oLuxiSocket
54
    , oMachineReadable
54 55
    , oMaxCpu
55 56
    , oMaxSolLength
56 57
    , oMinDisk
......
115 116
    , optSelInst     :: [String]       -- ^ Instances to be excluded
116 117
    , optISpec       :: RSpec          -- ^ Requested instance specs
117 118
    , optLuxi        :: Maybe FilePath -- ^ Collect data from Luxi
119
    , optMachineReadable :: Bool       -- ^ Output machine-readable format
118 120
    , optMaster      :: String         -- ^ Collect data from RAPI
119 121
    , optMaxLength   :: Int            -- ^ Stop after this many steps
120 122
    , optMcpu        :: Double         -- ^ Max cpu ratio for nodes
......
154 156
 , optSelInst     = []
155 157
 , optISpec       = RSpec 1 4096 102400
156 158
 , optLuxi        = Nothing
159
 , optMachineReadable = False
157 160
 , optMaster      = ""
158 161
 , optMaxLength   = -1
159 162
 , optMcpu        = defVcpuRatio
......
277 280
                       fromMaybe defaultLuxiSocket) "SOCKET")
278 281
              "collect data via Luxi, optionally using the given SOCKET path"
279 282

  
283
oMachineReadable :: OptType
284
oMachineReadable = Option "" ["machine-readable"]
285
          (OptArg (\ f opts -> do
286
                     flag <- parseYesNo True f
287
                     return $ opts { optMachineReadable = flag }) "CHOICE")
288
          "enable machine readable output (pass either 'yes' or 'no' to\
289
          \ explicitely control the flag, or without an argument defaults to\
290
          \ yes"
291

  
280 292
oMaxCpu :: OptType
281 293
oMaxCpu = Option "" ["max-cpu"]
282 294
          (ReqArg (\ n opts -> Ok opts { optMcpu = read n }) "RATIO")
......
416 428

  
417 429
-- * Functions
418 430

  
431
-- | Helper for parsing a yes\/no command line flag.
432
parseYesNo :: Bool         -- ^ Default whalue (when we get a @Nothing@)
433
           -> Maybe String -- ^ Parameter value
434
           -> Result Bool  -- ^ Resulting boolean value
435
parseYesNo v Nothing      = return v
436
parseYesNo _ (Just "yes") = return True
437
parseYesNo _ (Just "no")  = return False
438
parseYesNo _ (Just s)     = fail $ "Invalid choice '" ++ s ++
439
                            "', pass one of 'yes' or 'no'"
440

  
419 441
-- | Usage info.
420 442
usageHelp :: String -> [OptType] -> String
421 443
usageHelp progname =

Also available in: Unified diff