summaryrefslogtreecommitdiff
path: root/dhall-to-yaml/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'dhall-to-yaml/Main.hs')
-rw-r--r--dhall-to-yaml/Main.hs62
1 files changed, 46 insertions, 16 deletions
diff --git a/dhall-to-yaml/Main.hs b/dhall-to-yaml/Main.hs
index 15369fb..56b7485 100644
--- a/dhall-to-yaml/Main.hs
+++ b/dhall-to-yaml/Main.hs
@@ -1,14 +1,13 @@
-{-# LANGUAGE DataKinds #-}
-{-# LANGUAGE DeriveAnyClass #-}
-{-# LANGUAGE DeriveGeneric #-}
+{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
-{-# LANGUAGE TypeOperators #-}
module Main where
import Control.Exception (SomeException)
-import Options.Generic (Generic, ParseRecord, type (<?>))
+import Data.Monoid ((<>))
+import Dhall.JSON (Conversion)
+import Options.Applicative (Parser, ParserInfo)
import qualified Control.Exception
import qualified Data.ByteString
@@ -17,28 +16,59 @@ import qualified Data.Yaml
import qualified Dhall
import qualified Dhall.JSON
import qualified GHC.IO.Encoding
-import qualified Options.Generic
+import qualified Options.Applicative
import qualified System.Exit
import qualified System.IO
data Options = Options
- { explain :: Bool <?> "Explain error messages in detail"
- , omitNull :: Bool <?> "Omit record fields that are null"
- } deriving (Generic, ParseRecord)
+ { explain :: Bool
+ , omitNull :: Bool
+ , conversion :: Conversion
+ }
+
+parseOptions :: Parser Options
+parseOptions = Options.Applicative.helper <*> do
+ explain <- parseExplain
+ omitNull <- parseOmitNull
+ conversion <- Dhall.JSON.parseConversion
+ return (Options {..})
+ where
+ parseExplain =
+ Options.Applicative.switch
+ ( Options.Applicative.long "explain"
+ <> Options.Applicative.help "Explain error messages in detail"
+ )
+
+ parseOmitNull =
+ Options.Applicative.switch
+ ( Options.Applicative.long "omitNull"
+ <> Options.Applicative.help "Omit record fields that are null"
+ )
+
+parserInfo :: ParserInfo Options
+parserInfo =
+ Options.Applicative.info
+ parseOptions
+ ( Options.Applicative.fullDesc
+ <> Options.Applicative.progDesc "Compile Dhall to YAML"
+ )
main :: IO ()
-main = handle $ do
+main = do
GHC.IO.Encoding.setLocaleEncoding GHC.IO.Encoding.utf8
- Options{..} <- Options.Generic.getRecord "Compile Dhall to JSON"
- let explaining = if Options.Generic.unHelpful explain then Dhall.detailed else id
- omittingNull = if Options.Generic.unHelpful omitNull then Dhall.JSON.omitNull else id
+ Options {..} <- Options.Applicative.execParser parserInfo
+
+ handle $ do
+ let explaining = if explain then Dhall.detailed else id
+
+ let omittingNull = if omitNull then Dhall.JSON.omitNull else id
- stdin <- Data.Text.IO.getContents
+ stdin <- Data.Text.IO.getContents
- json <- omittingNull <$> explaining (Dhall.JSON.codeToValue "(stdin)" stdin)
+ json <- omittingNull <$> explaining (Dhall.JSON.codeToValue conversion "(stdin)" stdin)
- Data.ByteString.putStr $ Data.Yaml.encode json
+ Data.ByteString.putStr $ Data.Yaml.encode json
handle :: IO a -> IO a
handle = Control.Exception.handle handler