Revision 75d1edf8

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 =
b/hail.hs
41 41
    , optShowHelp  :: Bool           -- ^ Just show the help
42 42
    } deriving Show
43 43

  
44
instance CLI.CLIOptions Options where
45
    showVersion = optShowVer
46
    showHelp    = optShowHelp
47

  
44 48
-- | Default values for the command line options.
45 49
defaultOptions :: Options
46 50
defaultOptions  = Options
......
163 167
main = do
164 168
  cmd_args <- System.getArgs
165 169
  (opts, args) <- CLI.parseOpts cmd_args "hail" options
166
                  defaultOptions optShowHelp
167

  
168
  when (optShowVer opts) $ do
169
         putStr $ CLI.showVersion "hbal"
170
         exitWith ExitSuccess
170
                  defaultOptions
171 171

  
172 172
  when (null args) $ do
173 173
         hPutStrLn stderr "Error: this program needs an input file."
b/hbal.hs
44 44
    , optShowHelp  :: Bool           -- ^ Just show the help
45 45
    } deriving Show
46 46

  
47
instance CLI.CLIOptions Options where
48
    showVersion = optShowVer
49
    showHelp    = optShowHelp
50

  
47 51
-- | Default values for the command line options.
48 52
defaultOptions :: Options
49 53
defaultOptions  = Options
......
165 169
main :: IO ()
166 170
main = do
167 171
  cmd_args <- System.getArgs
168
  (opts, args) <- CLI.parseOpts cmd_args "hbal" options
169
                  defaultOptions optShowHelp
172
  (opts, args) <- CLI.parseOpts cmd_args "hbal" options defaultOptions
170 173

  
171 174
  unless (null args) $ do
172 175
         hPutStrLn stderr "Error: this program doesn't take any arguments."
173 176
         exitWith $ ExitFailure 1
174 177

  
175
  when (optShowVer opts) $ do
176
         putStr $ CLI.showVersion "hbal"
177
         exitWith ExitSuccess
178

  
179 178
  (env_node, env_inst) <- CLI.parseEnv ()
180 179
  let nodef = if optNodeSet opts then optNodef opts
181 180
              else env_node
b/hn1.hs
40 40
    , optShowHelp    :: Bool     -- ^ Just show the help
41 41
    } deriving Show
42 42

  
43
instance CLI.CLIOptions Options where
44
    showVersion = optShowVer
45
    showHelp    = optShowHelp
46

  
43 47
-- | Default values for the command line options.
44 48
defaultOptions :: Options
45 49
defaultOptions    = Options
......
130 134
main = do
131 135
  cmd_args <- System.getArgs
132 136
  (opts, args) <- CLI.parseOpts cmd_args "hn1" options
133
                  defaultOptions optShowHelp
137
                  defaultOptions
134 138

  
135 139
  unless (null args) $ do
136 140
         hPutStrLn stderr "Error: this program doesn't take any arguments."
137 141
         exitWith $ ExitFailure 1
138 142

  
139
  when (optShowVer opts) $ do
140
         putStr $ CLI.showVersion "hn1"
141
         exitWith ExitSuccess
142

  
143 143
  (env_node, env_inst) <- CLI.parseEnv ()
144 144
  let nodef = if optNodeSet opts then optNodef opts
145 145
              else env_node
b/hscan.hs
35 35
    , optShowHelp  :: Bool     -- ^ Just show the help
36 36
    } deriving Show
37 37

  
38
instance CLI.CLIOptions Options where
39
    showVersion = optShowVer
40
    showHelp    = optShowHelp
41

  
38 42
-- | Default values for the command line options.
39 43
defaultOptions :: Options
40 44
defaultOptions  = Options
......
141 145
main = do
142 146
  cmd_args <- System.getArgs
143 147
  (opts, clusters) <- CLI.parseOpts cmd_args "hscan" options
144
                      defaultOptions optShowHelp
145

  
146
  when (optShowVer opts) $ do
147
         putStr $ CLI.showVersion "hscan"
148
         exitWith ExitSuccess
148
                      defaultOptions
149 149

  
150 150
  let odir = optOutPath opts
151 151
      nlen = maximum . map length $ clusters

Also available in: Unified diff