Revision 097ad7ee

b/htools/Ganeti/Common.hs
33 33
  , optComplYesNo
34 34
  , oShowHelp
35 35
  , oShowVer
36
  , oShowComp
36 37
  , usageHelp
37 38
  , versionInfo
38 39
  , reqWithConversion
......
42 43
  ) where
43 44

  
44 45
import Control.Monad (foldM)
46
import Data.Char (toLower)
47
import Data.List (intercalate, stripPrefix)
48
import Data.Maybe (fromMaybe)
45 49
import qualified Data.Version
46 50
import System.Console.GetOpt
47 51
import System.Exit
......
75 79
optComplYesNo :: OptCompletion
76 80
optComplYesNo = OptComplChoices ["yes", "no"]
77 81

  
82
-- | Text serialisation for 'OptCompletion', used on the Python side.
83
complToText :: OptCompletion -> String
84
complToText (OptComplChoices choices) = "choices " ++ intercalate "," choices
85
complToText compl =
86
  let show_compl = show compl
87
      stripped = stripPrefix "OptCompl" show_compl
88
  in map toLower $ fromMaybe show_compl stripped
89

  
78 90
-- | Abrreviation for the option type.
79 91
type GenericOptType a = (OptDescr (a -> Result a), OptCompletion)
80 92

  
......
82 94
class StandardOptions a where
83 95
  helpRequested :: a -> Bool
84 96
  verRequested  :: a -> Bool
97
  compRequested :: a -> Bool
85 98
  requestHelp   :: a -> a
86 99
  requestVer    :: a -> a
100
  requestComp   :: a -> a
87 101

  
88
-- | Options to request help output.
102
-- | Option to request help output.
89 103
oShowHelp :: (StandardOptions a) => GenericOptType a
90 104
oShowHelp = (Option "h" ["help"] (NoArg (Ok . requestHelp)) "show help",
91 105
             OptComplNone)
......
96 110
            "show the version of the program",
97 111
            OptComplNone)
98 112

  
113
-- | Option to request completion information
114
oShowComp :: (StandardOptions a) => GenericOptType a
115
oShowComp =
116
  (Option "" ["help-completion"] (NoArg (Ok . requestComp) )
117
   "show completion info", OptComplNone)
118

  
99 119
-- | Usage info.
100 120
usageHelp :: String -> [GenericOptType a] -> String
101 121
usageHelp progname =
......
110 130
         (Data.Version.showVersion compilerVersion)
111 131
         os arch
112 132

  
133
-- | Show completion info.
134
completionInfo :: String -> [GenericOptType a] -> String
135
completionInfo _ =
136
  unlines .
137
  map (\(Option shorts longs _ _, compinfo) ->
138
         let all_opts = map (\c -> ['-', c]) shorts ++ map ("--" ++) longs
139
         in intercalate "," all_opts ++ " " ++ complToText compinfo
140
      )
141

  
113 142
-- | Helper for parsing a yes\/no command line flag.
114 143
parseYesNo :: Bool         -- ^ Default value (when we get a @Nothing@)
115 144
           -> Maybe String -- ^ Parameter value
......
169 198
                    Left (ExitSuccess, usageHelp progname options))
170 199
                 , (verRequested parsed,
171 200
                    Left (ExitSuccess, versionInfo progname))
201
                 , (compRequested parsed,
202
                    Left (ExitSuccess, completionInfo progname options))
172 203
                 ]
173 204
    (_, _, errs) ->
174 205
      Left (ExitFailure 2, "Command line error: "  ++ concat errs ++ "\n" ++
b/htools/Ganeti/Daemon.hs
81 81
data DaemonOptions = DaemonOptions
82 82
  { optShowHelp     :: Bool           -- ^ Just show the help
83 83
  , optShowVer      :: Bool           -- ^ Just show the program version
84
  , optShowComp     :: Bool           -- ^ Just show the completion info
84 85
  , optDaemonize    :: Bool           -- ^ Whether to daemonize or not
85 86
  , optPort         :: Maybe Word16   -- ^ Override for the network port
86 87
  , optDebug        :: Bool           -- ^ Enable debug messages
......
94 95
defaultOptions  = DaemonOptions
95 96
  { optShowHelp     = False
96 97
  , optShowVer      = False
98
  , optShowComp     = False
97 99
  , optDaemonize    = True
98 100
  , optPort         = Nothing
99 101
  , optDebug        = False
......
105 107
instance StandardOptions DaemonOptions where
106 108
  helpRequested = optShowHelp
107 109
  verRequested  = optShowVer
110
  compRequested = optShowComp
108 111
  requestHelp o = o { optShowHelp = True }
109 112
  requestVer  o = o { optShowVer  = True }
113
  requestComp o = o { optShowComp = True }
110 114

  
111 115
-- | Abrreviation for the option type.
112 116
type OptType = GenericOptType DaemonOptions
......
165 169
genericOpts :: [OptType]
166 170
genericOpts = [ oShowHelp
167 171
              , oShowVer
172
              , oShowComp
168 173
              ]
169 174

  
170 175
-- | Small wrapper over getArgs and 'parseOpts'.
b/htools/Ganeti/HTools/CLI.hs
77 77
  , oSelInst
78 78
  , oShowHelp
79 79
  , oShowVer
80
  , oShowComp
80 81
  , oStdSpec
81 82
  , oTieredSpec
82 83
  , oVerbose
......
132 133
  , optSaveCluster :: Maybe FilePath -- ^ Save cluster state to this file
133 134
  , optShowCmds    :: Maybe FilePath -- ^ Whether to show the command list
134 135
  , optShowHelp    :: Bool           -- ^ Just show the help
136
  , optShowComp    :: Bool           -- ^ Just show the completion info
135 137
  , optShowInsts   :: Bool           -- ^ Whether to show the instance map
136 138
  , optShowNodes   :: Maybe [String] -- ^ Whether to show node status
137 139
  , optShowVer     :: Bool           -- ^ Just show the program version
......
175 177
  , optSaveCluster = Nothing
176 178
  , optShowCmds    = Nothing
177 179
  , optShowHelp    = False
180
  , optShowComp    = False
178 181
  , optShowInsts   = False
179 182
  , optShowNodes   = Nothing
180 183
  , optShowVer     = False
......
191 194
instance StandardOptions Options where
192 195
  helpRequested = optShowHelp
193 196
  verRequested  = optShowVer
197
  compRequested = optShowComp
194 198
  requestHelp o = o { optShowHelp = True }
195 199
  requestVer  o = o { optShowVer  = True }
200
  requestComp o = o { optShowComp = True }
196 201

  
197 202
-- * Helper functions
198 203

  
......
512 517
genericOpts :: [GenericOptType Options]
513 518
genericOpts =  [ oShowVer
514 519
               , oShowHelp
520
               , oShowComp
515 521
               ]
516 522

  
517 523
-- * Functions

Also available in: Unified diff