options.py (5470:ad060d1f1037) options.py (5586:d27058799d3a)
1# Copyright (c) 2005 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright

--- 14 unchanged lines hidden (view full) ---

23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28
29import optparse
30import sys
1# Copyright (c) 2005 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright

--- 14 unchanged lines hidden (view full) ---

23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28
29import optparse
30import sys
31import util
32
33from optparse import *
34
35class nodefault(object): pass
36
37class splitter(object):
38 def __init__(self, split):
39 self.split = split
40 def __call__(self, option, opt_str, value, parser):
41 values = value.split(self.split)
42 dest = getattr(parser.values, option.dest)
43 if dest is None:
44 setattr(parser.values, option.dest, values)
45 else:
46 dest.extend(values)
47
31
32from optparse import *
33
34class nodefault(object): pass
35
36class splitter(object):
37 def __init__(self, split):
38 self.split = split
39 def __call__(self, option, opt_str, value, parser):
40 values = value.split(self.split)
41 dest = getattr(parser.values, option.dest)
42 if dest is None:
43 setattr(parser.values, option.dest, values)
44 else:
45 dest.extend(values)
46
48class OptionParser(object):
47class OptionParser(dict):
49 def __init__(self, *args, **kwargs):
50 kwargs.setdefault('formatter', optparse.TitledHelpFormatter())
51 self._optparse = optparse.OptionParser(*args, **kwargs)
52 self._optparse.disable_interspersed_args()
53
54 self._allopts = {}
48 def __init__(self, *args, **kwargs):
49 kwargs.setdefault('formatter', optparse.TitledHelpFormatter())
50 self._optparse = optparse.OptionParser(*args, **kwargs)
51 self._optparse.disable_interspersed_args()
52
53 self._allopts = {}
55 self._defaults = {}
56 self._options = util.attrdict()
57
58 # current option group
59 self._group = self._optparse
60
54
55 # current option group
56 self._group = self._optparse
57
58 def set_defaults(self, *args, **kwargs):
59 return self._optparse.set_defaults(*args, **kwargs)
60
61 def set_group(self, *args, **kwargs):
62 '''set the current option group'''
63 if not args and not kwargs:
64 self._group = self._optparse
65 else:
66 self._group = self._optparse.add_option_group(*args, **kwargs)
67
68 def add_option(self, *args, **kwargs):

--- 4 unchanged lines hidden (view full) ---

73
74 if kwargs.get('action', None) == 'append' and 'split' in kwargs:
75 split = kwargs.pop('split')
76 kwargs['default'] = []
77 kwargs['type'] = 'string'
78 kwargs['action'] = 'callback'
79 kwargs['callback'] = splitter(split)
80
61 def set_group(self, *args, **kwargs):
62 '''set the current option group'''
63 if not args and not kwargs:
64 self._group = self._optparse
65 else:
66 self._group = self._optparse.add_option_group(*args, **kwargs)
67
68 def add_option(self, *args, **kwargs):

--- 4 unchanged lines hidden (view full) ---

73
74 if kwargs.get('action', None) == 'append' and 'split' in kwargs:
75 split = kwargs.pop('split')
76 kwargs['default'] = []
77 kwargs['type'] = 'string'
78 kwargs['action'] = 'callback'
79 kwargs['callback'] = splitter(split)
80
81 default = kwargs.pop('default', nodefault)
82 option = self._group.add_option(*args, **kwargs)
83 dest = option.dest
84 if dest not in self._allopts:
85 self._allopts[dest] = option
86
81 option = self._group.add_option(*args, **kwargs)
82 dest = option.dest
83 if dest not in self._allopts:
84 self._allopts[dest] = option
85
87 if default != nodefault:
88 if dest not in self._options:
89 self._options[dest] = default
90
91 return option
92
93 def bool_option(self, name, default, help):
94 '''add a boolean option called --name and --no-name.
95 Display help depending on which is the default'''
96
97 tname = '--%s' % name
98 fname = '--no-%s' % name

--- 9 unchanged lines hidden (view full) ---

108 help=thelp)
109 fopt = self.add_option(fname, action="store_false", dest=dest,
110 help=fhelp)
111
112 return topt,fopt
113
114 def __getattr__(self, attr):
115 if attr.startswith('_'):
86 return option
87
88 def bool_option(self, name, default, help):
89 '''add a boolean option called --name and --no-name.
90 Display help depending on which is the default'''
91
92 tname = '--%s' % name
93 fname = '--no-%s' % name

--- 9 unchanged lines hidden (view full) ---

103 help=thelp)
104 fopt = self.add_option(fname, action="store_false", dest=dest,
105 help=fhelp)
106
107 return topt,fopt
108
109 def __getattr__(self, attr):
110 if attr.startswith('_'):
116 return super(OptionParser, self).__getattr__(attr)
111 return super(OptionParser, self).__getattribute__(attr)
117
112
118 if attr in self._options:
119 return self._options[attr]
113 if attr in self:
114 return self[attr]
120
115
121 raise AttributeError, "Option %s not found" % attr
116 return super(OptionParser, self).__getattribute__(attr)
122
123 def __setattr__(self, attr, value):
124 if attr.startswith('_'):
117
118 def __setattr__(self, attr, value):
119 if attr.startswith('_'):
125 return super(OptionParser, self).__setattr__(attr, value)
120 super(OptionParser, self).__setattr__(attr, value)
121 elif attr in self._allopts:
122 defaults = { attr : value }
123 self.set_defaults(**defaults)
124 if attr in self:
125 self[attr] = value
126 else:
127 super(OptionParser, self).__setattr__(attr, value)
126
128
127 if attr in self._options:
128 self._options[attr] = value
129
130 return super(OptionParser, self).__setattr__(attr, value)
131
132 def parse_args(self):
133 opts,args = self._optparse.parse_args()
134
135 for key,val in opts.__dict__.iteritems():
129 def parse_args(self):
130 opts,args = self._optparse.parse_args()
131
132 for key,val in opts.__dict__.iteritems():
136 if val is not None or key not in self._options:
137 self._options[key] = val
133 if val is not None or key not in self:
134 self[key] = val
138
139 return args
140
141 def usage(self, exitcode=None):
142 self._optparse.print_help()
143 if exitcode is not None:
144 sys.exit(exitcode)
145
135
136 return args
137
138 def usage(self, exitcode=None):
139 self._optparse.print_help()
140 if exitcode is not None:
141 sys.exit(exitcode)
142