From bf4a164a6f832bbb8435c4021a6b019f8e1c29f7 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Fri, 8 Mar 2019 08:40:31 -0800
Subject: [PATCH] Allow -o/--output to be used with --print-*.

`--print-default-data-file`
`--print-highlighting-style`
`--print-default-template`

Note that `-o` must occur BEFORE the `--print*` command on the command line
(this is documented).

Closes #5357.
---
 MANUAL.txt                                | 39 +++++++++++++----------
 src/Text/Pandoc/App/CommandLineOptions.hs | 21 ++++++++----
 2 files changed, 38 insertions(+), 22 deletions(-)

diff --git a/MANUAL.txt b/MANUAL.txt
index df5dccd5e..08d3363e9 100644
--- a/MANUAL.txt
+++ b/MANUAL.txt
@@ -695,12 +695,17 @@ General writer options {.options}
 
 :   Print the system default template for an output *FORMAT*. (See `-t`
     for a list of possible *FORMAT*s.)  Templates in the user data
-    directory are ignored.
+    directory are ignored.  This option may be used with
+    `-o`/`--output` to redirect output to a file, but
+    `-o`/`--output` must come before `--print-default-template`
+    on the command line.
 
 `--print-default-data-file=`*FILE*
 
 :   Print a system default data file.  Files in the user data directory
-    are ignored.
+    are ignored.  This option may be used with `-o`/`--output` to
+    redirect output to a file, but `-o`/`--output` must come before
+    `--print-default-data-file` on the command line.
 
 `--eol=crlf`|`lf`|`native`
 
@@ -784,7 +789,10 @@ General writer options {.options}
 
 :   Prints a JSON version of a highlighting style, which can
     be modified, saved with a `.theme` extension, and used
-    with `--highlight-style`.
+    with `--highlight-style`.  This option may be used with
+    `-o`/`--output` to redirect output to a file, but
+    `-o`/`--output` must come before `--print-highlight-style`
+    on the command line.
 
 `--syntax-definition=`*FILE*
 
@@ -1016,11 +1024,11 @@ Options affecting specific writers {.options}
 
         To produce a custom `reference.docx`, first get a copy of
         the default `reference.docx`: `pandoc
-        --print-default-data-file reference.docx >
-        custom-reference.docx`.  Then open `custom-reference.docx`
-        in Word, modify the styles as you wish, and save the file.
-        For best results, do not make changes to this file other
-        than modifying the styles used by pandoc:
+        -o custom-reference.docx --print-default-data-file reference.docx`.
+        Then open `custom-reference.docx` in Word, modify the
+        styles as you wish, and save the file.  For best
+        results, do not make changes to this file other than
+        modifying the styles used by pandoc:
 
         Paragraph styles:
 
@@ -1078,10 +1086,9 @@ Options affecting specific writers {.options}
 
         To produce a custom `reference.odt`, first get a copy of
         the default `reference.odt`: `pandoc
-        --print-default-data-file reference.odt >
-        custom-reference.odt`.  Then open `custom-reference.odt` in
-        LibreOffice, modify the styles as you wish, and save the
-        file.
+        -o custom-reference.odt --print-default-data-file reference.odt`.
+        Then open `custom-reference.odt` in LibreOffice, modify
+        the styles as you wish, and save the file.
 
     PowerPoint
 
@@ -1102,10 +1109,10 @@ Options affecting specific writers {.options}
         `Home` menu to check.)
 
         You can also modify the default `reference.pptx`: first run
-        `pandoc --print-default-data-file reference.pptx >
-        custom-reference.pptx`, and then modify
-        `custom-reference.pptx` in MS PowerPoint (pandoc will use the
-        first four layout slides, as mentioned above).
+        `pandoc -o custom-reference.pptx --print-default-data-file
+        reference.pptx`, and then modify `custom-reference.pptx`
+        in MS PowerPoint (pandoc will use the first four layout
+        slides, as mentioned above).
 
 `--epub-cover-image=`*FILE*
 
diff --git a/src/Text/Pandoc/App/CommandLineOptions.hs b/src/Text/Pandoc/App/CommandLineOptions.hs
index 4253620f7..14f665aa9 100644
--- a/src/Text/Pandoc/App/CommandLineOptions.hs
+++ b/src/Text/Pandoc/App/CommandLineOptions.hs
@@ -786,7 +786,10 @@ options =
 
     , Option "D" ["print-default-template"]
                  (ReqArg
-                  (\arg _ -> do
+                  (\arg opt -> do
+                     let write = case optOutputFile opt of
+                                        Just f  -> UTF8.writeFile f
+                                        Nothing -> UTF8.hPutStr stdout
                      templ <- runIO $ do
                                 setUserDataDir Nothing
                                 getDefaultTemplate arg
@@ -794,7 +797,7 @@ options =
                           Right "" -> -- e.g. for docx, odt, json:
                             E.throwIO $ PandocCouldNotFindDataFileError
                                ("templates/default." ++ arg)
-                          Right t -> UTF8.hPutStr stdout t
+                          Right t -> write t
                           Left e  -> E.throwIO e
                      exitSuccess)
                   "FORMAT")
@@ -802,18 +805,24 @@ options =
 
     , Option "" ["print-default-data-file"]
                  (ReqArg
-                  (\arg _ -> do
+                  (\arg opt -> do
+                     let write = case optOutputFile opt of
+                                        Just f  -> BS.writeFile f
+                                        Nothing -> BS.hPutStr stdout
                      runIOorExplode $
-                       readDefaultDataFile arg >>= liftIO . BS.hPutStr stdout
+                       readDefaultDataFile arg >>= liftIO . write
                      exitSuccess)
                   "FILE")
                   "" -- "Print default data file"
 
     , Option "" ["print-highlight-style"]
                  (ReqArg
-                  (\arg _ -> do
+                  (\arg opt -> do
+                     let write = case optOutputFile opt of
+                                        Just f  -> B.writeFile f
+                                        Nothing -> B.putStr
                      sty <- fromMaybe pygments <$> lookupHighlightStyle arg
-                     B.putStr $ encodePretty'
+                     write $ encodePretty'
                        defConfig{confIndent = Spaces 4
                                 ,confCompare = keyOrder
                                   (map T.pack