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

Function _match_abbrev

Lib/optparse.py:1642–1664  ·  view source on GitHub ↗

_match_abbrev(s : string, wordmap : {string : Option}) -> string Return the string key in 'wordmap' for which 's' is an unambiguous abbreviation. If 's' is found to be ambiguous or doesn't match any of 'words', raise BadOptionError.

(s, wordmap)

Source from the content-addressed store, hash-verified

1640
1641
1642def _match_abbrev(s, wordmap):
1643 """_match_abbrev(s : string, wordmap : {string : Option}) -> string
1644
1645 Return the string key in 'wordmap' for which 's' is an unambiguous
1646 abbreviation. If 's' is found to be ambiguous or doesn't match any of
1647 'words', raise BadOptionError.
1648 """
1649 # Is there an exact match?
1650 if s in wordmap:
1651 return s
1652 else:
1653 # Isolate all words with s as a prefix.
1654 possibilities = [word for word in wordmap.keys()
1655 if word.startswith(s)]
1656 # No exact match, so there had better be just one possibility.
1657 if len(possibilities) == 1:
1658 return possibilities[0]
1659 elif not possibilities:
1660 raise BadOptionError(s)
1661 else:
1662 # More than one possible completion: ambiguous prefix.
1663 possibilities.sort()
1664 raise AmbiguousOptionError(s, possibilities)
1665
1666
1667# Some day, there might be many Option classes. As of Optik 1.3, the

Callers 2

test_match_abbrevMethod · 0.90
_match_long_optMethod · 0.85

Calls 5

BadOptionErrorClass · 0.85
keysMethod · 0.45
startswithMethod · 0.45
sortMethod · 0.45

Tested by 1

test_match_abbrevMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…