<s> """<STR_LIT>""" <EOL> import itertools <EOL> import shlex <EOL> import click <EOL> from prompt_toolkit import completion as completion <EOL> class ShellCompleter ( completion . Completer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , click_root ) : <EOL> self . root = click_root <EOL> def get_completions ( self , document , complete_event ) : <EOL> """<STR_LIT>""" <EOL> return _click_autocomplete ( self . root , document . text_before_cursor ) <EOL> def _click_autocomplete ( root , text ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> parts = shlex . split ( text ) <EOL> except ValueError : <EOL> raise StopIteration <EOL> location , incomplete = _click_resolve_command ( root , parts ) <EOL> if not text . endswith ( '<STR_LIT:U+0020>' ) and not incomplete and text : <EOL> raise StopIteration <EOL> if incomplete and not incomplete [ <NUM_LIT:0> : <NUM_LIT:2> ] . isalnum ( ) : <EOL> for param in location . params : <EOL> if not isinstance ( param , click . Option ) : <EOL> continue <EOL> for opt in itertools . chain ( param . opts , param . secondary_opts ) : <EOL> if opt . startswith ( incomplete ) : <EOL> yield completion . Completion ( opt , - len ( incomplete ) , <EOL> display_meta = param . help ) <EOL> elif isinstance ( location , ( click . MultiCommand , click . core . Group ) ) : <EOL> ctx = click . Context ( location ) <EOL> commands = location . list_commands ( ctx ) <EOL> for command in commands : <EOL> if command . startswith ( incomplete ) : <EOL> cmd = location . get_command ( ctx , command ) <EOL> yield completion . Completion ( command , - len ( incomplete ) , <EOL> display_meta = cmd . short_help ) <EOL> def _click_resolve_command ( root , parts ) : <EOL> """<STR_LIT>""" <EOL> location = root <EOL> incomplete = '<STR_LIT>' <EOL> for part in parts : <EOL> incomplete = part <EOL> if not part [ <NUM_LIT:0> : <NUM_LIT:2> ] . isalnum ( ) : <EOL> continue <EOL> try : <EOL> next_location = location . get_command ( click . Context ( location ) , <EOL> part ) <EOL> if next_location is not None : <EOL> location = next_location <EOL> incomplete = '<STR_LIT>' <EOL> except AttributeError : <EOL> break <EOL> return location , incomplete </s>
