1 # Copyright (c) 2005 The Regents of The University of Michigan
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.
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.
27 # Authors: Nathan Binkert
32 from optparse
import *
34 class nodefault(object): pass
36 class splitter(object):
37 def __init__(self
, split
):
39 def __call__(self
, option
, opt_str
, value
, parser
):
40 values
= value
.split(self
.split
)
41 dest
= getattr(parser
.values
, option
.dest
)
43 setattr(parser
.values
, option
.dest
, values
)
47 class OptionParser(dict):
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()
55 # current option group
56 self
._group
= self
._optparse
58 def set_defaults(self
, *args
, **kwargs
):
59 return self
._optparse
.set_defaults(*args
, **kwargs
)
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
66 self
._group
= self
._optparse
.add_option_group(*args
, **kwargs
)
68 def add_option(self
, *args
, **kwargs
):
69 '''add an option to the current option group, or global none set'''
71 # if action=split, but allows the option arguments
72 # themselves to be lists separated by the split variable'''
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
)
81 option
= self
._group
.add_option(*args
, **kwargs
)
83 if dest
not in self
._allopts
:
84 self
._allopts
[dest
] = option
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'''
93 fname
= '--no-%s' % name
94 dest
= name
.replace('-', '_')
96 thelp
= optparse
.SUPPRESS_HELP
100 fhelp
= optparse
.SUPPRESS_HELP
102 topt
= self
.add_option(tname
, action
="store_true", default
=default
,
104 fopt
= self
.add_option(fname
, action
="store_false", dest
=dest
,
109 def __getattr__(self
, attr
):
110 if attr
.startswith('_'):
111 return super(OptionParser
, self
).__getattribute
__(attr
)
116 return super(OptionParser
, self
).__getattribute
__(attr
)
118 def __setattr__(self
, attr
, value
):
119 if attr
.startswith('_'):
120 super(OptionParser
, self
).__setattr
__(attr
, value
)
121 elif attr
in self
._allopts
:
122 defaults
= { attr
: value
}
123 self
.set_defaults(**defaults
)
127 super(OptionParser
, self
).__setattr
__(attr
, value
)
129 def parse_args(self
):
130 opts
,args
= self
._optparse
.parse_args()
132 for key
,val
in opts
.__dict
__.iteritems():
133 if val
is not None or key
not in self
:
138 def usage(self
, exitcode
=None):
139 self
._optparse
.print_help()
140 if exitcode
is not None: