Skip to content

Commit 9654987

Browse files
gh-86463: Fix default prog in subparsers if usage is used in the main parser
The usage parameter of argparse.ArgumentParser no longer affects the default value of the prog parameter in subparsers. Previously the full custom usage of the main parser was used as the prog prefix in subparsers.
1 parent 834ba5a commit 9654987

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

Doc/library/argparse.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ arguments it contains. The default message can be overridden with the
192192
The ``%(prog)s`` format specifier is available to fill in the program name in
193193
your usage messages.
194194

195+
When a custom usage message is specified for the main parser, consider
196+
also passing the ``prog`` argument to :meth:`~ArgumentParser.add_subparsers`
197+
or the ``prog`` and the ``usage`` arguments to
198+
:meth:`~_SubParsersAction.add_parser`.
199+
195200

196201
.. _description:
197202

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,7 @@ def add_subparsers(self, **kwargs):
18891889
formatter = self._get_formatter()
18901890
positionals = self._get_positional_actions()
18911891
groups = self._mutually_exclusive_groups
1892-
formatter.add_usage(self.usage, positionals, groups, '')
1892+
formatter.add_usage(None, positionals, groups, '')
18931893
kwargs['prog'] = formatter.format_help().strip()
18941894

18951895
# create the parsers action and add it to the positionals list

Lib/test/test_argparse.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,16 +2388,17 @@ def assertArgumentParserError(self, *args, **kwargs):
23882388
self.assertRaises(ArgumentParserError, *args, **kwargs)
23892389

23902390
def _get_parser(self, subparser_help=False, prefix_chars=None,
2391-
aliases=False):
2391+
aliases=False, usage=None):
23922392
# create a parser with a subparsers argument
23932393
if prefix_chars:
23942394
parser = ErrorRaisingArgumentParser(
2395-
prog='PROG', description='main description', prefix_chars=prefix_chars)
2395+
prog='PROG', description='main description', usage=usage,
2396+
prefix_chars=prefix_chars)
23962397
parser.add_argument(
23972398
prefix_chars[0] * 2 + 'foo', action='store_true', help='foo help')
23982399
else:
23992400
parser = ErrorRaisingArgumentParser(
2400-
prog='PROG', description='main description')
2401+
prog='PROG', description='main description', usage=usage)
24012402
parser.add_argument(
24022403
'--foo', action='store_true', help='foo help')
24032404
parser.add_argument(
@@ -2434,7 +2435,8 @@ def _get_parser(self, subparser_help=False, prefix_chars=None,
24342435
parser2.add_argument('z', type=complex, nargs='*', help='z help')
24352436

24362437
# add third sub-parser
2437-
parser3_kwargs = dict(description='3 description')
2438+
parser3_kwargs = dict(description='3 description',
2439+
usage='PROG --foo bar 3 t ...')
24382440
if subparser_help:
24392441
parser3_kwargs['help'] = '3 help'
24402442
parser3 = subparsers.add_parser('3', **parser3_kwargs)
@@ -2456,6 +2458,47 @@ def test_parse_args_failures(self):
24562458
args = args_str.split()
24572459
self.assertArgumentParserError(self.parser.parse_args, args)
24582460

2461+
def test_parse_args_failures_details(self):
2462+
for args_str, usage_str, error_str in [
2463+
('',
2464+
'usage: PROG [-h] [--foo] bar {1,2,3} ...',
2465+
'PROG: error: the following arguments are required: bar'),
2466+
('0.5 1 -y',
2467+
'usage: PROG bar 1 [-h] [-w W] {a,b,c}',
2468+
'PROG bar 1: error: the following arguments are required: x'),
2469+
('0.5 3',
2470+
'usage: PROG --foo bar 3 t ...',
2471+
'PROG bar 3: error: the following arguments are required: t'),
2472+
]:
2473+
with self.subTest(args_str):
2474+
args = args_str.split()
2475+
with self.assertRaises(ArgumentParserError) as cm:
2476+
self.parser.parse_args(args)
2477+
self.assertEqual(cm.exception.args[0], 'SystemExit')
2478+
self.assertEqual(cm.exception.args[2], f'{usage_str}\n{error_str}\n')
2479+
2480+
def test_parse_args_failures_details_custom_usage(self):
2481+
parser = self._get_parser(usage='PROG [--foo] bar 1 [-w W] {a,b,c}\n'
2482+
' PROG --foo bar 3 t ...')
2483+
for args_str, usage_str, error_str in [
2484+
('',
2485+
'usage: PROG [--foo] bar 1 [-w W] {a,b,c}\n'
2486+
' PROG --foo bar 3 t ...',
2487+
'PROG: error: the following arguments are required: bar'),
2488+
('0.5 1 -y',
2489+
'usage: PROG bar 1 [-h] [-w W] {a,b,c}',
2490+
'PROG bar 1: error: the following arguments are required: x'),
2491+
('0.5 3',
2492+
'usage: PROG --foo bar 3 t ...',
2493+
'PROG bar 3: error: the following arguments are required: t'),
2494+
]:
2495+
with self.subTest(args_str):
2496+
args = args_str.split()
2497+
with self.assertRaises(ArgumentParserError) as cm:
2498+
parser.parse_args(args)
2499+
self.assertEqual(cm.exception.args[0], 'SystemExit')
2500+
self.assertEqual(cm.exception.args[2], f'{usage_str}\n{error_str}\n')
2501+
24592502
def test_parse_args(self):
24602503
# check some non-failure cases:
24612504
self.assertEqual(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The ``usage`` parameter of :class:`argparse.ArgumentParser` no longer
2+
affects the default value of the ``prog`` parameter in subparsers.

0 commit comments

Comments
 (0)