# HG changeset patch # User Jeff Hammel # Date 1294358095 28800 # Node ID 112bf081148cf0fda6ab89780321e779b4677c30 # Parent 074a32920f7c272307b91a45ebc93c9adadeeee2 make a full CLI class for a single API template diff -r 074a32920f7c -r 112bf081148c makeitso/cli.py --- a/makeitso/cli.py Thu Jan 06 14:27:09 2011 -0800 +++ b/makeitso/cli.py Thu Jan 06 15:54:55 2011 -0800 @@ -4,12 +4,57 @@ from optparse import OptionParser -def parser(template): - """ - return a command line parser for the template - """ - usage = '%prog [options]' - description = getattr(template, 'description', None) - parser = OptionParser(usage=usage, description=description) - return parser +class MakeItSoCLI(object): + """command line interface to a makeitso template""" + + def __init__(self, template_class): + self.template_class = template_class + + def parser(self): + """ + return a command line parser for the template + """ + usage = '%prog [options] output [var1=value1] [var2=value2] [...]' + description = getattr(template, '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") + return parser + + def parse(self): + parser = self.parser() + 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() + + # find all variables + template = self.template() + variables = template.variables() + + # print them + for variable in sorted(variables): + print variable + return + + # 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") + + template = self.template_class(output=output[0], variables=variables) + + return template diff -r 074a32920f7c -r 112bf081148c makeitso/template.py --- a/makeitso/template.py Thu Jan 06 14:27:09 2011 -0800 +++ b/makeitso/template.py Thu Jan 06 15:54:55 2011 -0800 @@ -14,7 +14,7 @@ class Variable(object): """variable object for MakeItSo templates""" - def __init__(self, name, default=Undefined, description=None, + def __init__(self, name, description=None, default=Undefined, cast=None): self.name = name self.default = default @@ -63,7 +63,8 @@ # inspect the templates for more variables look = False - def __init__(self, output=None, interactive=True, usedefaults=False, **variables): + def __init__(self, output=None, interactive=True, usedefaults=True, + variables=None): """ - output : output file or directory - interactive : whether tointeractively get variables @@ -73,10 +74,10 @@ assert self.templates self.output = output self.interactive = interactive - self.location = os.path.dirnme(os.path.abspath(__file__)) - self.defaults = variables.copy + self.location = os.path.dirname(os.path.abspath(__file__)) + self.defaults = variables.copy() - # make a dictionary of the variables + # make a dictionary of the variables for lookup convenience self.vardict = {} for i in self.vars: self.vardict[i.name] = i