1 {-| Implementation of command-line functions.
3 This module holds the common cli-related functions for the binaries,
4 separated into this module since Utils.hs is used in many other places
5 and this is more I/O oriented.
9 module Ganeti.HTools.CLI
15 import System.Console.GetOpt
20 import Text.Printf (printf)
21 import qualified Data.Version
23 import qualified Ganeti.HTools.Version as Version(version)
25 -- | Command line parser, using the 'options' structure.
26 parseOpts :: [String] -- ^ The command line arguments
27 -> String -- ^ The program name
28 -> [OptDescr (b -> b)] -- ^ The supported command line options
29 -> b -- ^ The default options record
30 -> (b -> Bool) -- ^ The function which given the options
31 -- tells us whether we need to show help
32 -> IO (b, [String]) -- ^ The resulting options a leftover
34 parseOpts argv progname options defaultOptions fn =
35 case getOpt Permute options argv of
38 let resu@(po, _) = (foldl (flip id) defaultOptions o, n)
40 putStr $ usageInfo header options
44 ioError (userError (concat errs ++ usageInfo header options))
45 where header = printf "%s %s\nUsage: %s [OPTION...]"
46 progname Version.version progname
49 -- | Return a version string for the program
50 showVersion :: String -- ^ The program name
51 -> String -- ^ The formatted version and other information data
53 printf "%s %s\ncompiled with %s %s\nrunning on %s %s\n"
55 compilerName (Data.Version.showVersion compilerVersion)