mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[3.13] gh-61181: Fix support of choices with string value in argparse (GH-124578) (GH-124755)
Substrings of the specified string no longer considered valid values.
(cherry picked from commit f1a2417b9e
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
11d4b54b88
commit
4a9a359f32
4 changed files with 17 additions and 11 deletions
|
@ -2582,11 +2582,15 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
|
||||
def _check_value(self, action, value):
|
||||
# converted value must be one of the choices (if specified)
|
||||
if action.choices is not None and value not in action.choices:
|
||||
args = {'value': value,
|
||||
'choices': ', '.join(map(repr, action.choices))}
|
||||
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
|
||||
raise ArgumentError(action, msg % args)
|
||||
choices = action.choices
|
||||
if choices is not None:
|
||||
if isinstance(choices, str):
|
||||
choices = iter(choices)
|
||||
if value not in choices:
|
||||
args = {'value': value,
|
||||
'choices': ', '.join(map(repr, action.choices))}
|
||||
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
|
||||
raise ArgumentError(action, msg % args)
|
||||
|
||||
# =======================
|
||||
# Help-formatting methods
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue