Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a1454f

Browse files
committed
Use proper gettext plural forms in optparse (closes #4391).
Original patch by Dwayne Bailey.
1 parent 2592f62 commit 6a1454f

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

Lib/optparse.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,16 @@ def _repr(self):
8686
# Id: errors.py 509 2006-04-20 00:58:24Z gward
8787

8888
try:
89-
from gettext import gettext
89+
from gettext import gettext, ngettext
9090
except ImportError:
9191
def gettext(message):
9292
return message
93+
94+
def ngettext(singular, plural, n):
95+
if n == 1:
96+
return singular
97+
return plural
98+
9399
_ = gettext
94100

95101

@@ -1478,11 +1484,10 @@ def _process_long_opt(self, rargs, values):
14781484
if option.takes_value():
14791485
nargs = option.nargs
14801486
if len(rargs) < nargs:
1481-
if nargs == 1:
1482-
self.error(_("%s option requires an argument") % opt)
1483-
else:
1484-
self.error(_("%s option requires %d arguments")
1485-
% (opt, nargs))
1487+
self.error(ngettext(
1488+
"%(option)s option requires %(number)d argument",
1489+
"%(option)s option requires %(number)d arguments",
1490+
nargs) % {"option": opt, "number": nargs})
14861491
elif nargs == 1:
14871492
value = rargs.pop(0)
14881493
else:
@@ -1517,11 +1522,10 @@ def _process_short_opts(self, rargs, values):
15171522

15181523
nargs = option.nargs
15191524
if len(rargs) < nargs:
1520-
if nargs == 1:
1521-
self.error(_("%s option requires an argument") % opt)
1522-
else:
1523-
self.error(_("%s option requires %d arguments")
1524-
% (opt, nargs))
1525+
self.error(ngettext(
1526+
"%(option)s option requires %(number)d argument",
1527+
"%(option)s option requires %(number)d arguments",
1528+
nargs) % {"option": opt, "number": nargs})
15251529
elif nargs == 1:
15261530
value = rargs.pop(0)
15271531
else:

Lib/test/test_optparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ def setUp(self):
631631
option_list=options)
632632

633633
def test_required_value(self):
634-
self.assertParseFail(["-a"], "-a option requires an argument")
634+
self.assertParseFail(["-a"], "-a option requires 1 argument")
635635

636636
def test_invalid_integer(self):
637637
self.assertParseFail(["-b", "5x"],

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Core and Builtins
7575
Library
7676
-------
7777

78+
- Issue #4391: Use proper gettext plural forms in optparse.
79+
7880
- Issue #11563: Connection:close header is sent by requests using URLOpener
7981
class which helps in closing of sockets after connection is over. Patch
8082
contributions by Jeff McNeil and Nadeem Vawda.

0 commit comments

Comments
 (0)