Skip to content

Commit 68d86eb

Browse files
authored
gh-157127: Reword the argparse mutually exclusive group error message (#157134)
1 parent 64d315a commit 68d86eb

5 files changed

Lines changed: 10 additions & 7 deletions

File tree

‎Doc/library/argparse.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2099,7 +2099,7 @@ Mutual exclusion
20992099
>>> group.add_argument('--bar', action='store_false')
21002100
>>> parser.parse_args([])
21012101
usage: PROG [-h] (--foo | --bar)
2102-
PROG: error: one of the arguments --foo --bar is required
2102+
PROG: error: one of the following arguments is required: --foo, --bar
21032103

21042104
Note that currently mutually exclusive argument groups do not support the
21052105
*title* and *description* arguments of

‎Lib/argparse.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,8 +2499,8 @@ def consume_positionals(start_index):
24992499
names = [_get_action_name(action)
25002500
for action in group._group_actions
25012501
if action.help is not SUPPRESS]
2502-
msg = _('one of the arguments %s is required')
2503-
raise ArgumentError(None, msg % ' '.join(names))
2502+
msg = _('one of the following arguments is required: %s')
2503+
raise ArgumentError(None, msg % ', '.join(names))
25042504

25052505
# return the updated namespace and the extra arguments
25062506
return namespace, extras

‎Lib/test/test_argparse.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6866,7 +6866,7 @@ def test_required_exclusive(self):
68666866
args = parser.parse_intermixed_args('1 --foo 2'.split())
68676867
self.assertEqual(NS(badger=['1', '2'], foo=True, spam=None), args)
68686868
self.assertRaisesRegex(argparse.ArgumentError,
6869-
'one of the arguments --foo --spam is required',
6869+
'one of the following arguments is required: --foo, --spam',
68706870
parser.parse_intermixed_args, '1 2'.split())
68716871
self.assertEqual(group.required, True)
68726872

@@ -6882,7 +6882,7 @@ def test_required_exclusive_with_positional(self):
68826882
args = parser.parse_intermixed_args(['a', 'b'])
68836883
self.assertEqual(NS(foo=False, spam=None, badger=['a', 'b']), args)
68846884
self.assertRaisesRegex(argparse.ArgumentError,
6885-
'one of the arguments --foo --spam badger is required',
6885+
'one of the following arguments is required: --foo, --spam, badger',
68866886
parser.parse_intermixed_args, [])
68876887
self.assertRaisesRegex(argparse.ArgumentError,
68886888
'argument badger: not allowed with argument --foo',
@@ -7258,7 +7258,7 @@ def test_required_mutually_exclusive_args(self):
72587258
group.add_argument('--bar')
72597259
group.add_argument('--baz')
72607260
self.assertRaisesRegex(argparse.ArgumentError,
7261-
'one of the arguments --bar --baz is required',
7261+
'one of the following arguments is required: --bar, --baz',
72627262
self.parser.parse_args, [])
72637263

72647264
def test_conflicting_mutually_exclusive_args_optional_with_metavar(self):

‎Lib/test/translationdata/argparse/msgids.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ invalid %(type)s value: %(value)r
1818
invalid choice: %(value)r (choose from %(choices)s)
1919
invalid choice: %(value)r, maybe you meant %(closest)r? (choose from %(choices)s)
2020
not allowed with argument %s
21-
one of the arguments %s is required
21+
one of the following arguments is required: %s
2222
option '%(option)s' is deprecated
2323
options
2424
positional arguments
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Reword the :mod:`argparse` error message reported for a missing required
2+
mutually exclusive group to ``one of the following arguments is required:
3+
--foo, --bar``, so that the argument names are separated by commas.

0 commit comments

Comments
 (0)