summaryrefslogtreecommitdiff
path: root/dhall-to-json/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'dhall-to-json/Main.hs')
-rw-r--r--dhall-to-json/Main.hs38
1 files changed, 34 insertions, 4 deletions
diff --git a/dhall-to-json/Main.hs b/dhall-to-json/Main.hs
index f1fda41..d58130f 100644
--- a/dhall-to-json/Main.hs
+++ b/dhall-to-json/Main.hs
@@ -4,8 +4,11 @@
module Main where
+import Control.Applicative ((<|>))
import Control.Exception (SomeException)
+import Control.Monad (when)
import Data.Monoid ((<>))
+import Data.Version (showVersion)
import Dhall.JSON (Conversion)
import Options.Applicative (Parser, ParserInfo)
@@ -19,6 +22,7 @@ import qualified Dhall
import qualified Dhall.JSON
import qualified GHC.IO.Encoding
import qualified Options.Applicative
+import qualified Paths_dhall_json as Meta
import qualified System.Exit
import qualified System.IO
@@ -26,6 +30,7 @@ data Options = Options
{ explain :: Bool
, pretty :: Bool
, omitNull :: Bool
+ , version :: Bool
, conversion :: Conversion
}
@@ -34,6 +39,7 @@ parseOptions = Options.Applicative.helper <*> do
explain <- parseExplain
pretty <- parsePretty
omitNull <- parseOmitNull
+ version <- parseVersion
conversion <- Dhall.JSON.parseConversion
return (Options {..})
where
@@ -44,10 +50,24 @@ parseOptions = Options.Applicative.helper <*> do
)
parsePretty =
- Options.Applicative.switch
- ( Options.Applicative.long "pretty"
- <> Options.Applicative.help "Pretty print generated JSON"
- )
+ prettyFlag <|> compactFlag <|> defaultBehavior
+ where
+ prettyFlag =
+ Options.Applicative.flag'
+ True
+ ( Options.Applicative.long "pretty"
+ <> Options.Applicative.help "Pretty print generated JSON"
+ )
+
+ compactFlag =
+ Options.Applicative.flag'
+ False
+ ( Options.Applicative.long "compact"
+ <> Options.Applicative.help "Render JSON on one line"
+ )
+
+ defaultBehavior =
+ pure False
parseOmitNull =
Options.Applicative.switch
@@ -55,6 +75,12 @@ parseOptions = Options.Applicative.helper <*> do
<> Options.Applicative.help "Omit record fields that are null"
)
+ parseVersion =
+ Options.Applicative.switch
+ ( Options.Applicative.long "version"
+ <> Options.Applicative.help "Display version"
+ )
+
parserInfo :: ParserInfo Options
parserInfo =
Options.Applicative.info
@@ -69,6 +95,10 @@ main = do
Options {..} <- Options.Applicative.execParser parserInfo
+ when version $ do
+ putStrLn (showVersion Meta.version)
+ System.Exit.exitSuccess
+
handle $ do
let config = Data.Aeson.Encode.Pretty.Config
{ Data.Aeson.Encode.Pretty.confIndent = Data.Aeson.Encode.Pretty.Spaces 2