1
+ {-# LANGUAGE ApplicativeDo #-}
1
2
{-# LANGUAGE DataKinds #-}
2
3
{-# LANGUAGE DeriveAnyClass #-}
3
4
{-# LANGUAGE DeriveGeneric #-}
8
9
module Main where
9
10
10
11
import Control.Exception (SomeException )
11
- import Data.ByteString (ByteString )
12
- import Options.Generic (Generic , ParseRecord , type (<?> ) (.. ))
12
+ import Data.Text (Text )
13
+ import Data.Text.Encoding (encodeUtf8 )
14
+ import GHC.Generics (Generic )
15
+ import Options.Applicative
13
16
import System.Exit (ExitCode (.. ))
14
17
15
18
import qualified Control.Exception
@@ -22,32 +25,57 @@ import qualified Dhall.Import
22
25
import qualified Dhall.Parser
23
26
import qualified Dhall.TypeCheck
24
27
import qualified GHC.IO.Encoding
25
- import qualified Options.Generic
26
28
import qualified Paths_dhall_bash
27
29
import qualified System.Exit
28
30
import qualified System.IO
29
31
30
32
data Options = Options
31
33
{ explain :: Bool
32
- <?> " Explain error messages in detail"
33
- , declare :: Maybe ByteString
34
- <?> " Declare the given variable as a statement instead of an expression"
34
+ , declare :: Maybe Text
35
35
, version :: Bool
36
- <?> " Display version"
37
- } deriving (Generic , ParseRecord )
36
+ } deriving (Generic )
37
+
38
+ -- | Parser for all the command arguments and options
39
+ parseOptions :: Parser Options
40
+ parseOptions = do
41
+ explain <- switch
42
+ ( long " explain"
43
+ <> help " Explain error messages in detail"
44
+ )
45
+
46
+ declare <- (optional . strOption)
47
+ ( long " declare"
48
+ <> metavar " NAME"
49
+ <> help " Declare the given variable as a statement instead of an expression"
50
+ )
51
+
52
+ version <- switch
53
+ ( long " version"
54
+ <> help " Display version"
55
+ )
56
+
57
+ pure Options {.. }
58
+
59
+ parserInfo :: ParserInfo Options
60
+ parserInfo =
61
+ info
62
+ (helper <*> parseOptions)
63
+ ( fullDesc
64
+ <> progDesc " Compile Dhall to Bash"
65
+ )
38
66
39
67
main :: IO ()
40
68
main = do
41
69
GHC.IO.Encoding. setLocaleEncoding GHC.IO.Encoding. utf8
42
- Options {.. } <- Options.Generic. getRecord " Compile Dhall to Bash "
70
+ Options {.. } <- execParser parserInfo
43
71
44
- if unHelpful version
72
+ if version
45
73
then do
46
74
putStrLn (Data.Version. showVersion Paths_dhall_bash. version)
47
75
System.Exit. exitSuccess
48
76
else return ()
49
77
50
- (if unHelpful explain then Dhall. detailed else id ) (handle (do
78
+ (if explain then Dhall. detailed else id ) (handle (do
51
79
inText <- Data.Text.IO. getContents
52
80
53
81
expr <- case Dhall.Parser. exprFromText " (input)" inText of
@@ -59,13 +87,13 @@ main = do
59
87
Left err -> Control.Exception. throwIO err
60
88
Right _ -> return ()
61
89
62
- bytes <- case unHelpful declare of
90
+ bytes <- case declare of
63
91
Nothing -> do
64
92
case Dhall.Bash. dhallToExpression expr' of
65
93
Left err -> Control.Exception. throwIO err
66
94
Right bytes -> return bytes
67
95
Just var -> do
68
- case Dhall.Bash. dhallToStatement expr' var of
96
+ case Dhall.Bash. dhallToStatement expr' (encodeUtf8 var) of
69
97
Left err -> Control.Exception. throwIO err
70
98
Right bytes -> return bytes
71
99
Data.ByteString. putStr bytes ))
0 commit comments