MCPcopy
hub / github.com/tanelpoder/0xtools / _parse_optional

Method _parse_optional

lib/0xtools/argparse.py:2064–2119  ·  view source on GitHub ↗
(self, arg_string)

Source from the content-addressed store, hash-verified

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