8000 gh-63143: Fix parsing mutually exclusive arguments in argparse by serhiy-storchaka · Pull Request #124307 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-63143: Fix parsing mutually exclusive arguments in argparse 8000 #124307

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

Merged
Merged
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
Prev Previous commit
Add test for gh-85531.
  • Loading branch information
serhiy-storchaka committed Sep 21, 2024
commit 82a56b69861b65c4f154ffb79619b0c87cf2d47b
36 changes: 36 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3288,6 +3288,41 @@ def get_parser(self, required):
test_successes_when_required = None


class TestMutuallyExclusiveOptionalOptional(MEMixin, TestCase):
def get_parser(self, required=None):
parser = ErrorRaisingArgumentParser(prog='PROG')
group = parser.add_mutually_exclusive_group(required=required)
group.add_argument('--foo')
group.add_argument('--bar', nargs='?')
return parser

failures = [
'--foo X --bar Y',
'--foo X --bar',
]
successes = [
('--foo X', NS(foo='X', bar=None)),
('--bar X', NS(foo=None, bar='X')),
('--bar', NS(foo=None, bar=None)),
]
successes_when_not_required = [
('', NS(foo=None, bar=None)),
]
usage_when_required = '''\
usage: PROG [-h] (--foo FOO | --bar [BAR])
'''
usage_when_not_required = '''\
usage: PROG [-h] [--foo FOO | --bar [BAR]]
'''
help = '''\

options:
-h, --help show this help message and exit
--foo FOO
--bar [BAR]
'''


class TestMutuallyExclusiveOptionalWithDefault(MEMixin, TestCase):
def get_parser(self, required=None):
parser = ErrorRaisingArgumentParser(prog='PROG')
Expand Down Expand Up @@ -3322,6 +3357,7 @@ def get_parser(self, required=None):
--bar BAR
'''


class TestMutuallyExclusivePositionalWithDefault(MEMixin, TestCase):
def get_parser(self, required=None):
parser = ErrorRaisingArgumentParser(prog='PROG')
Expand Down
Loading
0