Skip to content

Instantly share code, notes, and snippets.

Last active Dec 3, 2015
What would you like to do?
Command line Jinja2 renderer
#!/usr/bin/env python
# -*- coding:utf8 -*-
import sys, optparse
import jinja2
def main(args, params):
encoding = params.encoding
tpl = jinja2.Template(
param = dict()
ex_args = list()
for item in args:
item = unicode(item, encoding)
if '=' in item:
k,v = item.split('=',1)
param[k] = v
param['args'] = ex_args
if __name__ == '__main__':
usage = """\
usage: %prog [-e encoding] [var1=value1 var2=value2 ...] [arg0 arg1 ...]
render jinja2 template string using given parameter.
$ echo "{{hello}} {{world}} {{args[0]}} !" | jj hello="Hello" world="World" "Foo"
Hello World Foo !
parser = optparse.OptionParser(usage=usage)
parser.add_option("-e", "--encoding",action="store", default="utf-8",help="character encoding (utf-8 default)")
options, args = parser.parse_args()
main(args, options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment