Mercurial > hg > MakeItSo
diff makeitso/cli.py @ 58:112bf081148c
make a full CLI class for a single API template
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 06 Jan 2011 15:54:55 -0800 |
parents | 074a32920f7c |
children | 93f2b2c7f838 |
line wrap: on
line diff
--- 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