| 2119 | |
| 2120 | # function to convert arg_strings into an optional action |
| 2121 | def consume_optional(start_index): |
| 2122 | |
| 2123 | # get the optional identified at this index |
| 2124 | option_tuples = option_string_indices[start_index] |
| 2125 | # if multiple actions match, the option string was ambiguous |
| 2126 | if len(option_tuples) > 1: |
| 2127 | options = ', '.join([option_string |
| 2128 | for action, option_string, sep, explicit_arg in option_tuples]) |
| 2129 | args = {'option': arg_strings[start_index], 'matches': options} |
| 2130 | msg = _('ambiguous option: %(option)s could match %(matches)s') |
| 2131 | raise ArgumentError(None, msg % args) |
| 2132 | |
| 2133 | action, option_string, sep, explicit_arg = option_tuples[0] |
| 2134 | |
| 2135 | # identify additional optionals in the same arg string |
| 2136 | # (e.g. -xyz is the same as -x -y -z if no args are required) |
| 2137 | match_argument = self._match_argument |
| 2138 | action_tuples = [] |
| 2139 | while True: |
| 2140 | |
| 2141 | # if we found no optional action, skip it |
| 2142 | if action is None: |
| 2143 | extras.append(arg_strings[start_index]) |
| 2144 | extras_pattern.append('O') |
| 2145 | return start_index + 1 |
| 2146 | |
| 2147 | # if there is an explicit argument, try to match the |
| 2148 | # optional's string arguments to only this |
| 2149 | if explicit_arg is not None: |
| 2150 | arg_count = match_argument(action, 'A') |
| 2151 | |
| 2152 | # if the action is a single-dash option and takes no |
| 2153 | # arguments, try to parse more single-dash options out |
| 2154 | # of the tail of the option string |
| 2155 | chars = self.prefix_chars |
| 2156 | if ( |
| 2157 | arg_count == 0 |
| 2158 | and option_string[1] not in chars |
| 2159 | and explicit_arg != '' |
| 2160 | ): |
| 2161 | if sep or explicit_arg[0] in chars: |
| 2162 | msg = _('ignored explicit argument %r') |
| 2163 | raise ArgumentError(action, msg % explicit_arg) |
| 2164 | action_tuples.append((action, [], option_string)) |
| 2165 | char = option_string[0] |
| 2166 | option_string = char + explicit_arg[0] |
| 2167 | optionals_map = self._option_string_actions |
| 2168 | if option_string in optionals_map: |
| 2169 | action = optionals_map[option_string] |
| 2170 | explicit_arg = explicit_arg[1:] |
| 2171 | if not explicit_arg: |
| 2172 | sep = explicit_arg = None |
| 2173 | elif explicit_arg[0] == '=': |
| 2174 | sep = '=' |
| 2175 | explicit_arg = explicit_arg[1:] |
| 2176 | else: |
| 2177 | sep = '' |
| 2178 | else: |