comparison configuration/config.py @ 17:a78ab14ae376

separate thing to add options to its own function
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 26 Mar 2012 12:04:49 -0700
parents 0df4bfdc2c96
children d8871956536e
comparison
equal deleted inserted replaced
16:b7e7f450ad1a 17:a78ab14ae376
70 self.check(config) 70 self.check(config)
71 71
72 self.config.update(config) 72 self.config.update(config)
73 # TODO: option to extend; augment lists/dicts 73 # TODO: option to extend; augment lists/dicts
74 74
75 def parser(self, configuration_provider_option=None, **parser_args): 75 def optparse_options(self, parser):
76 """ 76 """add optparse options to a OptionParser instance"""
77 return OptionParser for this Configuration instance
78 - configuration_provider_options : option for configuration files [TODO]
79 (also TODO: a special value that equates to the first file extension value
80 for the configuration_providers)
81 - parser_args : arguments to the OptionParser constructor
82 """
83 if 'description' not in parser_args:
84 parser_args['description'] = getattr(self, '__doc__', '')
85 if 'formatter' not in parser_args:
86 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter):
87 """description formatter for console script entry point"""
88 def format_description(self, description):
89 if description:
90 return description.strip() + '\n'
91 else:
92 return ''
93 parser_args['formatter'] = PlainDescriptionFormatter()
94 parser = optparse.OptionParser(**parser_args)
95 for key, value in self.items(): 77 for key, value in self.items():
96 # TODO: move adding options to a separate function 78 # TODO: move adding options to a separate function
97 79
98 # CLI arguments 80 # CLI arguments
99 args = value.get('flags', ['--%s' % key]) 81 args = value.get('flags', ['--%s' % key])
111 93
112 help += ' [DEFAULT: %s]' % value['default'] 94 help += ' [DEFAULT: %s]' % value['default']
113 kw['help'] = help 95 kw['help'] = help
114 kw['action'] = 'store' # TODO: types 96 kw['action'] = 'store' # TODO: types
115 parser.add_option(*args, **kw) 97 parser.add_option(*args, **kw)
98
99 def parser(self, configuration_provider_option=None, **parser_args):
100 """
101 return OptionParser for this Configuration instance
102 - configuration_provider_options : option for configuration files [TODO]
103 (also TODO: a special value that equates to the first file extension value
104 for the configuration_providers)
105 - parser_args : arguments to the OptionParser constructor
106 """
107 if 'description' not in parser_args:
108 parser_args['description'] = getattr(self, '__doc__', '')
109 if 'formatter' not in parser_args:
110 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter):
111 """description formatter for console script entry point"""
112 def format_description(self, description):
113 if description:
114 return description.strip() + '\n'
115 else:
116 return ''
117 parser_args['formatter'] = PlainDescriptionFormatter()
118 parser = optparse.OptionParser(**parser_args)
119 self.optparse_options(parser)
116 return parser 120 return parser
117 121
118 def main(args=sys.argv[:]): 122 def main(args=sys.argv[:]):
119 123
120 # parse command line options 124 # parse command line options