Mark Roddy avatar Mark Roddy committed 299fb2e

Added the ability to specify template name/value pairs via the command line.

Comments (0)

Files changed (1)

 def main(args):
-    parser = OptionParser("usage: %prog [-o file] [-i file] template")
+    parser = OptionParser("usage: %prog [-o file] [-i file] [--value name1=value --value name2=value ...] template")
     parser.add_option('--search-path-template', dest='template_search_path', default='.', action = 'store',
                       help = 'Comma seperated list of directories to search for templates (defaults to ".")')
     parser.add_option('-o', '--out-file', dest='output_file', default = '', action = 'store',
                       help = 'Path to write rendered template to.  Defaults to stdout')
     parser.add_option('-i', '--in-file', dest='input_file', default = '', action = 'store',
-                      help = 'Path to file containing JSON encoded values to populate the template.  Defaults to stdin.')
+                      help = 'Path to file containing JSON encoded values to populate the template.  Specify \'-\' to use stdin')
+    parser.add_option('--value', dest='template_values', action = 'append',
+                      help = 'name=value pair to be populated in the template.  This option may be specified more than once')
     options, args = parser.parse_args(args)
     if 2 != len(args):
         outfile = sys.stdout
     if options.input_file:
-        infile = open(options.input_file, 'r')
+        if '-' == options.input_file:
+            infile = sys.stdin
+        else:
+            infile = open(options.input_file, 'r')
-        infile = sys.stdin
+        infile = None
         template = tr.make_template(template_spec)
         sys.stderr.write(str(e) + '\n')
         return 1
-    tr.add_values(infile)
+    if infile:
+        tr.add_values(infile)
+    for item in options.template_values:
+        name, value = item.split('=',1)
+        tr.add_value(name,value)
     tr.render(template, outfile)
     return 0
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.