1 -----------------------------------------------------------------------------
5 -- This is an abstract command-line parser used by both StaticFlags and
8 -- (c) The University of Glasgow 2005
10 -----------------------------------------------------------------------------
12 module CmdLineParser (
13 processArgs, OptKind(..),
14 CmdLineP(..), getCmdLineState, putCmdLineState,
15 Flag(..), Deprecated(..),
19 #include "HsVersions.h"
30 flagName :: String, -- flag, without the leading -
31 flagOptKind :: (OptKind m), -- what to do if we see it
32 flagDeprecated :: Deprecated -- is the flag deprecated?
35 data Deprecated = Supported | Deprecated String
37 data OptKind m -- Suppose the flag is -f
38 = NoArg (m ()) -- -f all by itself
39 | HasArg (String -> m ()) -- -farg or -f arg
40 | SepArg (String -> m ()) -- -f arg
41 | Prefix (String -> m ()) -- -farg
42 | OptPrefix (String -> m ()) -- -f or -farg (i.e. the arg is optional)
43 | OptIntSuffix (Maybe Int -> m ()) -- -f or -f=n; pass n to fn
44 | IntSuffix (Int -> m ()) -- -f or -f=n; pass n to fn
45 | PassFlag (String -> m ()) -- -f; pass "-f" fn
46 | AnySuffix (String -> m ()) -- -f or -farg; pass entire "-farg" to fn
47 | PrefixPred (String -> Bool) (String -> m ())
48 | AnySuffixPred (String -> Bool) (String -> m ())
50 processArgs :: Monad m
51 => [Flag m] -- cmdline parser spec
52 -> [Located String] -- args
54 [Located String], -- spare args
55 [Located String], -- errors
56 [Located String] -- warnings
58 processArgs spec args = process spec args [] [] []
60 process _spec [] spare errs warns =
61 return (reverse spare, reverse errs, reverse warns)
63 process spec (locArg@(L loc dash_arg@('-' : arg)) : args) spare errs warns =
64 case findArg spec arg of
65 Just (rest, action, deprecated) ->
66 let warns' = case deprecated of
68 L loc ("Warning: " ++ dash_arg ++ " is deprecated: " ++ warning) : warns
70 in case processOneArg action rest arg args of
71 Left err -> process spec args spare (L loc err : errs) warns'
72 Right (action,rest) -> do action
73 process spec rest spare errs warns'
74 Nothing -> process spec args (locArg : spare) errs warns
76 process spec (arg : args) spare errs warns =
77 process spec args (arg : spare) errs warns
80 processOneArg :: OptKind m -> String -> String -> [Located String]
81 -> Either String (m (), [Located String])
82 processOneArg action rest arg args
83 = let dash_arg = '-' : arg
84 rest_no_eq = dropEq rest
86 NoArg a -> ASSERT(null rest) Right (a, args)
88 HasArg f | notNull rest_no_eq -> Right (f rest_no_eq, args)
89 | otherwise -> case args of
90 [] -> missingArgErr dash_arg
91 (L _ arg1:args1) -> Right (f arg1, args1)
93 SepArg f -> case args of
94 [] -> unknownFlagErr dash_arg
95 (L _ arg1:args1) -> Right (f arg1, args1)
97 Prefix f | notNull rest_no_eq -> Right (f rest_no_eq, args)
98 | otherwise -> unknownFlagErr dash_arg
100 PrefixPred _ f | notNull rest_no_eq -> Right (f rest_no_eq, args)
101 | otherwise -> unknownFlagErr dash_arg
103 PassFlag f | notNull rest -> unknownFlagErr dash_arg
104 | otherwise -> Right (f dash_arg, args)
106 OptIntSuffix f | null rest -> Right (f Nothing, args)
107 | Just n <- parseInt rest_no_eq -> Right (f (Just n), args)
108 | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
110 IntSuffix f | Just n <- parseInt rest_no_eq -> Right (f n, args)
111 | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
113 OptPrefix f -> Right (f rest_no_eq, args)
114 AnySuffix f -> Right (f dash_arg, args)
115 AnySuffixPred _ f -> Right (f dash_arg, args)
118 findArg :: [Flag m] -> String -> Maybe (String, OptKind m, Deprecated)
120 = case [ (removeSpaces rest, optKind, flagDeprecated flag)
122 let optKind = flagOptKind flag,
123 Just rest <- [stripPrefix (flagName flag) arg],
124 arg_ok optKind rest arg ]
129 arg_ok :: OptKind t -> [Char] -> String -> Bool
130 arg_ok (NoArg _) rest _ = null rest
131 arg_ok (HasArg _) _ _ = True
132 arg_ok (SepArg _) rest _ = null rest
133 arg_ok (Prefix _) rest _ = notNull rest
134 arg_ok (PrefixPred p _) rest _ = notNull rest && p (dropEq rest)
135 arg_ok (OptIntSuffix _) _ _ = True
136 arg_ok (IntSuffix _) _ _ = True
137 arg_ok (OptPrefix _) _ _ = True
138 arg_ok (PassFlag _) rest _ = null rest
139 arg_ok (AnySuffix _) _ _ = True
140 arg_ok (AnySuffixPred p _) _ arg = p arg
142 parseInt :: String -> Maybe Int
143 -- Looks for "433" or "=342", with no trailing gubbins
145 -- gibberish => Nothing
146 parseInt s = case reads s of
150 dropEq :: String -> String
151 -- Discards a leading equals sign
155 unknownFlagErr :: String -> Either String a
156 unknownFlagErr f = Left ("unrecognised flag: " ++ f)
158 missingArgErr :: String -> Either String a
159 missingArgErr f = Left ("missing argument for flag: " ++ f)
161 -- -----------------------------------------------------------------------------
162 -- A state monad for use in the command-line parser
164 newtype CmdLineP s a = CmdLineP { runCmdLine :: s -> (a, s) }
166 instance Monad (CmdLineP s) where
167 return a = CmdLineP $ \s -> (a, s)
168 m >>= k = CmdLineP $ \s -> let
169 (a, s') = runCmdLine m s
170 in runCmdLine (k a) s'
172 getCmdLineState :: CmdLineP s s
173 getCmdLineState = CmdLineP $ \s -> (s,s)
174 putCmdLineState :: s -> CmdLineP s ()
175 putCmdLineState s = CmdLineP $ \_ -> ((),s)
177 -- ---------------------------------------------------------------------
180 errorsToGhcException :: [Located String] -> GhcException
181 errorsToGhcException errs =
182 let errors = vcat [ ppr l <> text ": " <> text e | L l e <- errs ]
183 in UsageError (showSDoc errors)