diff --git a/Lib/argparse.py b/Lib/argparse.py --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -2042,6 +2042,18 @@ # return the list of arg string counts return result + def _get_optional_action(self, arg_string): + if arg_string in self._option_string_actions: + return self._option_string_actions[arg_string] + if self._subparsers is not None: + for action in self._subparsers._actions: + if isinstance(action, _SubParsersAction): + for parser in action._name_parser_map.values(): + sub_action = parser._get_optional_action(arg_string) + if sub_action is not None: + return sub_action + return None + def _parse_optional(self, arg_string): # if it's an empty string, it was meant to be a positional if not arg_string: @@ -2052,8 +2064,8 @@ return None # if the option string is present in the parser, return the action - if arg_string in self._option_string_actions: - action = self._option_string_actions[arg_string] + action = self._get_optional_action(arg_string) + if action is not None: return action, arg_string, None # if it's just a single character, it was meant to be positional