diff options
author | Guido Günther <agx@sigxcpu.org> | 2008-12-28 23:50:51 +0100 |
---|---|---|
committer | Guido Guenther <agx@sigxcpu.org> | 2008-12-28 23:53:39 +0100 |
commit | 1cce0b50074dd825e7a2d93675d9e4e0ea48f558 (patch) | |
tree | 5f741b691aa5850d044dc5590b9c4d0ccf37ca6b /gbp | |
parent | 54b9a3eeba9c0d95713e868ac527b4ff5941bd31 (diff) | |
download | git-buildpackage-1cce0b50074dd825e7a2d93675d9e4e0ea48f558.tar.gz git-buildpackage-1cce0b50074dd825e7a2d93675d9e4e0ea48f558.tar.bz2 git-buildpackage-1cce0b50074dd825e7a2d93675d9e4e0ea48f558.zip |
add support for --foo and --no-foo options
by looking add at foo and no-foo config file defaults.
Diffstat (limited to 'gbp')
-rw-r--r-- | gbp/config.py | 45 |
1 files changed, 38 insertions, 7 deletions
diff --git a/gbp/config.py b/gbp/config.py index 20bb4a90..8814cd7d 100644 --- a/gbp/config.py +++ b/gbp/config.py @@ -94,14 +94,45 @@ class GbpOptionParser(OptionParser): self.__parse_config_files() OptionParser.__init__(self, usage=usage, version='%s %s' % (self.command, gbp_version)) + def _is_boolean(self, option_name, *args, **kwargs): + """is option_name a boolean option""" + ret = False + try: + if kwargs['action'] in [ 'store_true', 'store_false' ]: + ret=True + except KeyError: + ret=False + return ret + + def _get_bool_default(self, option_name): + """ + get default for boolean options + this way we can handle no-foo=True and foo=False + """ + if option_name.startswith('no-'): + pos = option_name[3:] + neg = option_name + else: + pos = option_name + neg = "no-%s" % option_name + + try: + default = self.config[pos] + except KeyError: + default = self.config[neg] + + if default in [ 'True', 'False' ]: + ret = eval(default) + else: + raise ValueError, "Boolean options must be True or False" + return ret + def get_default(self, option_name, **kwargs): - default = self.config[option_name] - if kwargs.has_key('action'): - if kwargs['action'] in [ 'store_true', 'store_false' ] and self.config[option_name]: - if self.config[option_name] in [ 'True', 'False' ]: - default = eval(self.config[option_name]) - else: - raise ValueError, "Boolean options must be True or False" + """get the default value""" + if self._is_boolean(self, option_name, **kwargs): + default = self._get_bool_default(option_name) + else: + default = self.config[option_name] return default def add_config_file_option(self, option_name, dest, help=None, **kwargs): |