_argparse: improve parse_* method compatibility v2.2.0_alpha192
authorZac Medico <zmedico@gentoo.org>
Sat, 3 Aug 2013 09:39:00 +0000 (02:39 -0700)
committerZac Medico <zmedico@gentoo.org>
Sat, 3 Aug 2013 09:39:00 +0000 (02:39 -0700)
pym/portage/util/_argparse.py

index a9cdc9e62aad153e545d7279946c7436db8872c8..6ca785235db51c63a5eacda8f8af9e338e3853e7 100644 (file)
@@ -9,6 +9,8 @@ except ImportError:
        # Compatibility with Python 2.6 and 3.1
        from optparse import OptionGroup, OptionParser
 
+       from portage.localization import _
+
        class ArgumentParser(object):
                def __init__(self, **kwargs):
                        add_help = kwargs.pop("add_help", None)
@@ -17,8 +19,6 @@ except ImportError:
                        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
                        self.print_help = parser.print_help
                        self.error = parser.error
 
@@ -27,6 +27,16 @@ except ImportError:
                        self._parser.add_option_group(optiongroup)
                        return _ArgumentGroup(optiongroup)
 
+               def parse_known_args(self, args=None, namespace=None):
+                       return self._parser.parse_args(args, namespace)
+
+               def parse_args(self, args=None, namespace=None):
+                       args, argv = self.parse_known_args(args, namespace)
+                       if argv:
+                               msg = _('unrecognized arguments: %s')
+                               self.error(msg % ' '.join(argv))
+                       return args
+
        class _ArgumentGroup(object):
                def __init__(self, optiongroup):
                        self.add_argument = optiongroup.add_option