aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Options.purs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Options.purs')
-rw-r--r--src/Options.purs72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/Options.purs b/src/Options.purs
new file mode 100644
index 0000000..b96cddc
--- /dev/null
+++ b/src/Options.purs
@@ -0,0 +1,72 @@
1module PursLoader.Options
2 ( pscMakeOptions
3 , pscMakeDefaultOutput
4 , pscMakeOutputOption
5 ) where
6
7import Data.Either (either)
8
9import Data.Foreign (Foreign(), F())
10import Data.Foreign.Class (IsForeign, read, readProp)
11import Data.Foreign.NullOrUndefined (NullOrUndefined(), runNullOrUndefined)
12
13import Data.Maybe (Maybe(..), maybe)
14
15noPreludeOpt = "no-prelude"
16
17noOptsOpt = "no-opts"
18
19noMagicDoOpt = "no-magic-do"
20
21noTcoOpt = "no-tco"
22
23verboseErrorsOpt = "verbose-errors"
24
25outputOpt = "output"
26
27pscMakeDefaultOutput = "output"
28
29newtype Options
30 = Options { noPrelude :: NullOrUndefined Boolean
31 , noOpts :: NullOrUndefined Boolean
32 , noMagicDo :: NullOrUndefined Boolean
33 , noTco :: NullOrUndefined Boolean
34 , verboseErrors :: NullOrUndefined Boolean
35 , output :: NullOrUndefined String
36 }
37
38instance isForeignOptions :: IsForeign Options where
39 read obj = (\a b c d e f ->
40 Options { noPrelude: a
41 , noOpts: b
42 , noMagicDo: c
43 , noTco: d
44 , verboseErrors: e
45 , output: f
46 }) <$> readProp noPreludeOpt obj
47 <*> readProp noOptsOpt obj
48 <*> readProp noMagicDoOpt obj
49 <*> readProp noTcoOpt obj
50 <*> readProp verboseErrorsOpt obj
51 <*> readProp outputOpt obj
52
53booleanOpt :: String -> NullOrUndefined Boolean -> [String]
54booleanOpt key opt = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined opt)
55
56stringOpt :: String -> NullOrUndefined String -> [String]
57stringOpt key opt = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined opt)
58
59pscMakeOutputOption :: Foreign -> Maybe String
60pscMakeOutputOption query = either (const Nothing)
61 (\(Options a) -> runNullOrUndefined a.output)
62 (read query)
63
64pscMakeOptions :: Foreign -> [String]
65pscMakeOptions query = either (const []) fold parsed
66 where parsed = read query :: F Options
67 fold (Options a) = booleanOpt noPreludeOpt a.noPrelude <>
68 booleanOpt noOptsOpt a.noOpts <>
69 booleanOpt noMagicDoOpt a.noMagicDo <>
70 booleanOpt noTcoOpt a.noTco <>
71 booleanOpt verboseErrorsOpt a.verboseErrors <>
72 stringOpt outputOpt a.output