mirror of https://github.com/python/cpython
gh-117941: Reject option names starting with "--no-" in argparse.BooleanOptionalAction (GH-125894)
They never worked correctly.
This commit is contained in:
parent
819830f34a
commit
79805d2284
|
@ -863,6 +863,9 @@ class BooleanOptionalAction(Action):
|
||||||
_option_strings.append(option_string)
|
_option_strings.append(option_string)
|
||||||
|
|
||||||
if option_string.startswith('--'):
|
if option_string.startswith('--'):
|
||||||
|
if option_string.startswith('--no-'):
|
||||||
|
raise ValueError(f'invalid option name {option_string!r} '
|
||||||
|
f'for BooleanOptionalAction')
|
||||||
option_string = '--no-' + option_string[2:]
|
option_string = '--no-' + option_string[2:]
|
||||||
_option_strings.append(option_string)
|
_option_strings.append(option_string)
|
||||||
|
|
||||||
|
|
|
@ -789,6 +789,13 @@ class TestBooleanOptionalAction(ParserTestCase):
|
||||||
|
|
||||||
self.assertIn("got an unexpected keyword argument 'const'", str(cm.exception))
|
self.assertIn("got an unexpected keyword argument 'const'", str(cm.exception))
|
||||||
|
|
||||||
|
def test_invalid_name(self):
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
with self.assertRaises(ValueError) as cm:
|
||||||
|
parser.add_argument('--no-foo', action=argparse.BooleanOptionalAction)
|
||||||
|
self.assertEqual(str(cm.exception),
|
||||||
|
"invalid option name '--no-foo' for BooleanOptionalAction")
|
||||||
|
|
||||||
class TestBooleanOptionalActionRequired(ParserTestCase):
|
class TestBooleanOptionalActionRequired(ParserTestCase):
|
||||||
"""Tests BooleanOptionalAction required"""
|
"""Tests BooleanOptionalAction required"""
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
:class:`!argparse.BooleanOptionalAction` now rejects option names starting
|
||||||
|
with ``--no-``.
|
Loading…
Reference in New Issue