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
17 #include "HsVersions.h"
19 import Util ( maybePrefixMatch, notNull, removeSpaces )
21 import Panic ( assertPanic )
24 data OptKind m -- Suppose the flag is -f
25 = NoArg (m ()) -- -f all by itself
26 | HasArg (String -> m ()) -- -farg or -f arg
27 | SepArg (String -> m ()) -- -f arg
28 | Prefix (String -> m ()) -- -farg
29 | OptPrefix (String -> m ()) -- -f or -farg (i.e. the arg is optional)
30 | OptIntSuffix (Maybe Int -> m ()) -- -f or -f=n; pass n to fn
31 | PassFlag (String -> m ()) -- -f; pass "-f" fn
32 | AnySuffix (String -> m ()) -- -f or -farg; pass entire "-farg" to fn
33 | PrefixPred (String -> Bool) (String -> m ())
34 | AnySuffixPred (String -> Bool) (String -> m ())
36 processArgs :: Monad m
37 => [(String, OptKind m)] -- cmdline parser spec
40 [String], -- spare args
43 processArgs spec args = process spec args [] []
45 process _spec [] spare errs =
46 return (reverse spare, reverse errs)
48 process spec (dash_arg@('-':arg):args) spare errs =
49 case findArg spec arg of
51 case processOneArg action rest arg args of
52 Left err -> process spec args spare (err:errs)
53 Right (action,rest) -> action >> process spec rest spare errs
54 Nothing -> process spec args (dash_arg:spare) errs
56 process spec (arg:args) spare errs =
57 process spec args (arg:spare) errs
60 processOneArg :: OptKind m -> String -> String -> [String]
61 -> Either String (m (), [String])
62 processOneArg action rest arg args
63 = let dash_arg = '-' : arg
65 NoArg a -> ASSERT(null rest) Right (a, args)
67 HasArg f | notNull rest -> Right (f rest, args)
68 | otherwise -> case args of
69 [] -> missingArgErr dash_arg
70 (arg1:args1) -> Right (f arg1, args1)
72 SepArg f -> case args of
73 [] -> unknownFlagErr dash_arg
74 (arg1:args1) -> Right (f arg1, args1)
76 Prefix f | notNull rest -> Right (f rest, args)
77 | otherwise -> unknownFlagErr dash_arg
79 PrefixPred p f | notNull rest -> Right (f rest, args)
80 | otherwise -> unknownFlagErr dash_arg
82 PassFlag f | notNull rest -> unknownFlagErr dash_arg
83 | otherwise -> Right (f dash_arg, args)
85 OptIntSuffix f | Just n <- parseInt rest -> Right (f n, args)
86 | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
88 OptPrefix f -> Right (f rest, args)
89 AnySuffix f -> Right (f dash_arg, args)
90 AnySuffixPred p f -> Right (f dash_arg, args)
94 findArg :: [(String,OptKind a)] -> String -> Maybe (String,OptKind a)
96 = case [ (removeSpaces rest, k)
98 Just rest <- [maybePrefixMatch pat arg],
104 arg_ok (NoArg _) rest arg = null rest
105 arg_ok (HasArg _) rest arg = True
106 arg_ok (SepArg _) rest arg = null rest
107 arg_ok (Prefix _) rest arg = notNull rest
108 arg_ok (PrefixPred p _) rest arg = notNull rest && p rest
109 arg_ok (OptIntSuffix _) rest arg = True
110 arg_ok (OptPrefix _) rest arg = True
111 arg_ok (PassFlag _) rest arg = null rest
112 arg_ok (AnySuffix _) rest arg = True
113 arg_ok (AnySuffixPred p _) rest arg = p arg
115 parseInt :: String -> Maybe (Maybe Int)
116 -- Looks for "433" or "=342", with no trailing gubbins
117 -- empty string => Just Nothing
118 -- n or =n => Just (Just n)
119 -- gibberish => Nothing
121 | null s = Just Nothing
122 | otherwise = case reads (dropEq s) of
123 ((n,""):_) -> Just (Just n)
126 dropEq :: String -> String
127 -- Discards a leading equals sign
131 unknownFlagErr :: String -> Either String a
132 unknownFlagErr f = Left ("unrecognised flag: " ++ f)
134 missingArgErr :: String -> Either String a
135 missingArgErr f = Left ("missing argument for flag: " ++ f)
137 -- -----------------------------------------------------------------------------
138 -- A state monad for use in the command-line parser
140 newtype CmdLineP s a = CmdLineP { runCmdLine :: s -> (a, s) }
142 instance Monad (CmdLineP s) where
143 return a = CmdLineP $ \s -> (a, s)
144 m >>= k = CmdLineP $ \s -> let
145 (a, s') = runCmdLine m s
146 in runCmdLine (k a) s'
148 getCmdLineState = CmdLineP $ \s -> (s,s)
149 putCmdLineState s = CmdLineP $ \_ -> ((),s)