Mercurial > hg > MakeItSo
annotate makeitso/cli.py @ 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 |
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] [...]' |
60
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
18 description = getattr(self.template_class, 'description', None) |
58
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) |
60
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
20 |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
21 # add the variables as options |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
22 for variable in self.template_class.vars: |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
23 parser.add_option('--%s' % variable.name, dest=variable.name, |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
24 default=variable.default, |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
25 help=variable.description) |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
26 |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
27 # add CLI-specific options |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
28 # should start with '_' to indicate reserved |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
29 parser.add_option('--variables', dest='_print_variables', |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
30 action='store_true', default=False, |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
31 help="display variables in the template") |
58
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
32 return parser |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
33 |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
34 def parse(self): |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
35 parser = self.parser() |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
36 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
|
37 |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
38 # print the variables for the templates |
60
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
39 if options._print_variables: |
58
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 # find all variables |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
42 template = self.template() |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
43 variables = template.variables() |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
44 |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
45 # print them |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
46 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
|
47 print variable |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
48 return |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
49 |
60
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
50 # ensure output is given |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
51 if len(args) != 1: |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
52 parser.error("Please specify one output") |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
53 |
58
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
54 # template variables |
60
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
55 variables = dict([(key, value) |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
56 for key, value in options.__dict__.items() |
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
57 if not key.startswith('_')]) |
58
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
58 |
59 | 59 # instantiate the template |
60
da0d8c5c5157
what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents:
59
diff
changeset
|
60 template = self.template_class(output=args[0], variables=variables) |
58
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
61 |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
57
diff
changeset
|
62 return template |