summaryrefslogtreecommitdiff
path: root/lib/CommandLine.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/CommandLine.hs')
-rw-r--r--lib/CommandLine.hs38
1 files changed, 27 insertions, 11 deletions
diff --git a/lib/CommandLine.hs b/lib/CommandLine.hs
index ea5d6b8..2676260 100644
--- a/lib/CommandLine.hs
+++ b/lib/CommandLine.hs
@@ -14,6 +14,8 @@ module CommandLine
14 , fancy 14 , fancy
15 , runArgs 15 , runArgs
16 , arg 16 , arg
17 , args
18 , flag
17 , param 19 , param
18 , params 20 , params
19 , label 21 , label
@@ -50,11 +52,11 @@ type MergeData = [(Int,Ordering)]
50data Expr a where 52data Expr a where
51 -- Prim 53 -- Prim
52 -- 54 --
53 -- Takes a function from the option arguments and unamed arguments repsectively to 55 -- Takes a function from the option arguments and unnamed arguments
54 -- a value of type a, usually IO (), and gives you an expression tree. As one 56 -- respectively to a value of type a, usually IO (), and gives you an
55 -- traverses down the tree only the 'interesting' option arguments are passed 57 -- expression tree. As one traverses down the tree only the 'interesting'
56 -- to this function, but all of the unnamed arguments are passed regardless of 58 -- option arguments are passed to this function, but all of the unnamed
57 -- where we are in the tree. 59 -- arguments are passed regardless of where we are in the tree.
58 -- 60 --
59 Prim :: ([[String]] -> [String] -> a) -> Interval (SuperOrd Int) -> Expr a 61 Prim :: ([[String]] -> [String] -> a) -> Interval (SuperOrd Int) -> Expr a
60 -- Star 62 -- Star
@@ -233,17 +235,31 @@ mergeBy comp xs ys = trace (unlines ["xs="++show xs,"ys="++show ys,"mergeData="+
233-} 235-}
234 236
235 237
238-- | The nth unnamed argument.
236param :: Int -> Args String 239param :: Int -> Args String
237param n = Args (Prim (\_ us -> us!!n) (singleton $ exactly (n+1))) [] 240param n = Args (Prim (\_ us -> us!!n) (singleton $ exactly (n+1))) []
238 241
242-- | All unnamed arguments as a list.
243params :: Args [String]
244params = Args (Prim (\_ args -> args) (exactly 0 ... PositiveInfinity)) []
245
246
247-- | The value of named by the given option name.
239arg :: String -> Args String 248arg :: String -> Args String
240arg optname = Args (Prim (\opts _ -> concat $ take 1 $ concat $ take 1 opts) 249arg optname = Args (Prim (\opts _ -> concat $ take 1 $ concat $ take 1 opts)
241 (singleton $ exactly 0)) 250 (singleton $ exactly 0))
242 [optname] 251 [optname]
243 252
244params :: Args [String] 253-- | All values named by the given option name.
245params = Args (Prim (\_ args -> args) (exactly 0 ... PositiveInfinity)) [] 254args :: String -> Args [String]
255args optname = fromMaybe [] <$> optional
256 (Args (Prim (\opts _ -> concat $ take 1 opts)
257 (singleton $ exactly 0)) -- no unnamed arguments
258 [optname]) -- one named argument
246 259
260-- | True if the given named option is present.
261flag :: String -> Args Bool
262flag optname = maybe False (const True) <$> optional (arg optname)
247 263
248label :: String -> Args a -> Args a 264label :: String -> Args a -> Args a
249label _ = id 265label _ = id
@@ -286,7 +302,7 @@ vanilla flags = ArgsStyle { parseInvocation = parse flags }
286-- 302--
287-- * default polyvariadic - Implicit polyvariadic option if no other option is specified. 303-- * default polyvariadic - Implicit polyvariadic option if no other option is specified.
288-- 304--
289fancy :: [([Char], Int)] -> [[Char]] -> [Char] -> ArgsStyle 305fancy :: [(String, Int)] -> [String] -> String -> ArgsStyle
290fancy sargspsec polyVariadicArgs defaultPoly = ArgsStyle 306fancy sargspsec polyVariadicArgs defaultPoly = ArgsStyle
291 { parseInvocation = parse sargspsec polyVariadicArgs defaultPoly } 307 { parseInvocation = parse sargspsec polyVariadicArgs defaultPoly }
292 where 308 where
@@ -470,8 +486,8 @@ runArgs (os,us) c
470 where 486 where
471 os' = sortOn fst os 487 os' = sortOn fst os
472 dups = mapMaybe notSingle $ groupBy ((==) `on` fst) (os' :: [(String,[String])]) 488 dups = mapMaybe notSingle $ groupBy ((==) `on` fst) (os' :: [(String,[String])])
473 where notSingle [x] = Nothing 489 where notSingle [x] = Nothing
474 notSingle ((k,v):xs) = Just (k,v : map snd xs) 490 notSingle ((k,v):xs) = Just (k,v : map snd xs)
475 getbit = Map.fromList $ zip (accepts c) [0..] 491 getbit = Map.fromList $ zip (accepts c) [0..]
476 goods :: [(Int,[String])] 492 goods :: [(Int,[String])]
477 (bads,goods) = partitionEithers $ map f os' 493 (bads,goods) = partitionEithers $ map f os'