33
44module Main where
55
6- import Control.Applicative ((<|>) )
6+ import Control.Applicative ((<|>) , optional )
77import Control.Exception (SomeException )
88import Control.Monad (when )
99import Data.Aeson (Value )
@@ -17,12 +17,13 @@ import qualified Data.Aeson
1717import qualified Data.Aeson.Encode.Pretty
1818import qualified Data.ByteString.Char8
1919import qualified Data.ByteString.Lazy
20- import qualified Data.Text.IO
20+ import qualified Data.Text as Text
21+ import qualified Data.Text.IO as Text.IO
2122import qualified Dhall
2223import qualified Dhall.JSON
2324import qualified GHC.IO.Encoding
24- import qualified Options.Applicative
25- import qualified Paths_dhall_json as Meta
25+ import qualified Options.Applicative as Options
26+ import qualified Paths_dhall_json as Meta
2627import qualified System.Exit
2728import qualified System.IO
2829
@@ -33,6 +34,7 @@ data Options = Options
3334 , version :: Bool
3435 , conversion :: Conversion
3536 , approximateSpecialDoubles :: Bool
37+ , file :: Maybe FilePath
3638 }
3739
3840parseOptions :: Parser Options
@@ -44,58 +46,66 @@ parseOptions =
4446 <*> parseVersion
4547 <*> Dhall.JSON. parseConversion
4648 <*> parseApproximateSpecialDoubles
49+ <*> optional parseFile
4750 where
4851 parseExplain =
49- Options.Applicative. switch
50- ( Options.Applicative. long " explain"
51- <> Options.Applicative. help " Explain error messages in detail"
52+ Options. switch
53+ ( Options. long " explain"
54+ <> Options. help " Explain error messages in detail"
5255 )
5356
5457 parsePretty =
5558 prettyFlag <|> compactFlag <|> defaultBehavior
5659 where
5760 prettyFlag =
58- Options.Applicative. flag'
61+ Options. flag'
5962 True
60- ( Options.Applicative. long " pretty"
61- <> Options.Applicative. help " Pretty print generated JSON"
63+ ( Options. long " pretty"
64+ <> Options. help " Pretty print generated JSON"
6265 )
6366
6467 compactFlag =
65- Options.Applicative. flag'
68+ Options. flag'
6669 False
67- ( Options.Applicative. long " compact"
68- <> Options.Applicative. help " Render JSON on one line"
70+ ( Options. long " compact"
71+ <> Options. help " Render JSON on one line"
6972 )
7073
7174 defaultBehavior =
7275 pure False
7376
7477 parseVersion =
75- Options.Applicative. switch
76- ( Options.Applicative. long " version"
77- <> Options.Applicative. help " Display version"
78+ Options. switch
79+ ( Options. long " version"
80+ <> Options. help " Display version"
7881 )
7982
8083 parseApproximateSpecialDoubles =
81- Options.Applicative. switch
82- ( Options.Applicative. long " approximate-special-doubles"
83- <> Options.Applicative. help " Use approximate representation for NaN/±Infinity"
84+ Options. switch
85+ ( Options. long " approximate-special-doubles"
86+ <> Options. help " Use approximate representation for NaN/±Infinity"
87+ )
88+
89+ parseFile =
90+ Options. strOption
91+ ( Options. long " file"
92+ <> Options. help " Read expression from a file instead of standard input"
93+ <> Options. metavar " FILE"
8494 )
8595
8696parserInfo :: ParserInfo Options
8797parserInfo =
88- Options.Applicative. info
89- (Options.Applicative. helper <*> parseOptions)
90- ( Options.Applicative. fullDesc
91- <> Options.Applicative. progDesc " Compile Dhall to JSON"
98+ Options. info
99+ (Options. helper <*> parseOptions)
100+ ( Options. fullDesc
101+ <> Options. progDesc " Compile Dhall to JSON"
92102 )
93103
94104main :: IO ()
95105main = do
96106 GHC.IO.Encoding. setLocaleEncoding GHC.IO.Encoding. utf8
97107
98- Options {.. } <- Options.Applicative. execParser parserInfo
108+ Options {.. } <- Options. execParser parserInfo
99109
100110 when version $ do
101111 putStrLn (showVersion Meta. version)
@@ -119,9 +129,15 @@ main = do
119129 then ApproximateWithinJSON
120130 else ForbidWithinJSON
121131
122- stdin <- Data.Text.IO. getContents
132+ text <- case file of
133+ Nothing -> Text.IO. getContents
134+ Just path -> Text.IO. readFile path
135+
136+ let path = case file of
137+ Nothing -> " (stdin)"
138+ Just p -> Text. pack p
123139
124- json <- omission <$> explaining (Dhall.JSON. codeToValue conversion specialDoubleMode " (stdin) " stdin )
140+ json <- omission <$> explaining (Dhall.JSON. codeToValue conversion specialDoubleMode path text )
125141
126142 Data.ByteString.Char8. putStrLn $ Data.ByteString.Lazy. toStrict $ encode json
127143
0 commit comments