MCPcopy Create free account
hub / github.com/apache/madlib / _parse_optional

Method _parse_optional

src/madpack/argparse.py:2054–2109  ·  view source on GitHub ↗
(self, arg_string)

Source from the content-addressed store, hash-verified

2052 return result
2053
2054 def _parse_optional(self, arg_string):
2055 # if it's an empty string, it was meant to be a positional
2056 if not arg_string:
2057 return None
2058
2059 # if it doesn't start with a prefix, it was meant to be positional
2060 if not arg_string[0] in self.prefix_chars:
2061 return None
2062
2063 # if the option string is present in the parser, return the action
2064 if arg_string in self._option_string_actions:
2065 action = self._option_string_actions[arg_string]
2066 return action, arg_string, None
2067
2068 # if it's just a single character, it was meant to be positional
2069 if len(arg_string) == 1:
2070 return None
2071
2072 # if the option string before the "=" is present, return the action
2073 if '=' in arg_string:
2074 option_string, explicit_arg = arg_string.split('=', 1)
2075 if option_string in self._option_string_actions:
2076 action = self._option_string_actions[option_string]
2077 return action, option_string, explicit_arg
2078
2079 # search through all possible prefixes of the option string
2080 # and all actions in the parser for possible interpretations
2081 option_tuples = self._get_option_tuples(arg_string)
2082
2083 # if multiple actions match, the option string was ambiguous
2084 if len(option_tuples) > 1:
2085 options = ', '.join([option_string
2086 for action, option_string, explicit_arg in option_tuples])
2087 tup = arg_string, options
2088 self.error(_('ambiguous option: %s could match %s') % tup)
2089
2090 # if exactly one action matched, this segmentation is good,
2091 # so return the parsed action
2092 elif len(option_tuples) == 1:
2093 option_tuple, = option_tuples
2094 return option_tuple
2095
2096 # if it was not found as an option, but it looks like a negative
2097 # number, it was meant to be positional
2098 # unless there are negative-number-like options
2099 if self._negative_number_matcher.match(arg_string):
2100 if not self._has_negative_number_optionals:
2101 return None
2102
2103 # if it contains a space, it was meant to be a positional
2104 if ' ' in arg_string:
2105 return None
2106
2107 # it was meant to be an optional but there is no such option
2108 # in this parser (though it might be a valid option in a subparser)
2109 return None, arg_string, None
2110
2111 def _get_option_tuples(self, option_string):

Callers 1

_parse_known_argsMethod · 0.95

Calls 2

_get_option_tuplesMethod · 0.95
errorMethod · 0.95

Tested by

no test coverage detected