2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
8 -----------------------------------------------------------------------------
10 -- Command-line parser
12 -- This is an abstract command-line parser used by both StaticFlags and
15 -- (c) The University of Glasgow 2005
17 -----------------------------------------------------------------------------
19 module CmdLineParser (
20 processArgs, OptKind(..),
21 CmdLineP(..), getCmdLineState, putCmdLineState
24 #include "HsVersions.h"
26 import Util ( maybePrefixMatch, notNull, removeSpaces )
29 data OptKind m -- Suppose the flag is -f
30 = NoArg (m ()) -- -f all by itself
31 | HasArg (String -> m ()) -- -farg or -f arg
32 | SepArg (String -> m ()) -- -f arg
33 | Prefix (String -> m ()) -- -farg
34 | OptPrefix (String -> m ()) -- -f or -farg (i.e. the arg is optional)
35 | OptIntSuffix (Maybe Int -> m ()) -- -f or -f=n; pass n to fn
36 | IntSuffix (Int -> m ()) -- -f or -f=n; pass n to fn
37 | PassFlag (String -> m ()) -- -f; pass "-f" fn
38 | AnySuffix (String -> m ()) -- -f or -farg; pass entire "-farg" to fn
39 | PrefixPred (String -> Bool) (String -> m ())
40 | AnySuffixPred (String -> Bool) (String -> m ())
42 processArgs :: Monad m
43 => [(String, OptKind m)] -- cmdline parser spec
46 [String], -- spare args
49 processArgs spec args = process spec args [] []
51 process _spec [] spare errs =
52 return (reverse spare, reverse errs)
54 process spec (dash_arg@('-':arg):args) spare errs =
55 case findArg spec arg of
57 case processOneArg action rest arg args of
58 Left err -> process spec args spare (err:errs)
59 Right (action,rest) -> action >> process spec rest spare errs
60 Nothing -> process spec args (dash_arg:spare) errs
62 process spec (arg:args) spare errs =
63 process spec args (arg:spare) errs
66 processOneArg :: OptKind m -> String -> String -> [String]
67 -> Either String (m (), [String])
68 processOneArg action rest arg args
69 = let dash_arg = '-' : arg
70 rest_no_eq = dropEq rest
72 NoArg a -> ASSERT(null rest) Right (a, args)
74 HasArg f | notNull rest_no_eq -> Right (f rest_no_eq, args)
75 | otherwise -> case args of
76 [] -> missingArgErr dash_arg
77 (arg1:args1) -> Right (f arg1, args1)
79 SepArg f -> case args of
80 [] -> unknownFlagErr dash_arg
81 (arg1:args1) -> Right (f arg1, args1)
83 Prefix f | notNull rest_no_eq -> Right (f rest_no_eq, args)
84 | otherwise -> unknownFlagErr dash_arg
86 PrefixPred p f | notNull rest_no_eq -> Right (f rest_no_eq, args)
87 | otherwise -> unknownFlagErr dash_arg
89 PassFlag f | notNull rest -> unknownFlagErr dash_arg
90 | otherwise -> Right (f dash_arg, args)
92 OptIntSuffix f | null rest -> Right (f Nothing, args)
93 | Just n <- parseInt rest_no_eq -> Right (f (Just n), args)
94 | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
96 IntSuffix f | Just n <- parseInt rest_no_eq -> Right (f n, args)
97 | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
99 OptPrefix f -> Right (f rest_no_eq, args)
100 AnySuffix f -> Right (f dash_arg, args)
101 AnySuffixPred p f -> Right (f dash_arg, args)
104 findArg :: [(String,OptKind a)] -> String -> Maybe (String,OptKind a)
106 = case [ (removeSpaces rest, k)
108 Just rest <- [maybePrefixMatch pat arg],
114 arg_ok (NoArg _) rest arg = null rest
115 arg_ok (HasArg _) rest arg = True
116 arg_ok (SepArg _) rest arg = null rest
117 arg_ok (Prefix _) rest arg = notNull rest
118 arg_ok (PrefixPred p _) rest arg = notNull rest && p (dropEq rest)
119 arg_ok (OptIntSuffix _) rest arg = True
120 arg_ok (IntSuffix _) rest arg = True
121 arg_ok (OptPrefix _) rest arg = True
122 arg_ok (PassFlag _) rest arg = null rest
123 arg_ok (AnySuffix _) rest arg = True
124 arg_ok (AnySuffixPred p _) rest arg = p arg
126 parseInt :: String -> Maybe Int
127 -- Looks for "433" or "=342", with no trailing gubbins
129 -- gibberish => Nothing
130 parseInt s = case reads s of
134 dropEq :: String -> String
135 -- Discards a leading equals sign
139 unknownFlagErr :: String -> Either String a
140 unknownFlagErr f = Left ("unrecognised flag: " ++ f)
142 missingArgErr :: String -> Either String a
143 missingArgErr f = Left ("missing argument for flag: " ++ f)
145 -- -----------------------------------------------------------------------------
146 -- A state monad for use in the command-line parser
148 newtype CmdLineP s a = CmdLineP { runCmdLine :: s -> (a, s) }
150 instance Monad (CmdLineP s) where
151 return a = CmdLineP $ \s -> (a, s)
152 m >>= k = CmdLineP $ \s -> let
153 (a, s') = runCmdLine m s
154 in runCmdLine (k a) s'
156 getCmdLineState = CmdLineP $ \s -> (s,s)
157 putCmdLineState s = CmdLineP $ \_ -> ((),s)