changeset 60:da0d8c5c5157

what i really meant to do with variables
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 06 Jan 2011 17:12:36 -0800
parents 93f2b2c7f838
children 57f9b0349192
files makeitso/cli.py
diffstat 1 files changed, 22 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/makeitso/cli.py	Thu Jan 06 16:38:40 2011 -0800
+++ b/makeitso/cli.py	Thu Jan 06 17:12:36 2011 -0800
@@ -15,11 +15,20 @@
     return a command line parser for the template
     """
     usage = '%prog [options] output [var1=value1] [var2=value2] [...]'
-    description = getattr(template, 'description', None)
+    description = getattr(self.template_class, 'description', None)
     parser = OptionParser(usage=usage, description=description)
-    parser.add_options('--variables', dest='variables', 
-                       action='store_true', default=False,
-                       help="display variables in the template")
+
+    # add the variables as options
+    for variable in self.template_class.vars:
+      parser.add_option('--%s' % variable.name, dest=variable.name,
+                        default=variable.default,
+                        help=variable.description)
+
+    # add CLI-specific options
+    # should start with '_' to indicate reserved
+    parser.add_option('--variables', dest='_print_variables', 
+                      action='store_true', default=False,
+                      help="display variables in the template")
     return parser
 
   def parse(self):
@@ -27,12 +36,7 @@
     options, args = parser.parse_args()
 
     # print the variables for the templates
-    if options.variables:
-
-        # makes no sense without a template
-        if not args:
-            parser.print_usage()
-            parser.exit()
+    if options._print_variables:
 
         # find all variables
         template = self.template()
@@ -43,19 +47,16 @@
             print variable
         return
 
+    # ensure output is given
+    if len(args) != 1:
+      parser.error("Please specify one output")
+
     # template variables
-    variables = {}
-    output = []
-    for arg in args:
-        if '=' in arg:
-            key, value = arg.split('=')
-            variables[key] = value
-        else:
-            output.append(arg)
-    if len(output) != 1:
-      parser.error("Please specify one output")
+    variables = dict([(key, value)
+                      for key, value in options.__dict__.items()
+                      if not key.startswith('_')])
     
     # instantiate the template
-    template = self.template_class(output=output[0], variables=variables)
+    template = self.template_class(output=args[0], variables=variables)
 
     return template