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

Method consume_optional

src/madpack/argparse.py:1820–1890  ·  view source on GitHub ↗
(start_index)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

ArgumentErrorClass · 0.85

Tested by

no test coverage detected