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

Method _parse_optional

Lib/argparse.py:2511–2555  ·  view source on GitHub ↗
(self, arg_string)

Source from the content-addressed store, hash-verified

2509 return []
2510
2511 def _parse_optional(self, arg_string):
2512 # if it's an empty string, it was meant to be a positional
2513 if not arg_string:
2514 return None
2515
2516 # if it doesn't start with a prefix, it was meant to be positional
2517 if arg_string[0] not in self.prefix_chars:
2518 return None
2519
2520 # if the option string is present in the parser, return the action
2521 if arg_string in self._option_string_actions:
2522 action = self._option_string_actions[arg_string]
2523 return [(action, arg_string, None, None)]
2524
2525 # if it's just a single character, it was meant to be positional
2526 if len(arg_string) == 1:
2527 return None
2528
2529 # if the option string before the "=" is present, return the action
2530 option_string, sep, explicit_arg = arg_string.partition('=')
2531 if sep and option_string in self._option_string_actions:
2532 action = self._option_string_actions[option_string]
2533 return [(action, option_string, sep, explicit_arg)]
2534
2535 # search through all possible prefixes of the option string
2536 # and all actions in the parser for possible interpretations
2537 option_tuples = self._get_option_tuples(arg_string)
2538
2539 if option_tuples:
2540 return option_tuples
2541
2542 # if it was not found as an option, but it looks like a negative
2543 # number, it was meant to be positional
2544 # unless there are negative-number-like options
2545 if self._negative_number_matcher.match(arg_string):
2546 if not self._has_negative_number_optionals:
2547 return None
2548
2549 # if it contains a space, it was meant to be a positional
2550 if ' ' in arg_string:
2551 return None
2552
2553 # it was meant to be an optional but there is no such option
2554 # in this parser (though it might be a valid option in a subparser)
2555 return [(None, arg_string, None, None)]
2556
2557 def _get_option_tuples(self, option_string):
2558 result = []

Callers 1

_parse_known_argsMethod · 0.95

Calls 3

_get_option_tuplesMethod · 0.95
partitionMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected