-
Notifications
You must be signed in to change notification settings - Fork 114
/
Common.hs
324 lines (288 loc) · 10.8 KB
/
Common.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
{-# LANGUAGE Rank2Types #-}
module Options.Applicative.Common (
-- * Option parsers
--
-- | A 'Parser' is composed of a list of options. Several kinds of options
-- are supported:
--
-- * Flags: simple no-argument options. When a flag is encountered on the
-- command line, its value is returned.
--
-- * Options: options with an argument. An option can define a /reader/,
-- which converts its argument from String to the desired value, or throws a
-- parse error if the argument does not validate correctly.
--
-- * Arguments: positional arguments, validated in the same way as option
-- arguments.
--
-- * Commands. A command defines a completely independent sub-parser. When a
-- command is encountered, the whole command line is passed to the
-- corresponding parser.
--
Parser,
liftOpt,
showOption,
-- * Program descriptions
--
-- A 'ParserInfo' describes a command line program, used to generate a help
-- screen. Two help modes are supported: brief and full. In brief mode, only
-- an option and argument summary is displayed, while in full mode each
-- available option and command, including hidden ones, is described.
--
-- A basic 'ParserInfo' with default values for fields can be created using
-- the 'info' function.
--
-- A 'ParserPrefs' contains general preferences for all command-line
-- options, and can be built with the 'prefs' function.
ParserInfo(..),
ParserPrefs(..),
-- * Running parsers
runParserInfo,
runParserFully,
runParserStep,
runParser,
evalParser,
-- * Low-level utilities
mapParser,
treeMapParser,
optionNames
) where
import Control.Applicative
import Control.Monad (guard, mzero, msum, when)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.State (StateT(..), get, put, runStateT)
import Data.List (isPrefixOf)
import Data.Maybe (maybeToList, isJust, isNothing)
import Prelude
import Options.Applicative.Internal
import Options.Applicative.Types
showOption :: OptName -> String
showOption (OptLong n) = "--" ++ n
showOption (OptShort n) = '-' : [n]
optionNames :: OptReader a -> [OptName]
optionNames (OptReader names _ _) = names
optionNames (FlagReader names _) = names
optionNames _ = []
isOptionPrefix :: OptName -> OptName -> Bool
isOptionPrefix (OptShort x) (OptShort y) = x == y
isOptionPrefix (OptLong x) (OptLong y) = x `isPrefixOf` y
isOptionPrefix _ _ = False
-- | Create a parser composed of a single option.
liftOpt :: Option a -> Parser a
liftOpt = OptP
optMatches :: MonadP m => Bool -> OptReader a -> OptWord -> Maybe (StateT Args m a)
optMatches disambiguate opt (OptWord arg1 val) = case opt of
OptReader names rdr no_arg_err -> do
guard $ has_name arg1 names
Just $ do
args <- get
let mb_args = uncons $ maybeToList val ++ args
let missing_arg = missingArgP (no_arg_err $ showOption arg1) (crCompleter rdr)
(arg', args') <- maybe (lift missing_arg) return mb_args
put args'
lift $ runReadM (withReadM (errorFor arg1) (crReader rdr)) arg'
FlagReader names x -> do
guard $ has_name arg1 names
-- #242 Flags/switches succeed incorrectly when given an argument.
-- We'll not match a long option for a flag if there's a word attached.
-- This was revealing an implementation detail as
-- `--foo=val` was being parsed as `--foo -val`, which is gibberish.
guard $ isShortName arg1 || isNothing val
Just $ do
args <- get
let val' = ('-' :) <$> val
put $ maybeToList val' ++ args
return x
_ -> Nothing
where
errorFor name msg = "option " ++ showOption name ++ ": " ++ msg
has_name a
| disambiguate = any (isOptionPrefix a)
| otherwise = elem a
isArg :: OptReader a -> Bool
isArg (ArgReader _) = True
isArg _ = False
data OptWord = OptWord OptName (Maybe String)
parseWord :: String -> Maybe OptWord
parseWord ('-' : '-' : w) = Just $ let
(opt, arg) = case span (/= '=') w of
(_, "") -> (w, Nothing)
(w', _ : rest) -> (w', Just rest)
in OptWord (OptLong opt) arg
parseWord ('-' : w) = case w of
[] -> Nothing
(a : rest) -> Just $ let
arg = rest <$ guard (not (null rest))
in OptWord (OptShort a) arg
parseWord _ = Nothing
searchParser :: Monad m
=> (forall r . Option r -> NondetT m (Parser r))
-> Parser a -> NondetT m (Parser a)
searchParser _ (NilP _) = mzero
searchParser f (OptP opt) = f opt
searchParser f (MultP p1 p2) = foldr1 (<!>)
[ do p1' <- searchParser f p1
return (p1' <*> p2)
, do p2' <- searchParser f p2
return (p1 <*> p2') ]
searchParser f (AltP p1 p2) = msum
[ searchParser f p1
, searchParser f p2 ]
searchParser f (BindP p k) = msum
[ do p' <- searchParser f p
return $ BindP p' k
, case evalParser p of
Nothing -> mzero
Just aa -> searchParser f (k aa) ]
searchOpt :: MonadP m => ParserPrefs -> OptWord -> Parser a
-> NondetT (StateT Args m) (Parser a)
searchOpt pprefs w = searchParser $ \opt -> do
let disambiguate = prefDisambiguate pprefs
&& optVisibility opt > Internal
case optMatches disambiguate (optMain opt) w of
Just matcher -> lift $ fmap pure matcher
Nothing -> mzero
searchArg :: MonadP m => ParserPrefs -> String -> Parser a
-> NondetT (StateT Args m) (Parser a)
searchArg prefs arg =
searchParser $ \opt -> do
when (isArg (optMain opt)) cut
case optMain opt of
CmdReader _ _ f ->
case (f arg, prefBacktrack prefs) of
(Just subp, NoBacktrack) -> lift $ do
args <- get <* put []
fmap pure . lift $ enterContext arg subp *> runParserInfo subp args <* exitContext
(Just subp, Backtrack) -> fmap pure . lift . StateT $ \args ->
enterContext arg subp *> runParser (infoPolicy subp) CmdStart (infoParser subp) args <* exitContext
(Just subp, SubparserInline) -> lift $ do
lift $ enterContext arg subp
return $ infoParser subp
(Nothing, _) -> mzero
ArgReader rdr ->
fmap pure . lift . lift $ runReadM (crReader rdr) arg
_ -> mzero
stepParser :: MonadP m => ParserPrefs -> ArgPolicy -> String
-> Parser a -> NondetT (StateT Args m) (Parser a)
stepParser pprefs AllPositionals arg p =
searchArg pprefs arg p
stepParser pprefs ForwardOptions arg p = case parseWord arg of
Just w -> searchOpt pprefs w p <|> searchArg pprefs arg p
Nothing -> searchArg pprefs arg p
stepParser pprefs _ arg p = case parseWord arg of
Just w -> searchOpt pprefs w p
Nothing -> searchArg pprefs arg p
-- | Apply a 'Parser' to a command line, and return a result and leftover
-- arguments. This function returns an error if any parsing error occurs, or
-- if any options are missing and don't have a default value.
runParser :: MonadP m => ArgPolicy -> IsCmdStart -> Parser a -> Args -> m (a, Args)
runParser policy _ p ("--" : argt) | policy /= AllPositionals
= runParser AllPositionals CmdCont p argt
runParser policy isCmdStart p args = case args of
[] -> exitP isCmdStart policy p result
(arg : argt) -> do
(mp', args') <- do_step arg argt
case mp' of
Nothing -> hoistMaybe result <|> parseError arg p
Just p' -> runParser (newPolicy arg) CmdCont p' args'
where
result =
(,) <$> evalParser p <*> pure args
do_step =
runParserStep policy p
newPolicy a = case policy of
NoIntersperse -> if isJust (parseWord a) then NoIntersperse else AllPositionals
x -> x
runParserStep :: MonadP m => ArgPolicy -> Parser a -> String -> Args -> m (Maybe (Parser a), Args)
runParserStep policy p arg args = do
prefs <- getPrefs
flip runStateT args
$ disamb (not (prefDisambiguate prefs))
$ stepParser prefs policy arg p
parseError :: MonadP m => String -> Parser x -> m a
parseError arg = errorP . UnexpectedError arg . SomeParser
runParserInfo :: MonadP m => ParserInfo a -> Args -> m a
runParserInfo i = runParserFully (infoPolicy i) (infoParser i)
runParserFully :: MonadP m => ArgPolicy -> Parser a -> Args -> m a
runParserFully policy p args = do
(r, args') <- runParser policy CmdStart p args
case args' of
[] -> return r
a:_ -> parseError a (pure ())
-- | The default value of a 'Parser'. This function returns an error if any of
-- the options don't have a default value.
evalParser :: Parser a -> Maybe a
evalParser (NilP r) = r
evalParser (OptP _) = Nothing
evalParser (MultP p1 p2) = evalParser p1 <*> evalParser p2
evalParser (AltP p1 p2) = evalParser p1 <|> evalParser p2
evalParser (BindP p k) = evalParser p >>= evalParser . k
-- | Map a polymorphic function over all the options of a parser, and collect
-- the results in a list.
mapParser :: (forall x. ArgumentReachability -> Option x -> b)
-> Parser a -> [b]
mapParser f = flatten . treeMapParser f
where
flatten (Leaf x) = [x]
flatten (MultNode xs) = xs >>= flatten
flatten (AltNode _ xs) = xs >>= flatten
flatten (BindNode x) = flatten x
-- | Like 'mapParser', but collect the results in a tree structure.
treeMapParser :: (forall x. ArgumentReachability -> Option x -> b)
-> Parser a
-> OptTree b
treeMapParser g = simplify . go False g
where
has_default :: Parser a -> Bool
has_default p = isJust (evalParser p)
go :: Bool
-> (forall x. ArgumentReachability -> Option x -> b)
-> Parser a
-> OptTree b
go _ _ (NilP _) = MultNode []
go r f (OptP opt)
| optVisibility opt > Internal
= Leaf (f (ArgumentReachability r) opt)
| otherwise
= MultNode []
go r f (MultP p1 p2) =
MultNode [go r f p1, go r' f p2]
where r' = r || hasArg p1
go r f (AltP p1 p2) =
AltNode altNodeType [go r f p1, go r f p2]
where
-- The 'AltNode' indicates if one of the branches has a default.
-- This is used for rendering brackets, as well as filtering
-- out optional arguments when generating the "missing:" text.
altNodeType =
if has_default p1 || has_default p2
then MarkDefault
else NoDefault
go r f (BindP p k) =
let go' = go r f p
in case evalParser p of
Nothing -> BindNode go'
Just aa -> BindNode (MultNode [ go', go r f (k aa) ])
hasArg :: Parser a -> Bool
hasArg (NilP _) = False
hasArg (OptP p) = (isArg . optMain) p
hasArg (MultP p1 p2) = hasArg p1 || hasArg p2
hasArg (AltP p1 p2) = hasArg p1 || hasArg p2
hasArg (BindP p _) = hasArg p
simplify :: OptTree a -> OptTree a
simplify (Leaf x) = Leaf x
simplify (MultNode xs) =
case concatMap (remove_mult . simplify) xs of
[x] -> x
xs' -> MultNode xs'
where
remove_mult (MultNode ts) = ts
remove_mult t = [t]
simplify (AltNode b xs) =
AltNode b (concatMap (remove_alt . simplify) xs)
where
remove_alt (AltNode _ ts) = ts
remove_alt (MultNode []) = []
remove_alt t = [t]
simplify (BindNode x) =
BindNode $ simplify x