comparison makeitso/cli.py @ 98:37f92ae8f999

separate out variable getting to its own function
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 11 Jan 2011 11:35:38 -0800
parents e74baa8e6df4
children 9c70669854f4
comparison
equal deleted inserted replaced
97:3eb34cad5858 98:37f92ae8f999
13 13
14 def parser(self): 14 def parser(self):
15 """ 15 """
16 return a command line parser for the template 16 return a command line parser for the template
17 """ 17 """
18 usage = '%prog [options] output' 18 usage = getattr(self, 'usage', '%prog [options] output')
19 description = getattr(self.template_class, 'description', None) 19 description = getattr(self.template_class, 'description', None)
20 parser = OptionParser(usage=usage, description=description) 20 parser = OptionParser(usage=usage, description=description)
21 21
22 # add the variables as options 22 # add the variables as options
23 for variable in self.template_class.vars: 23 for variable in self.template_class.vars:
27 parser.add_option('--%s' % variable.name, dest=variable.name, 27 parser.add_option('--%s' % variable.name, dest=variable.name,
28 default=variable.default, 28 default=variable.default,
29 help=description) 29 help=description)
30 return parser 30 return parser
31 31
32 def get_variables(self, options):
33 """
34 return variables from (parsed) options
35 """
36 return dict([(key, value)
37 for key, value in options.__dict__.items()
38 if not key.startswith('_')])
39
32 def parse(self, args=None, parser=None, options=None): 40 def parse(self, args=None, parser=None, options=None):
33 41
34 # parse the command line 42 # parse the command line
35 if not parser or not options: 43 if not parser or not options:
36 parser = self.parser() 44 parser = self.parser()
39 # ensure output is given 47 # ensure output is given
40 if len(args) != 1: 48 if len(args) != 1:
41 parser.error("Please specify a single output destination") 49 parser.error("Please specify a single output destination")
42 50
43 # template variables 51 # template variables
44 variables = dict([(key, value) 52 variables = self.get_variables(options)
45 for key, value in options.__dict__.items() 53
46 if not key.startswith('_')]) 54 # return the variables and the output
47
48 #
49 return variables, args[0] 55 return variables, args[0]
50 56
51 def __call__(self, *args): 57 def __call__(self, *args):
52 variables, output = self.parse(list(args)) 58 variables, output = self.parse(list(args))
53 template = self.template_class(variables=variables) 59 template = self.template_class(variables=variables)