Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita27c540

Browse files
[3.11]gh-96310: Fix a traceback in argparse when all options in a mutually exclusive group are suppressed (GH-96311) (GH-115768)
Reproducer depends on terminal size - the traceback occurs when there'san option long enough so the usage line doesn't fit the terminal width.Option order is also important for reproducibility.Excluding empty groups (with all options suppressed) from insertsfixes the problem.(cherry picked from commit5f7df88)Co-authored-by: Daniel Mach <daniel.mach@suse.com>
1 parent35e5bbc commita27c540

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

‎Lib/argparse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ def _format_actions_usage(self, actions, groups):
414414
suppressed_actions_count+=1
415415

416416
exposed_actions_count=group_action_count-suppressed_actions_count
417+
ifnotexposed_actions_count:
418+
continue
417419

418420
ifnotgroup.required:
419421
ifstartininserts:

‎Lib/test/test_argparse.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,27 @@ def test_help(self):
27432743
'''
27442744
self.assertEqual(parser.format_help(),textwrap.dedent(expected))
27452745

2746+
deftest_help_subparser_all_mutually_exclusive_group_members_suppressed(self):
2747+
self.maxDiff=None
2748+
parser=ErrorRaisingArgumentParser(prog='PROG')
2749+
commands=parser.add_subparsers(title="commands",dest="command")
2750+
cmd_foo=commands.add_parser("foo")
2751+
group=cmd_foo.add_mutually_exclusive_group()
2752+
group.add_argument('--verbose',action='store_true',help=argparse.SUPPRESS)
2753+
group.add_argument('--quiet',action='store_true',help=argparse.SUPPRESS)
2754+
longopt='--'+'long'*32
2755+
longmeta='LONG'*32
2756+
cmd_foo.add_argument(longopt)
2757+
expected=f'''\
2758+
usage: PROG foo [-h]
2759+
[{longopt}{longmeta}]
2760+
2761+
options:
2762+
-h, --help show this help message and exit
2763+
{longopt}{longmeta}
2764+
'''
2765+
self.assertEqual(cmd_foo.format_help(),textwrap.dedent(expected))
2766+
27462767
deftest_empty_group(self):
27472768
# See issue 26952
27482769
parser=argparse.ArgumentParser()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a traceback in:mod:`argparse` when all options in a mutually exclusive
2+
group are suppressed.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp