|
1 | 1 | module Options (splitArgs) where |
2 | 2 |
|
3 | 3 | import Imports |
| 4 | +import Data.Coerce (coerce) |
| 5 | +import qualified GHC.List as List |
4 | 6 |
|
5 | 7 | import System.Console.GetOpt |
6 | 8 |
|
7 | 9 | splitArgs :: [String] -> ([String], [String]) |
8 | 10 | splitArgs args = case break (== "--") $ reverse args of |
9 | 11 | (xs, "--" : ys) -> (reverse ys, reverse xs) |
10 | | - _ -> case filter isHspecArgs $ tails args of |
11 | | - x : _ -> (dropEnd (length x) args, x) |
12 | | - [] -> (args, []) |
13 | | - where |
14 | | - isHspecArgs :: [String] -> Bool |
15 | | - isHspecArgs xs = case getOpt Permute options xs of |
16 | | - (result, [], []) -> all (== Valid) result |
17 | | - _ -> False |
| 12 | + _ -> partitionOptions $ classify args |
| 13 | + |
| 14 | +newtype GhcOption = GhcOption [String] |
| 15 | +newtype HspecOption = HspecOption [String] |
| 16 | + |
| 17 | +type Option = Either GhcOption HspecOption |
| 18 | + |
| 19 | +ghcOption :: [String] -> Option |
| 20 | +ghcOption = Left . GhcOption |
| 21 | + |
| 22 | +hspecOption :: [String] -> Option |
| 23 | +hspecOption = Right . HspecOption |
| 24 | + |
| 25 | +partitionOptions :: [Option] -> ([String], [String]) |
| 26 | +partitionOptions = bimap (List.concat . coerce) (List.concat . coerce) . partitionEithers |
| 27 | + |
| 28 | +classify :: [String] -> [Option] |
| 29 | +classify = takeHspec >>> \ case |
| 30 | + ([], []) -> [] |
| 31 | + ([], ghc : args) -> ghcOption [ghc] : classify args |
| 32 | + (hspec, args) -> hspecOption hspec : classify args |
| 33 | + |
| 34 | +takeHspec :: [String] -> ([String], [String]) |
| 35 | +takeHspec = \ case |
| 36 | + a : args | isHspecArgs [a] -> ([a], args) |
| 37 | + a : b : args | isHspecArgs [a, b] -> ([a, b], args) |
| 38 | + args -> ([], args) |
18 | 39 |
|
19 | | - dropEnd :: Int -> [a] -> [a] |
20 | | - dropEnd n = reverse . drop n . reverse |
| 40 | +isHspecArgs :: [String] -> Bool |
| 41 | +isHspecArgs xs = case getOpt Permute options xs of |
| 42 | + (result, [], []) -> all (== Valid) result |
| 43 | + _ -> False |
21 | 44 |
|
22 | 45 | data Valid = Valid | Invalid |
23 | 46 | deriving (Eq, Show) |
@@ -52,7 +75,7 @@ options = concat [ |
52 | 75 | , reqArg "" "seed" "N" |
53 | 76 | , reqArg "" "skip" "PATTERN" |
54 | 77 | , reqArg "a" "qc-max-success" "N" |
55 | | - , reqArg "f" "format" "NAME" |
| 78 | + , reqArg "" "format" "NAME" |
56 | 79 | , reqArg "m" "match" "PATTERN" |
57 | 80 | , [Option "p" ["print-slow-items"] (OptArg (maybe Valid intArg) "N") ""] |
58 | 81 | ] |
|
0 commit comments