Mercurial > hg > MakeItSo
view makeitso/cli.py @ 59:93f2b2c7f838
add a comment
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 06 Jan 2011 16:38:40 -0800 |
parents | 112bf081148c |
children | da0d8c5c5157 |
line wrap: on
line source
""" command line parser for MakeItSo """ from optparse import OptionParser 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") # instantiate the template template = self.template_class(output=output[0], variables=variables) return template