mattrobenolt / jinja2-cli

CLI for Jinja2
BSD 2-Clause "Simplified" License
506 stars 181 forks source link

Feature request: parse -D parameters with dot notation #82

Open nicklofaso opened 5 years ago

nicklofaso commented 5 years ago

Request

Currently it seems like -D parameter names are treated as a single value. It would be useful if the parameter used dot notation to parse it and allow it to be accessed the same way that the yaml parameters are.

See below for an example.

Example

test.tmpl

{
  foo: {{house.bathroom.size}}
  bar: {{house.kitchen.size}}
}

params.yaml

house:
  bathroom:
    size: 20

CLI Command

$ jinja2 test.tmpl params.yaml -D house.kitchen.value=55

Error Received

Traceback (most recent call last):
  File "/Users/nlofaso/.virtualenvs/platform-cdk/bin/jinja2", line 10, in <module>
    sys.exit(main())
  File "/Users/nlofaso/.virtualenvs/platform-cdk/lib/python3.7/site-packages/jinja2cli/cli.py", line 424, in main
    sys.exit(cli(opts, args))
  File "/Users/nlofaso/.virtualenvs/platform-cdk/lib/python3.7/site-packages/jinja2cli/cli.py", line 314, in cli
    out.write(render(template_path, data, extensions, opts.strict))
  File "/Users/nlofaso/.virtualenvs/platform-cdk/lib/python3.7/site-packages/jinja2cli/cli.py", line 229, in render
    return env.get_template(os.path.basename(template_path)).render(data)
  File "/Users/nlofaso/.virtualenvs/platform-cdk/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/Users/nlofaso/.virtualenvs/platform-cdk/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Users/nlofaso/.virtualenvs/platform-cdk/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/nlofaso/.virtualenvs/platform-cdk/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/Users/nlofaso/tmp/test.tmpl", line 3, in top-level template code
    bar: {{house.kitchen.size}}
  File "/Users/nlofaso/.virtualenvs/platform-cdk/lib/python3.7/site-packages/jinja2/environment.py", line 430, in getattr
    return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'kitchen'