annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
57
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 """
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 command line parser for MakeItSo
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 from optparse import OptionParser
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
58
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
7 class MakeItSoCLI(object):
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
8 """command line interface to a makeitso template"""
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
9
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
10 def __init__(self, template_class):
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
11 self.template_class = template_class
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
12
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
13 def parser(self):
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
14 """
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
15 return a command line parser for the template
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
16 """
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
17 usage = '%prog [options] output [var1=value1] [var2=value2] [...]'
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
18 description = getattr(template, 'description', None)
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
19 parser = OptionParser(usage=usage, description=description)
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
20 parser.add_options('--variables', dest='variables',
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
21 action='store_true', default=False,
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
22 help="display variables in the template")
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
23 return parser
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
24
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
25 def parse(self):
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
26 parser = self.parser()
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
27 options, args = parser.parse_args()
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
28
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
29 # print the variables for the templates
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
30 if options.variables:
57
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31
58
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
32 # makes no sense without a template
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
33 if not args:
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
34 parser.print_usage()
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
35 parser.exit()
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
36
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
37 # find all variables
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
38 template = self.template()
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
39 variables = template.variables()
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
40
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
41 # print them
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
42 for variable in sorted(variables):
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
43 print variable
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
44 return
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
45
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
46 # template variables
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
47 variables = {}
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
48 output = []
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
49 for arg in args:
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
50 if '=' in arg:
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
51 key, value = arg.split('=')
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
52 variables[key] = value
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
53 else:
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
54 output.append(arg)
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
55 if len(output) != 1:
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
56 parser.error("Please specify one output")
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
57
59
93f2b2c7f838 add a comment
Jeff Hammel <jhammel@mozilla.com>
parents: 58
diff changeset
58 # instantiate the template
58
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
59 template = self.template_class(output=output[0], variables=variables)
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
60
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
61 return template