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

Method consume_optional

lib/0xtools/argparse.py:1819–1889  ·  view source on GitHub ↗
(start_index)

Source from the content-addressed store, hash-verified

1817
1818 # function to convert arg_strings into an optional action
1819 def consume_optional(start_index):
1820
1821 # get the optional identified at this index
1822 option_tuple = option_string_indices[start_index]
1823 action, option_string, explicit_arg = option_tuple
1824
1825 # identify additional optionals in the same arg string
1826 # (e.g. -xyz is the same as -x -y -z if no args are required)
1827 match_argument = self._match_argument
1828 action_tuples = []
1829 while True:
1830
1831 # if we found no optional action, skip it
1832 if action is None:
1833 extras.append(arg_strings[start_index])
1834 return start_index + 1
1835
1836 # if there is an explicit argument, try to match the
1837 # optional's string arguments to only this
1838 if explicit_arg is not None:
1839 arg_count = match_argument(action, 'A')
1840
1841 # if the action is a single-dash option and takes no
1842 # arguments, try to parse more single-dash options out
1843 # of the tail of the option string
1844 chars = self.prefix_chars
1845 if arg_count == 0 and option_string[1] not in chars:
1846 action_tuples.append((action, [], option_string))
1847 char = option_string[0]
1848 option_string = char + explicit_arg[0]
1849 new_explicit_arg = explicit_arg[1:] or None
1850 optionals_map = self._option_string_actions
1851 if option_string in optionals_map:
1852 action = optionals_map[option_string]
1853 explicit_arg = new_explicit_arg
1854 else:
1855 msg = _('ignored explicit argument %r')
1856 raise ArgumentError(action, msg % explicit_arg)
1857
1858 # if the action expect exactly one argument, we've
1859 # successfully matched the option; exit the loop
1860 elif arg_count == 1:
1861 stop = start_index + 1
1862 args = [explicit_arg]
1863 action_tuples.append((action, args, option_string))
1864 break
1865
1866 # error if a double-dash option did not use the
1867 # explicit argument
1868 else:
1869 msg = _('ignored explicit argument %r')
1870 raise ArgumentError(action, msg % explicit_arg)
1871
1872 # if there is no explicit argument, try to match the
1873 # optional's string arguments with the following strings
1874 # if successful, exit the loop
1875 else:
1876 start = start_index + 1

Callers

nothing calls this directly

Calls 1

ArgumentErrorClass · 0.85

Tested by

no test coverage detected