mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[3.13] gh-59317: Improve parsing optional positional arguments in argparse (GH-124303) (GH-124436)
Fix parsing positional argument with nargs equal to '?' or '*' if it is
preceded by an option and another positional argument.
(cherry picked from commit 4a5e4aade4
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
03ae82d0d3
commit
16127de2eb
3 changed files with 102 additions and 42 deletions
|
@ -2252,18 +2252,19 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
def _match_arguments_partial(self, actions, arg_strings_pattern):
|
||||
# progressively shorten the actions list by slicing off the
|
||||
# final actions until we find a match
|
||||
result = []
|
||||
for i in range(len(actions), 0, -1):
|
||||
actions_slice = actions[:i]
|
||||
pattern = ''.join([self._get_nargs_pattern(action)
|
||||
for action in actions_slice])
|
||||
match = _re.match(pattern, arg_strings_pattern)
|
||||
if match is not None:
|
||||
result.extend([len(string) for string in match.groups()])
|
||||
break
|
||||
|
||||
# return the list of arg string counts
|
||||
return result
|
||||
result = [len(string) for string in match.groups()]
|
||||
if (match.end() < len(arg_strings_pattern)
|
||||
and arg_strings_pattern[match.end()] == 'O'):
|
||||
while result and not result[-1]:
|
||||
del result[-1]
|
||||
return result
|
||||
return []
|
||||
|
||||
def _parse_optional(self, arg_string):
|
||||
# if it's an empty string, it was meant to be a positional
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue