options.py revision 5586
112855Sgabeblack@google.com# Copyright (c) 2005 The Regents of The University of Michigan 212855Sgabeblack@google.com# All rights reserved. 312855Sgabeblack@google.com# 412855Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without 512855Sgabeblack@google.com# modification, are permitted provided that the following conditions are 612855Sgabeblack@google.com# met: redistributions of source code must retain the above copyright 712855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer; 812855Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright 912855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the 1012855Sgabeblack@google.com# documentation and/or other materials provided with the distribution; 1112855Sgabeblack@google.com# neither the name of the copyright holders nor the names of its 1212855Sgabeblack@google.com# contributors may be used to endorse or promote products derived from 1312855Sgabeblack@google.com# this software without specific prior written permission. 1412855Sgabeblack@google.com# 1512855Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1612855Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1712855Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1812855Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1912855Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2012855Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2112855Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2212855Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2312855Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2412855Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2512855Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2612855Sgabeblack@google.com# 2712855Sgabeblack@google.com# Authors: Nathan Binkert 2812855Sgabeblack@google.com 2912855Sgabeblack@google.comimport optparse 3012855Sgabeblack@google.comimport sys 3112855Sgabeblack@google.com 3212855Sgabeblack@google.comfrom optparse import * 3312855Sgabeblack@google.com 3412855Sgabeblack@google.comclass nodefault(object): pass 3512855Sgabeblack@google.com 3612855Sgabeblack@google.comclass splitter(object): 3712855Sgabeblack@google.com def __init__(self, split): 3812855Sgabeblack@google.com self.split = split 3912855Sgabeblack@google.com def __call__(self, option, opt_str, value, parser): 4012855Sgabeblack@google.com values = value.split(self.split) 4112855Sgabeblack@google.com dest = getattr(parser.values, option.dest) 4212855Sgabeblack@google.com if dest is None: 4312855Sgabeblack@google.com setattr(parser.values, option.dest, values) 4412855Sgabeblack@google.com else: 4512855Sgabeblack@google.com dest.extend(values) 4612855Sgabeblack@google.com 4712855Sgabeblack@google.comclass OptionParser(dict): 4812855Sgabeblack@google.com def __init__(self, *args, **kwargs): 4912855Sgabeblack@google.com kwargs.setdefault('formatter', optparse.TitledHelpFormatter()) 5012855Sgabeblack@google.com self._optparse = optparse.OptionParser(*args, **kwargs) 5112855Sgabeblack@google.com self._optparse.disable_interspersed_args() 5212855Sgabeblack@google.com 5312855Sgabeblack@google.com self._allopts = {} 5412855Sgabeblack@google.com 5512855Sgabeblack@google.com # current option group 5612855Sgabeblack@google.com self._group = self._optparse 5712855Sgabeblack@google.com 5812855Sgabeblack@google.com def set_defaults(self, *args, **kwargs): 5912855Sgabeblack@google.com return self._optparse.set_defaults(*args, **kwargs) 6012855Sgabeblack@google.com 6112855Sgabeblack@google.com def set_group(self, *args, **kwargs): 6212855Sgabeblack@google.com '''set the current option group''' 6312855Sgabeblack@google.com if not args and not kwargs: 6412855Sgabeblack@google.com self._group = self._optparse 6512855Sgabeblack@google.com else: 6612855Sgabeblack@google.com self._group = self._optparse.add_option_group(*args, **kwargs) 6712855Sgabeblack@google.com 6812855Sgabeblack@google.com def add_option(self, *args, **kwargs): 6912855Sgabeblack@google.com '''add an option to the current option group, or global none set''' 7012855Sgabeblack@google.com 7112855Sgabeblack@google.com # if action=split, but allows the option arguments 7212855Sgabeblack@google.com # themselves to be lists separated by the split variable''' 7312855Sgabeblack@google.com 7412855Sgabeblack@google.com if kwargs.get('action', None) == 'append' and 'split' in kwargs: 7512855Sgabeblack@google.com split = kwargs.pop('split') 7612855Sgabeblack@google.com kwargs['default'] = [] 7712855Sgabeblack@google.com kwargs['type'] = 'string' 7812855Sgabeblack@google.com kwargs['action'] = 'callback' 7912855Sgabeblack@google.com kwargs['callback'] = splitter(split) 8012855Sgabeblack@google.com 8112855Sgabeblack@google.com option = self._group.add_option(*args, **kwargs) 8212855Sgabeblack@google.com dest = option.dest 8312855Sgabeblack@google.com if dest not in self._allopts: 8412855Sgabeblack@google.com self._allopts[dest] = option 8512855Sgabeblack@google.com 8612855Sgabeblack@google.com return option 8712855Sgabeblack@google.com 8812855Sgabeblack@google.com def bool_option(self, name, default, help): 8912855Sgabeblack@google.com '''add a boolean option called --name and --no-name. 9012855Sgabeblack@google.com Display help depending on which is the default''' 9112855Sgabeblack@google.com 9212855Sgabeblack@google.com tname = '--%s' % name 9312855Sgabeblack@google.com fname = '--no-%s' % name 9412855Sgabeblack@google.com dest = name.replace('-', '_') 9512855Sgabeblack@google.com if default: 9612855Sgabeblack@google.com thelp = optparse.SUPPRESS_HELP 9712855Sgabeblack@google.com fhelp = help 9812855Sgabeblack@google.com else: 9912855Sgabeblack@google.com thelp = help 10012855Sgabeblack@google.com fhelp = optparse.SUPPRESS_HELP 10112855Sgabeblack@google.com 10212855Sgabeblack@google.com topt = self.add_option(tname, action="store_true", default=default, 10312855Sgabeblack@google.com help=thelp) 10412855Sgabeblack@google.com fopt = self.add_option(fname, action="store_false", dest=dest, 10512855Sgabeblack@google.com help=fhelp) 10612855Sgabeblack@google.com 10712855Sgabeblack@google.com return topt,fopt 10812855Sgabeblack@google.com 10912855Sgabeblack@google.com def __getattr__(self, attr): 11012855Sgabeblack@google.com if attr.startswith('_'): 11112855Sgabeblack@google.com return super(OptionParser, self).__getattribute__(attr) 11212855Sgabeblack@google.com 11312855Sgabeblack@google.com if attr in self: 11412855Sgabeblack@google.com return self[attr] 11512855Sgabeblack@google.com 11612855Sgabeblack@google.com return super(OptionParser, self).__getattribute__(attr) 11712855Sgabeblack@google.com 11812855Sgabeblack@google.com def __setattr__(self, attr, value): 11912855Sgabeblack@google.com if attr.startswith('_'): 12012855Sgabeblack@google.com super(OptionParser, self).__setattr__(attr, value) 12112855Sgabeblack@google.com elif attr in self._allopts: 12212855Sgabeblack@google.com defaults = { attr : value } 12312855Sgabeblack@google.com self.set_defaults(**defaults) 12412855Sgabeblack@google.com if attr in self: 12512855Sgabeblack@google.com self[attr] = value 12612855Sgabeblack@google.com else: 12712855Sgabeblack@google.com super(OptionParser, self).__setattr__(attr, value) 12812855Sgabeblack@google.com 12912855Sgabeblack@google.com def parse_args(self): 13012855Sgabeblack@google.com opts,args = self._optparse.parse_args() 13112855Sgabeblack@google.com 13212855Sgabeblack@google.com for key,val in opts.__dict__.iteritems(): 13312855Sgabeblack@google.com if val is not None or key not in self: 13412855Sgabeblack@google.com self[key] = val 13512855Sgabeblack@google.com 13612855Sgabeblack@google.com return args 13712855Sgabeblack@google.com 13812855Sgabeblack@google.com def usage(self, exitcode=None): 13912855Sgabeblack@google.com self._optparse.print_help() 14012855Sgabeblack@google.com if exitcode is not None: 14112855Sgabeblack@google.com sys.exit(exitcode) 14212855Sgabeblack@google.com 14312855Sgabeblack@google.com