Added --fail-if-warnings option.

This commit is contained in:
John MacFarlane 2016-12-04 09:43:32 +01:00
parent 57cff4b8ae
commit 70aa7b0485
2 changed files with 24 additions and 5 deletions

View file

@ -344,6 +344,10 @@ General options
: Suppress warning messages. : Suppress warning messages.
`--fail-if-warnings`
: Exit with error status if there are any warnings.
`--list-input-formats` `--list-input-formats`
: List supported input formats, one per line. : List supported input formats, one per line.

View file

@ -76,7 +76,7 @@ import System.Posix.Terminal (queryTerminal)
import System.Posix.IO (stdOutput) import System.Posix.IO (stdOutput)
#endif #endif
import Control.Monad.Trans import Control.Monad.Trans
import Text.Pandoc.Class (withMediaBag, PandocIO, withWarningsToStderr) import Text.Pandoc.Class (withMediaBag, PandocIO, getWarnings)
type Transform = Pandoc -> Pandoc type Transform = Pandoc -> Pandoc
@ -197,6 +197,7 @@ data Opt = Opt
, optIgnoreArgs :: Bool -- ^ Ignore command-line arguments , optIgnoreArgs :: Bool -- ^ Ignore command-line arguments
, optVerbose :: Bool -- ^ Verbose diagnostic output , optVerbose :: Bool -- ^ Verbose diagnostic output
, optQuiet :: Bool -- ^ Suppress warnings , optQuiet :: Bool -- ^ Suppress warnings
, optFailIfWarnings :: Bool -- ^ Fail on warnings
, optReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst , optReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
, optReferenceLocation :: ReferenceLocation -- ^ location for footnotes and link references in markdown output , optReferenceLocation :: ReferenceLocation -- ^ location for footnotes and link references in markdown output
, optDpi :: Int -- ^ Dpi , optDpi :: Int -- ^ Dpi
@ -263,6 +264,7 @@ defaultOpts = Opt
, optIgnoreArgs = False , optIgnoreArgs = False
, optVerbose = False , optVerbose = False
, optQuiet = False , optQuiet = False
, optFailIfWarnings = False
, optReferenceLinks = False , optReferenceLinks = False
, optReferenceLocation = EndOfDocument , optReferenceLocation = EndOfDocument
, optDpi = 96 , optDpi = 96
@ -910,6 +912,11 @@ options =
(\opt -> return opt { optQuiet = True })) (\opt -> return opt { optQuiet = True }))
"" -- "Suppress warnings." "" -- "Suppress warnings."
, Option "" ["fail-if-warnings"]
(NoArg
(\opt -> return opt { optFailIfWarnings = True }))
"" -- "Exit with error status if there were warnings."
, Option "" ["bash-completion"] , Option "" ["bash-completion"]
(NoArg (NoArg
(\_ -> do (\_ -> do
@ -1195,6 +1202,7 @@ convertWithOpts opts args = do
, optIgnoreArgs = ignoreArgs , optIgnoreArgs = ignoreArgs
, optVerbose = verbose , optVerbose = verbose
, optQuiet = quiet , optQuiet = quiet
, optFailIfWarnings = failIfWarnings
, optReferenceLinks = referenceLinks , optReferenceLinks = referenceLinks
, optReferenceLocation = referenceLocation , optReferenceLocation = referenceLocation
, optDpi = dpi , optDpi = dpi
@ -1409,10 +1417,17 @@ convertWithOpts opts args = do
then 0 then 0
else tabStop) else tabStop)
let runIO' = runIOorExplode . let runIO' f = do
(if quiet (res, warnings) <- runIOorExplode $ do
then id x <- f
else withWarningsToStderr) ws <- getWarnings
return (x, ws)
when (not (null warnings)) $ do
unless quiet $
mapM_ warn warnings
when failIfWarnings $
err 3 "Failing because there were warnings."
return res
let sourceToDoc :: [FilePath] -> IO (Pandoc, MediaBag) let sourceToDoc :: [FilePath] -> IO (Pandoc, MediaBag)
sourceToDoc sources' = sourceToDoc sources' =