aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjörn Dahlgren <bjodah@gmail.com>2018-11-26 21:52:21 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2020-02-26 11:14:15 -0500
commit64f2f8f123acf4a65d7ddc81acdf115fa8f6ad0b (patch)
treee6a271f55e70c03538a67babf48f1adbaa11674f
parenta60b6ea5f2b4aaeae493f2072600963665195179 (diff)
downloadexternal_python_mako-64f2f8f123acf4a65d7ddc81acdf115fa8f6ad0b.tar.gz
external_python_mako-64f2f8f123acf4a65d7ddc81acdf115fa8f6ad0b.tar.bz2
external_python_mako-64f2f8f123acf4a65d7ddc81acdf115fa8f6ad0b.zip
Add --output-file option to mako-render
Added --output-file argument to the Mako command line runner, which allows a specific output file to be selected. Pull request courtesy Björn Dahlgren. Fixes: #283 Change-Id: Iae7e2d42d5ae4bc6f09663c115bda9e3797ce68c Pull-request: https://bitbucket.org/zzzeek/mako/pull-requests/27
-rw-r--r--doc/build/unreleased/283.rst7
-rwxr-xr-xmako/cmd.py16
2 files changed, 22 insertions, 1 deletions
diff --git a/doc/build/unreleased/283.rst b/doc/build/unreleased/283.rst
new file mode 100644
index 0000000..f8cb585
--- /dev/null
+++ b/doc/build/unreleased/283.rst
@@ -0,0 +1,7 @@
+.. change::
+ :tags: feature, commands
+ :tickets: 283
+
+ Added --output-file argument to the Mako command line runner, which allows
+ a specific output file to be selected. Pull request courtesy Björn
+ Dahlgren.
diff --git a/mako/cmd.py b/mako/cmd.py
index 95de54a..c0f2c75 100755
--- a/mako/cmd.py
+++ b/mako/cmd.py
@@ -4,6 +4,7 @@
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
from argparse import ArgumentParser
+import io
from os.path import dirname
from os.path import isfile
import sys
@@ -46,11 +47,17 @@ def cmdline(argv=None):
parser.add_argument(
"--output-encoding", default=None, help="force output encoding"
)
+ parser.add_argument(
+ "--output-file",
+ default=None,
+ help="Write to file upon successful render instead of stdout",
+ )
parser.add_argument("input", nargs="?", default="-")
options = parser.parse_args(argv)
output_encoding = options.output_encoding
+ output_file = options.output_file
if options.input == "-":
lookup_dirs = options.template_dir or ["."]
@@ -80,9 +87,16 @@ def cmdline(argv=None):
kw = dict([varsplit(var) for var in options.var])
try:
- sys.stdout.write(template.render(**kw))
+ rendered = template.render(**kw)
except:
_exit()
+ else:
+ if output_file:
+ io.open(output_file, "wt", encoding=output_encoding).write(
+ rendered
+ )
+ else:
+ sys.stdout.write(rendered)
if __name__ == "__main__":