_argparse: support add_argument_group
authorZac Medico <zmedico@gentoo.org>
Fri, 2 Aug 2013 18:03:49 +0000 (11:03 -0700)
committerZac Medico <zmedico@gentoo.org>
Fri, 2 Aug 2013 18:03:49 +0000 (11:03 -0700)
pym/portage/util/_argparse.py

index 4227f4c47b4d378dd10e368cbb3e1fe1f4cd68d1..a44afed7b82ff167c61217f13eded3642df6312c 100644 (file)
@@ -7,7 +7,7 @@ try:
        from argparse import ArgumentParser
 except ImportError:
        # Compatibility with Python 2.6 and 3.1
-       from optparse import OptionParser
+       from optparse import OptionGroup, OptionParser
 
        class ArgumentParser(object):
                def __init__(self, **kwargs):
@@ -15,6 +15,16 @@ except ImportError:
                        if add_help is not None:
                                kwargs["add_help_option"] = add_help
                        parser = OptionParser(**kwargs)
+                       self._parser = parser
                        self.add_argument = parser.add_option
                        self.parse_known_args = parser.parse_args
                        self.parse_args = parser.parse_args
+
+               def add_argument_group(self, title=None, **kwargs):
+                       optiongroup = OptionGroup(self._parser, title, **kwargs)
+                       self._parser.add_option_group(optiongroup)
+                       return _ArgumentGroup(optiongroup)
+
+       class _ArgumentGroup(object):
+               def __init__(self, optiongroup):
+                       self.add_argument = optiongroup.add_option