diff options
-rw-r--r-- | doc/build/unreleased/283.rst | 7 | ||||
-rwxr-xr-x | mako/cmd.py | 16 |
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__": |