summaryrefslogtreecommitdiff
path: root/gbp/config.py
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2014-04-02 22:13:46 +0200
committerGuido Günther <agx@sigxcpu.org>2014-04-03 20:34:07 +0200
commit459d9bfcce481b0da71e9493e644488c83e0e9ba (patch)
tree4fa6b653354cba87f5da0953b151d54af474a5bb /gbp/config.py
parentb393080ac3b98342b53849d14049db024183f0cb (diff)
downloadgit-buildpackage-459d9bfcce481b0da71e9493e644488c83e0e9ba.tar.gz
git-buildpackage-459d9bfcce481b0da71e9493e644488c83e0e9ba.tar.bz2
git-buildpackage-459d9bfcce481b0da71e9493e644488c83e0e9ba.zip
config: add decorator to add_option_* functions
This allows us to build an internal list of valid options and print these.
Diffstat (limited to 'gbp/config.py')
-rw-r--r--gbp/config.py48
1 files changed, 36 insertions, 12 deletions
diff --git a/gbp/config.py b/gbp/config.py
index 710eddf8..fc31076e 100644
--- a/gbp/config.py
+++ b/gbp/config.py
@@ -45,6 +45,27 @@ def check_tristate(option, opt, value):
else:
return val
+
+def safe_option(f):
+ def _decorator(self, *args, **kwargs):
+ obj = self
+ option_name = kwargs.get('option_name')
+ if not option_name and len(args):
+ option_name = args[0]
+
+ # We're decorating GbpOption not GbpOptionParser
+ if not hasattr(obj, 'valid_options'):
+ if not hasattr(obj, 'parser'):
+ raise ValueError("Can only decorete GbpOptionParser and GbpOptionGroup not %s" % obj)
+ else:
+ obj = obj.parser
+
+ if option_name and not option_name.startswith('no-'):
+ obj.valid_options.append(option_name)
+ return f(self, *args, **kwargs)
+ return _decorator
+
+
class GbpOption(Option):
TYPES = Option.TYPES + ('path', 'tristate')
TYPE_CHECKER = copy(Option.TYPE_CHECKER)
@@ -373,6 +394,7 @@ class GbpOptionParser(OptionParser):
self.config = {}
self.config_files = self.get_config_files()
self.parse_config_files()
+ self.valid_options = []
if self.command.startswith('git-') or self.command.startswith('gbp-'):
prog = self.command
@@ -426,15 +448,16 @@ class GbpOptionParser(OptionParser):
default = self.config[option_name]
return default
+ @safe_option
def add_config_file_option(self, option_name, dest, help=None, **kwargs):
"""
set a option for the command line parser, the default is read from the config file
- @param option_name: name of the option
- @type option_name: string
- @param dest: where to store this option
- @type dest: string
- @param help: help text
- @type help: string
+ param option_name: name of the option
+ type option_name: string
+ param dest: where to store this option
+ type dest: string
+ param help: help text
+ type help: string
"""
if not help:
help = self.help[option_name]
@@ -460,15 +483,16 @@ class GbpOptionParser(OptionParser):
class GbpOptionGroup(OptionGroup):
+ @safe_option
def add_config_file_option(self, option_name, dest, help=None, **kwargs):
"""
set a option for the command line parser, the default is read from the config file
- @param option_name: name of the option
- @type option_name: string
- @param dest: where to store this option
- @type dest: string
- @param help: help text
- @type help: string
+ param option_name: name of the option
+ type option_name: string
+ param dest: where to store this option
+ type dest: string
+ param help: help text
+ type help: string
"""
if not help:
help = self.parser.help[option_name]