diff options
author | Guido Günther <agx@sigxcpu.org> | 2016-09-14 08:02:07 +0200 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2016-09-15 07:01:13 +0200 |
commit | ef7ca4a4d7a71a470182f5e88a2a3398853daa9f (patch) | |
tree | a97f93774d57d890e1a1b7dfd56546a0d64496ae | |
parent | 3b94d2373c49bd3a63fe27ecf6dbf4bee7b8c567 (diff) |
config: allow to set short options
-rw-r--r-- | gbp/config.py | 17 | ||||
-rw-r--r-- | tests/18_test_Config.py | 22 |
2 files changed, 37 insertions, 2 deletions
diff --git a/gbp/config.py b/gbp/config.py index 002d078e..4c32aafd 100644 --- a/gbp/config.py +++ b/gbp/config.py @@ -350,6 +350,9 @@ class GbpOptionParser(OptionParser): "'Default is '%(bare)s'.", } + short_opts = { + } + def_config_files = {'/etc/git-buildpackage/gbp.conf': 'system', '~/.gbp.conf': 'global', '%(top_dir)s/.gbp.conf': None, @@ -579,6 +582,14 @@ class GbpOptionParser(OptionParser): default = self.config[option_name] return default + def get_opt_names(self, option_name): + names = ["--%s%s" % (self.prefix, option_name)] + if option_name in self.short_opts: + if self.prefix: + raise ValueError("Options with prefix cannot have a short option") + names.insert(0, self.short_opts[option_name]) + return names + @save_option def add_config_file_option(self, option_name, dest, help=None, **kwargs): """ @@ -592,7 +603,8 @@ class GbpOptionParser(OptionParser): """ if not help: help = self.help[option_name] - OptionParser.add_option(self, "--%s%s" % (self.prefix, option_name), dest=dest, + opt_names = self.get_opt_names(option_name) + OptionParser.add_option(self, *opt_names, dest=dest, default=self.get_default(option_name, **kwargs), help=help % self.config, **kwargs) @@ -681,7 +693,8 @@ class GbpOptionGroup(OptionGroup): """ if not help: help = self.parser.help[option_name] - OptionGroup.add_option(self, "--%s%s" % (self.parser.prefix, option_name), dest=dest, + opt_names = self.parser.get_opt_names(option_name) + OptionGroup.add_option(self, *opt_names, dest=dest, default=self.parser.get_default(option_name, **kwargs), help=help % self.parser.config, **kwargs) diff --git a/tests/18_test_Config.py b/tests/18_test_Config.py index 306999f3..54c00bec 100644 --- a/tests/18_test_Config.py +++ b/tests/18_test_Config.py @@ -109,3 +109,25 @@ class TestConfigParser(unittest.TestCase, GbpLogTester): self.assertTrue('upstream-branch' in params) self.assertTrue('debian-branch' in params) self.assertTrue('color' in params) + + def test_short_option_with_prefix(self): + """Options with short options can't have a prefix""" + class TestOptonParser(GbpOptionParser): + list_opts = [] + defaults = {'withshort': 'foo'} + short_opts = {'withshort': '-S'} + parser = TestOptonParser('cmd', prefix='p') + with self.assertRaisesRegexp(ValueError, "Options with prefix cannot have a short option"): + parser.add_config_file_option(option_name="withshort", dest="with_short", help="foo") + + def test_short_option(self): + class TestOptionParser(GbpOptionParser): + list_opts = [] + defaults = {'withshort': 'foo'} + short_opts = {'withshort': '-S'} + + parser = TestOptionParser('cmd') + parser.add_config_file_option(option_name="withshort", dest="with_short", help="foo") + self.assertItemsEqual(['withshort'], parser.valid_options) + self.assertTrue(parser.has_option("--withshort")) + self.assertTrue(parser.has_option("-S")) |