GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-29553: Fix ArgumentParser.format_usage() for mutually exclusive g… · python/cpython@31ea447

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Lib
    • argparse.py
    • test
      • test_argparse.py
  • Misc/NEWS.d/next/Library
    • 2019-07-27-10-14-45.bpo-29553.TVeIDe.rst
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Lib/argparse.py‎

Copy file name to clipboardExpand all lines: Lib/argparse.py
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,19 @@ def _format_actions_usage(self, actions, groups):
407407
inserts[start] += ' ['
408408
else:
409409
inserts[start] = '['
410-
inserts[end] = ']'
410+
if end in inserts:
411+
inserts[end] += ']'
412+
else:
413+
inserts[end] = ']'
411414
else:
412415
if start in inserts:
413416
inserts[start] += ' ('
414417
else:
415418
inserts[start] = '('
416-
inserts[end] = ')'
419+
if end in inserts:
420+
inserts[end] += ')'
421+
else:
422+
inserts[end] = ')'
417423
for i in range(start + 1, end):
418424
inserts[i] = '|'
419425

Collapse file

‎Lib/test/test_argparse.py‎

Copy file name to clipboardExpand all lines: Lib/test/test_argparse.py
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,46 @@ def get_parser(self, required):
27722772
-c c help
27732773
'''
27742774

2775+
class TestMutuallyExclusiveNested(MEMixin, TestCase):
2776+
2777+
def get_parser(self, required):
2778+
parser = ErrorRaisingArgumentParser(prog='PROG')
2779+
group = parser.add_mutually_exclusive_group(required=required)
2780+
group.add_argument('-a')
2781+
group.add_argument('-b')
2782+
group2 = group.add_mutually_exclusive_group(required=required)
2783+
group2.add_argument('-c')
2784+
group2.add_argument('-d')
2785+
group3 = group2.add_mutually_exclusive_group(required=required)
2786+
group3.add_argument('-e')
2787+
group3.add_argument('-f')
2788+
return parser
2789+
2790+
usage_when_not_required = '''\
2791+
usage: PROG [-h] [-a A | -b B | [-c C | -d D | [-e E | -f F]]]
2792+
'''
2793+
usage_when_required = '''\
2794+
usage: PROG [-h] (-a A | -b B | (-c C | -d D | (-e E | -f F)))
2795+
'''
2796+
2797+
help = '''\
2798+
2799+
optional arguments:
2800+
-h, --help show this help message and exit
2801+
-a A
2802+
-b B
2803+
-c C
2804+
-d D
2805+
-e E
2806+
-f F
2807+
'''
2808+
2809+
# We are only interested in testing the behavior of format_usage().
2810+
test_failures_when_not_required = None
2811+
test_failures_when_required = None
2812+
test_successes_when_not_required = None
2813+
test_successes_when_required = None
2814+
27752815
# =================================================
27762816
# Mutually exclusive group in parent parser tests
27772817
# =================================================
Collapse file

‎Misc/NEWS.d/next/Library/2019-07-27-10-14-45.bpo-29553.TVeIDe.rst‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed :meth:`argparse.ArgumentParser.format_usage` for mutually exclusive groups.
2+
Patch by Andrew Nester.

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.