MCPcopy Index your code
hub / github.com/python/cpython / consume_optional

Method consume_optional

Lib/argparse.py:2203–2300  ·  view source on GitHub ↗
(start_index)

Source from the content-addressed store, hash-verified

2201
2202 # function to convert arg_strings into an optional action
2203 def consume_optional(start_index):
2204
2205 # get the optional identified at this index
2206 option_tuples = option_string_indices[start_index]
2207 # if multiple actions match, the option string was ambiguous
2208 if len(option_tuples) > 1:
2209 options = ', '.join([option_string
2210 for action, option_string, sep, explicit_arg in option_tuples])
2211 args = {'option': arg_strings[start_index], 'matches': options}
2212 msg = _('ambiguous option: %(option)s could match %(matches)s')
2213 raise ArgumentError(None, msg % args)
2214
2215 action, option_string, sep, explicit_arg = option_tuples[0]
2216
2217 # identify additional optionals in the same arg string
2218 # (e.g. -xyz is the same as -x -y -z if no args are required)
2219 match_argument = self._match_argument
2220 action_tuples = []
2221 while True:
2222
2223 # if we found no optional action, skip it
2224 if action is None:
2225 extras.append(arg_strings[start_index])
2226 extras_pattern.append('O')
2227 return start_index + 1
2228
2229 # if there is an explicit argument, try to match the
2230 # optional's string arguments to only this
2231 if explicit_arg is not None:
2232 arg_count = match_argument(action, 'A')
2233
2234 # if the action is a single-dash option and takes no
2235 # arguments, try to parse more single-dash options out
2236 # of the tail of the option string
2237 chars = self.prefix_chars
2238 if (
2239 arg_count == 0
2240 and option_string[1] not in chars
2241 and explicit_arg != ''
2242 ):
2243 if sep or explicit_arg[0] in chars:
2244 msg = _('ignored explicit argument %r')
2245 raise ArgumentError(action, msg % explicit_arg)
2246 action_tuples.append((action, [], option_string))
2247 char = option_string[0]
2248 option_string = char + explicit_arg[0]
2249 optionals_map = self._option_string_actions
2250 if option_string in optionals_map:
2251 action = optionals_map[option_string]
2252 explicit_arg = explicit_arg[1:]
2253 if not explicit_arg:
2254 sep = explicit_arg = None
2255 elif explicit_arg[0] == '=':
2256 sep = '='
2257 explicit_arg = explicit_arg[1:]
2258 else:
2259 sep = ''
2260 else:

Callers

nothing calls this directly

Calls 6

_warningMethod · 0.95
ArgumentErrorClass · 0.70
_Function · 0.50
joinMethod · 0.45
appendMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected