@@ -249,7 +249,19 @@ def _load_json5():
249249}
250250
251251
252- def render (template_path , data , extensions , strict = False ):
252+ def convert_value (value , target_type ):
253+ if target_type == bool :
254+ if value .lower () == "true" :
255+ return True
256+ if value .lower () == "false" :
257+ return False
258+ raise ValueError ("not a valid boolean: %s" % value )
259+ if target_type == int :
260+ return int (value )
261+ return value
262+
263+
264+ def render (template_path , data , extensions , strict = False , env_opts = {}):
253265 from jinja2 import (
254266 __version__ as jinja_version ,
255267 Environment ,
@@ -275,6 +287,13 @@ def render(template_path, data, extensions, strict=False):
275287 )
276288 if strict :
277289 env .undefined = StrictUndefined
290+ for key in env_opts :
291+ if hasattr (env , key ):
292+ target_type = type (getattr (env , key ))
293+ new_value = convert_value (env_opts [key ], target_type )
294+ setattr (env , key , new_value )
295+ else :
296+ raise MalformedEnv ("unsupported environment option: %s" % key )
278297
279298 # Add environ global
280299 env .globals ["environ" ] = lambda key : force_text (os .environ .get (key ))
@@ -359,6 +378,8 @@ def cli(opts, args):
359378
360379 data .update (parse_kv_string (opts .D or []))
361380
381+ env_opts = parse_kv_string (opts .env or [])
382+
362383 if opts .outfile is None :
363384 out = sys .stdout
364385 else :
@@ -369,7 +390,7 @@ def cli(opts, args):
369390
370391 out = codecs .getwriter ("utf8" )(out )
371392
372- out .write (render (template_path , data , extensions , opts .strict ))
393+ out .write (render (template_path , data , extensions , opts .strict , env_opts ))
373394 out .flush ()
374395 return 0
375396
@@ -468,6 +489,13 @@ def main():
468489 metavar = "FILE" ,
469490 action = "store" ,
470491 )
492+ parser .add_option (
493+ "--env-opt" ,
494+ help = "Define jinja2 Environment option in the form of option=value. Available options: %s" ,
495+ dest = "env" ,
496+ action = "append" ,
497+ metavar = "option=value" ,
498+ )
471499 opts , args = parser .parse_args ()
472500
473501 # Dedupe list
0 commit comments