]> granicus.if.org Git - python/commitdiff
Use proper gettext plural forms in optparse (closes #4391).
authorÉric Araujo <merwok@netwok.org>
Sun, 20 Mar 2011 18:59:25 +0000 (19:59 +0100)
committerÉric Araujo <merwok@netwok.org>
Sun, 20 Mar 2011 18:59:25 +0000 (19:59 +0100)
Original patch by Dwayne Bailey.

Lib/optparse.py
Lib/test/test_optparse.py
Misc/NEWS

index 3eb652a6fe99eb6d6c9d977d46b19b7d99ca2fbd..c2077136f05771f340a483ec810922fe0504a8b7 100644 (file)
@@ -86,10 +86,16 @@ def _repr(self):
 #   Id: errors.py 509 2006-04-20 00:58:24Z gward
 
 try:
-    from gettext import gettext
+    from gettext import gettext, ngettext
 except ImportError:
     def gettext(message):
         return message
+
+    def ngettext(singular, plural, n):
+        if n == 1:
+            return singular
+        return plural
+
 _ = gettext
 
 
@@ -1478,11 +1484,10 @@ class OptionParser (OptionContainer):
         if option.takes_value():
             nargs = option.nargs
             if len(rargs) < nargs:
-                if nargs == 1:
-                    self.error(_("%s option requires an argument") % opt)
-                else:
-                    self.error(_("%s option requires %d arguments")
-                               % (opt, nargs))
+                self.error(ngettext(
+                    "%(option)s option requires %(number)d argument",
+                    "%(option)s option requires %(number)d arguments",
+                    nargs) % {"option": opt, "number": nargs})
             elif nargs == 1:
                 value = rargs.pop(0)
             else:
@@ -1517,11 +1522,10 @@ class OptionParser (OptionContainer):
 
                 nargs = option.nargs
                 if len(rargs) < nargs:
-                    if nargs == 1:
-                        self.error(_("%s option requires an argument") % opt)
-                    else:
-                        self.error(_("%s option requires %d arguments")
-                                   % (opt, nargs))
+                    self.error(ngettext(
+                        "%(option)s option requires %(number)d argument",
+                        "%(option)s option requires %(number)d arguments",
+                        nargs) % {"option": opt, "number": nargs})
                 elif nargs == 1:
                     value = rargs.pop(0)
                 else:
index 7b956120243cbfe6e704e8626837e3b47c8b00d1..61f44ee06db5436238d97cf9375f6c25507064a9 100644 (file)
@@ -631,7 +631,7 @@ class TestStandard(BaseTest):
                                                option_list=options)
 
     def test_required_value(self):
-        self.assertParseFail(["-a"], "-a option requires an argument")
+        self.assertParseFail(["-a"], "-a option requires 1 argument")
 
     def test_invalid_integer(self):
         self.assertParseFail(["-b", "5x"],
index 7557bafc1b2c6b003e29f3e609670c69f7544eb1..310132783f1d787a5056bba0a1e9479a7ea120a2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -75,6 +75,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #4391: Use proper gettext plural forms in optparse.
+
 - Issue #11563: Connection:close header is sent by requests using URLOpener
   class which helps in closing of sockets after connection is over. Patch
   contributions by Jeff McNeil and Nadeem Vawda.