#!/usr/bin/env python
import sys
import argparse
from res.fm.templating import render_template


def _build_argument_parser():
    description = ("""
Loads the data from each file ("some/path/filename.xxx") in INPUT_FILES
and exposes it as the variable "filename". It then loads the Jinja2
template TEMPLATE_FILE and dumps the rendered result to OUTPUT.

Example:
Given an input file my_input.json:

{
    my_variable: my_value
}

And a template file tmpl.jinja:

This is written in my file together with {{my_input.my_variable}}

This job will produce an output file:

This is written in my file together with my_value
""")

    parser = argparse.ArgumentParser(description=description)
    parser.add_argument(
        '--output_file',
        '-o',
        required=True,
        help='the output file',
    )
    parser.add_argument(
        '--template_file',
        '-t',
        required=True,
        help='the jinja2 template file',
    )
    parser.add_argument(
        '--input_files',
        '-i',
        nargs='+',
        help='list of json and yaml input files',
    )
    return parser

if __name__ == '__main__':
    arg_parser = _build_argument_parser()
    args = arg_parser.parse_args()

    render_template(
        args.input_files,
        args.template_file,
        args.output_file,
    )
