python: Fix m5's tick rounding mechanism
[gem5.git] / src / python / m5 / options.py
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
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution;
11 # neither the name of the copyright holders nor the names of its
12 # contributors may be used to endorse or promote products derived from
13 # this software without specific prior written permission.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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 from __future__ import print_function
28 from __future__ import absolute_import
29
30 import optparse
31 import sys
32
33 from optparse import *
34
35 class nodefault(object): pass
36
37 class 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
48 class 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 = {}
55
56 # current option group
57 self._group = self._optparse
58
59 def set_defaults(self, *args, **kwargs):
60 return self._optparse.set_defaults(*args, **kwargs)
61
62 def set_group(self, *args, **kwargs):
63 '''set the current option group'''
64 if not args and not kwargs:
65 self._group = self._optparse
66 else:
67 self._group = self._optparse.add_option_group(*args, **kwargs)
68
69 def add_option(self, *args, **kwargs):
70 '''add an option to the current option group, or global none set'''
71
72 # if action=split, but allows the option arguments
73 # themselves to be lists separated by the split variable'''
74
75 if kwargs.get('action', None) == 'append' and 'split' in kwargs:
76 split = kwargs.pop('split')
77 kwargs['default'] = []
78 kwargs['type'] = 'string'
79 kwargs['action'] = 'callback'
80 kwargs['callback'] = splitter(split)
81
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
87 return option
88
89 def bool_option(self, name, default, help):
90 '''add a boolean option called --name and --no-name.
91 Display help depending on which is the default'''
92
93 tname = '--%s' % name
94 fname = '--no-%s' % name
95 dest = name.replace('-', '_')
96 if default:
97 thelp = optparse.SUPPRESS_HELP
98 fhelp = help
99 else:
100 thelp = help
101 fhelp = optparse.SUPPRESS_HELP
102
103 topt = self.add_option(tname, action="store_true", default=default,
104 help=thelp)
105 fopt = self.add_option(fname, action="store_false", dest=dest,
106 help=fhelp)
107
108 return topt,fopt
109
110 def __getattr__(self, attr):
111 if attr.startswith('_'):
112 return super(OptionParser, self).__getattribute__(attr)
113
114 if attr in self:
115 return self[attr]
116
117 return super(OptionParser, self).__getattribute__(attr)
118
119 def __setattr__(self, attr, value):
120 if attr.startswith('_'):
121 super(OptionParser, self).__setattr__(attr, value)
122 elif attr in self._allopts:
123 defaults = { attr : value }
124 self.set_defaults(**defaults)
125 if attr in self:
126 self[attr] = value
127 else:
128 super(OptionParser, self).__setattr__(attr, value)
129
130 def parse_args(self):
131 opts,args = self._optparse.parse_args()
132
133 for key,val in opts.__dict__.items():
134 if val is not None or key not in self:
135 self[key] = val
136
137 return args
138
139 def usage(self, exitcode=None):
140 self._optparse.print_help()
141 if exitcode is not None:
142 sys.exit(exitcode)
143