Navigation Menu

Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

Commit

Permalink
Add command line option to render many documents (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
f-f authored and Gabriella439 committed Sep 27, 2018
1 parent 135a5a7 commit 441a7d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions dhall-json.cabal
Expand Up @@ -67,6 +67,7 @@ Executable dhall-to-yaml
dhall-json ,
optparse-applicative ,
yaml >= 0.5.0 && < 0.11,
vector ,
text
GHC-Options: -Wall

Expand Down
18 changes: 17 additions & 1 deletion dhall-to-yaml/Main.hs
Expand Up @@ -12,6 +12,7 @@ import Options.Applicative (Parser, ParserInfo)
import qualified Control.Exception
import qualified Data.ByteString
import qualified Data.Text.IO
import qualified Data.Vector
import qualified Data.Yaml
import qualified Dhall
import qualified Dhall.JSON
Expand All @@ -23,13 +24,15 @@ import qualified System.IO
data Options = Options
{ explain :: Bool
, omitNull :: Bool
, documents :: Bool
, conversion :: Conversion
}

parseOptions :: Parser Options
parseOptions = Options.Applicative.helper <*> do
explain <- parseExplain
omitNull <- parseOmitNull
documents <- parseDocuments
conversion <- Dhall.JSON.parseConversion
return (Options {..})
where
Expand All @@ -45,6 +48,12 @@ parseOptions = Options.Applicative.helper <*> do
<> Options.Applicative.help "Omit record fields that are null"
)

parseDocuments =
Options.Applicative.switch
( Options.Applicative.long "documents"
<> Options.Applicative.help "If given a Dhall list, output a document for every element"
)

parserInfo :: ParserInfo Options
parserInfo =
Options.Applicative.info
Expand All @@ -68,7 +77,14 @@ main = do

json <- omittingNull <$> explaining (Dhall.JSON.codeToValue conversion "(stdin)" stdin)

Data.ByteString.putStr $ Data.Yaml.encode json
let yaml = case (documents, json) of
(True, Data.Yaml.Array elems)
-> Data.ByteString.intercalate "\n---\n"
$ fmap Data.Yaml.encode
$ Data.Vector.toList elems
_ -> Data.Yaml.encode json

Data.ByteString.putStr yaml

handle :: IO a -> IO a
handle = Control.Exception.handle handler
Expand Down

0 comments on commit 441a7d3

Please sign in to comment.