Skip to content

Commit

Permalink
Add the possibility to use only one section from the configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenselme committed Sep 19, 2015
1 parent 08547c5 commit 3f6fa41
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Options:
-e EXTENSIONS, --extension=EXTENSIONS
extra jinja2 extensions to load
-D key=value Define template variable in the form of key=value
```
-s, --section Use only this section from the configuration
```

## Optional YAML support
If `PyYAML` is present, you can use YAML as an input data source.
Expand Down
13 changes: 13 additions & 0 deletions jinja2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,18 @@ def cli(opts, args):
ext = 'jinja2.ext.' + ext
extensions.append(ext)


data.update(parse_kv_string(opts.D or []))

# Use only a specific section if needed
if opts.section:
section = opts.section
if section in data:
data = data[section]
else:
sys.stderr.write('ERROR: unknown section. Exiting.')
sys.exit(1)

output = render(template_path, data, extensions)

if isinstance(output, binary_type):
Expand All @@ -226,6 +237,8 @@ def main():
parser.add_option('-e', '--extension', help='extra jinja2 extensions to load',
dest='extensions', action='append', default=['do'])
parser.add_option('-D', action='append', help='Define template variable in the form of key=value', metavar='key=value')
parser.add_option('-s', '--section', help='Use only this section from the configuration',
dest='section', action='store')
opts, args = parser.parse_args()

# Dedupe list
Expand Down

0 comments on commit 3f6fa41

Please sign in to comment.