Revision a6cdfdcc
b/htools/Ganeti/Common.hs | ||
---|---|---|
30 | 30 |
( GenericOptType |
31 | 31 |
, StandardOptions(..) |
32 | 32 |
, OptCompletion(..) |
33 |
, ArgCompletion(..) |
|
33 | 34 |
, optComplYesNo |
34 | 35 |
, oShowHelp |
35 | 36 |
, oShowVer |
... | ... | |
71 | 72 |
| OptComplInstAddNodes -- ^ Either one or two nodes |
72 | 73 |
| OptComplOneGroup -- ^ One group |
73 | 74 |
| OptComplNumeric -- ^ Float values |
75 |
| OptComplJobId -- ^ Job Id |
|
76 |
| OptComplCommand -- ^ Command (executable) |
|
74 | 77 |
| OptComplString -- ^ Arbitrary string |
75 | 78 |
| OptComplChoices [String] -- ^ List of string choices |
79 |
| OptComplSuggest [String] -- ^ Suggested choices |
|
76 | 80 |
deriving (Show, Read, Eq) |
77 | 81 |
|
82 |
-- | Argument type. This differs from (and wraps) an Option by the |
|
83 |
-- fact that it can (and usually does) support multiple repetitions of |
|
84 |
-- the same argument, via a min and max limit. |
|
85 |
data ArgCompletion = ArgCompletion OptCompletion Int (Maybe Int) |
|
86 |
deriving (Show, Read, Eq) |
|
87 |
|
|
78 | 88 |
-- | Yes\/no choices completion. |
79 | 89 |
optComplYesNo :: OptCompletion |
80 | 90 |
optComplYesNo = OptComplChoices ["yes", "no"] |
... | ... | |
82 | 92 |
-- | Text serialisation for 'OptCompletion', used on the Python side. |
83 | 93 |
complToText :: OptCompletion -> String |
84 | 94 |
complToText (OptComplChoices choices) = "choices " ++ intercalate "," choices |
95 |
complToText (OptComplSuggest choices) = "suggest " ++ intercalate "," choices |
|
85 | 96 |
complToText compl = |
86 | 97 |
let show_compl = show compl |
87 | 98 |
stripped = stripPrefix "OptCompl" show_compl |
88 | 99 |
in map toLower $ fromMaybe show_compl stripped |
89 | 100 |
|
101 |
-- | Tex serialisation for 'ArgCompletion'. |
|
102 |
argComplToText :: ArgCompletion -> String |
|
103 |
argComplToText (ArgCompletion optc min_cnt max_cnt) = |
|
104 |
complToText optc ++ " " ++ show min_cnt ++ " " ++ maybe "none" show max_cnt |
|
105 |
|
|
90 | 106 |
-- | Abrreviation for the option type. |
91 | 107 |
type GenericOptType a = (OptDescr (a -> Result a), OptCompletion) |
92 | 108 |
|
... | ... | |
131 | 147 |
os arch |
132 | 148 |
|
133 | 149 |
-- | Show completion info. |
134 |
completionInfo :: String -> [GenericOptType a] -> String |
|
135 |
completionInfo _ = |
|
136 |
unlines .
|
|
150 |
completionInfo :: String -> [GenericOptType a] -> [ArgCompletion] -> String
|
|
151 |
completionInfo _ opts args =
|
|
152 |
unlines $
|
|
137 | 153 |
map (\(Option shorts longs _ _, compinfo) -> |
138 | 154 |
let all_opts = map (\c -> ['-', c]) shorts ++ map ("--" ++) longs |
139 | 155 |
in intercalate "," all_opts ++ " " ++ complToText compinfo |
140 |
) |
|
156 |
) opts ++ |
|
157 |
map argComplToText args |
|
141 | 158 |
|
142 | 159 |
-- | Helper for parsing a yes\/no command line flag. |
143 | 160 |
parseYesNo :: Bool -- ^ Default value (when we get a @Nothing@) |
... | ... | |
199 | 216 |
, (verRequested parsed, |
200 | 217 |
Left (ExitSuccess, versionInfo progname)) |
201 | 218 |
, (compRequested parsed, |
202 |
Left (ExitSuccess, completionInfo progname options)) |
|
219 |
Left (ExitSuccess, completionInfo progname options []))
|
|
203 | 220 |
] |
204 | 221 |
(_, _, errs) -> |
205 | 222 |
Left (ExitFailure 2, "Command line error: " ++ concat errs ++ "\n" ++ |
Also available in: Unified diff