Skip to content

bpo-29553: Fixed ArgumentParses format_usage for mutually exclusive groups #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-29553 - Fixed ArgumentParses format_usage for mutually exclusive …
…groups
  • Loading branch information
andrewnester committed Feb 19, 2017
commit d01e2eec11d6a6780f5023cede95b64d8ec3566e
10 changes: 8 additions & 2 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,19 @@ def _format_actions_usage(self, actions, groups):
inserts[start] += ' ['
else:
inserts[start] = '['
inserts[end] = ']'
if end in inserts:
inserts[end] += ']'
else:
inserts[end] = ']'
else:
if start in inserts:
inserts[start] += ' ('
else:
inserts[start] = '('
inserts[end] = ')'
if end in inserts:
inserts[end] += ')'
else:
inserts[end] = ')'
for i in range(start + 1, end):
inserts[i] = '|'

Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2725,6 +2725,35 @@ def get_parser(self, required):
-c c help
'''

class TestMutuallyExclusiveNested(TestCase):
def test_format_usage(self):
parser = ErrorRaisingArgumentParser(prog='PROG')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you can't use the existing test infrastructure? I think it's possible to convert these tests to follow the style of TestMutuallyExclusiveOptionalAndPositional: https://github.com/python/cpython/blob/82e38bd80113ecfc7aff2fb3edaf2802fe21c1fb/Lib/test/test_argparse.py#L2560

We can define a get_parser() method and use usage_when_required and usage_when_not_required to check output of parser.format_help().

group = parser.add_mutually_exclusive_group()
group.add_argument('-a')
group.add_argument('-b')
group2 = group.add_mutually_exclusive_group()
group2.add_argument('-c')
group2.add_argument('-d')
group3 = group2.add_mutually_exclusive_group()
group3.add_argument('-e')
group3.add_argument('-f')
self.assertEqual(parser.format_usage(),
'usage: PROG [-h] [-a A | -b B | [-c C | -d D | [-e E | -f F]]]\n')

def test_format_usage_required(self):
parser = ErrorRaisingArgumentParser(prog='PROG')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-a')
group.add_argument('-b')
group2 = group.add_mutually_exclusive_group(required=True)
group2.add_argument('-c')
group2.add_argument('-d')
group3 = group2.add_mutually_exclusive_group(required=True)
group3.add_argument('-e')
group3.add_argument('-f')
self.assertEqual(parser.format_usage(),
'usage: PROG [-h] (-a A | -b B | (-c C | -d D | (-e E | -f F)))\n')

# =================================================
# Mutually exclusive group in parent parser tests
# =================================================
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy