Revision 75d1edf8 Ganeti/HTools/CLI.hs

b/Ganeti/HTools/CLI.hs
7 7
-}
8 8

  
9 9
module Ganeti.HTools.CLI
10
    (
11
      parseOpts
10
    ( CLIOptions(..)
11
    , parseOpts
12 12
    , parseEnv
13
    , showVersion
14 13
    , shTemplate
15 14
    ) where
16 15

  
......
25 24

  
26 25
import qualified Ganeti.HTools.Version as Version(version)
27 26

  
27
-- | Class for types which support show help and show version
28
class CLIOptions a where
29
    showHelp    :: a -> Bool
30
    showVersion :: a -> Bool
31

  
28 32
-- | Command line parser, using the 'options' structure.
29
parseOpts :: [String]            -- ^ The command line arguments
33
parseOpts :: (CLIOptions b) =>
34
             [String]            -- ^ The command line arguments
30 35
          -> String              -- ^ The program name
31 36
          -> [OptDescr (b -> b)] -- ^ The supported command line options
32 37
          -> b                   -- ^ The default options record
33
          -> (b -> Bool)         -- ^ The function which given the options
34
                                 -- tells us whether we need to show help
35 38
          -> IO (b, [String])    -- ^ The resulting options a leftover
36 39
                                 -- arguments
37
parseOpts argv progname options defaultOptions fn =
40
parseOpts argv progname options defaultOptions =
38 41
    case getOpt Permute options argv of
39 42
      (o, n, []) ->
40 43
          do
41 44
            let resu@(po, _) = (foldl (flip id) defaultOptions o, n)
42
            when (fn po) $ do
45
            when (showHelp po) $ do
43 46
              putStr $ usageInfo header options
44 47
              exitWith ExitSuccess
48
            when (showVersion po) $ do
49
              printf "%s %s\ncompiled with %s %s\nrunning on %s %s\n"
50
                     progname Version.version
51
                     compilerName (Data.Version.showVersion compilerVersion)
52
                     os arch
53
              exitWith ExitSuccess
45 54
            return resu
46 55
      (_, _, errs) ->
47 56
          ioError (userError (concat errs ++ usageInfo header options))
......
56 65
  b <- getEnvDefault "HTOOLS_INSTANCES" "instances"
57 66
  return (a, b)
58 67

  
59
-- | Return a version string for the program
60
showVersion :: String -- ^ The program name
61
            -> String -- ^ The formatted version and other information data
62
showVersion name =
63
    printf "%s %s\ncompiled with %s %s\nrunning on %s %s\n"
64
           name Version.version
65
           compilerName (Data.Version.showVersion compilerVersion)
66
           os arch
67

  
68 68
-- | A shell script template for autogenerated scripts
69 69
shTemplate :: String
70 70
shTemplate =

Also available in: Unified diff