def autocomplete():
<EOL>if '<STR_LIT>' not in os.environ:<EOL><INDENT>return<EOL><DEDENT>cwords = os.environ['<STR_LIT>'].split()[<NUM_LIT:1>:]<EOL>cword = int(os.environ['<STR_LIT>'])<EOL>try:<EOL><INDENT>current = cwords[cword - <NUM_LIT:1>]<EOL><DEDENT>except IndexError:<EOL><INDENT>current = '<STR_LIT>'<EOL><DEDENT>subcommands = [cmd for cmd, summary in get_summaries()]<EOL>options = []<EOL>try:<EOL><INDENT>subcommand_name = [w for w in cwords if w in subcommands][<NUM_LIT:0>]<EOL><DEDENT>except IndexError:<EOL><INDENT>subcommand_name = None<EOL><DEDENT>parser = create_main_parser()<EOL>if subcommand_name:<EOL><INDENT>if subcommand_name == '<STR_LIT>':<EOL><INDENT>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>should_list_installed = (<EOL>subcommand_name in ['<STR_LIT>', '<STR_LIT>'] and<EOL>not current.startswith('<STR_LIT:->')<EOL>)<EOL>if should_list_installed:<EOL><INDENT>installed = []<EOL>lc = current.lower()<EOL>for dist in get_installed_distributions(local_only=True):<EOL><INDENT>if dist.key.startswith(lc) and dist.key not in cwords[<NUM_LIT:1>:]:<EOL><INDENT>installed.append(dist.key)<EOL><DEDENT><DEDENT>if installed:<EOL><INDENT>for dist in installed:<EOL><INDENT>print(dist)<EOL><DEDENT>sys.exit(<NUM_LIT:1>)<EOL><DEDENT><DEDENT>subcommand = commands_dict[subcommand_name]()<EOL>for opt in subcommand.parser.option_list_all:<EOL><INDENT>if opt.help != optparse.SUPPRESS_HELP:<EOL><INDENT>for opt_str in opt._long_opts + opt._short_opts:<EOL><INDENT>options.append((opt_str, opt.nargs))<EOL><DEDENT><DEDENT><DEDENT>prev_opts = [x.split('<STR_LIT:=>')[<NUM_LIT:0>] for x in cwords[<NUM_LIT:1>:cword - <NUM_LIT:1>]]<EOL>options = [(x, v) for (x, v) in options if x not in prev_opts]<EOL>options = [(k, v) for k, v in options if k.startswith(current)]<EOL>completion_type = get_path_completion_type(<EOL>cwords, cword, subcommand.parser.option_list_all,<EOL>)<EOL>if completion_type:<EOL><INDENT>options = auto_complete_paths(current, completion_type)<EOL>options = ((opt, <NUM_LIT:0>) for opt in options)<EOL><DEDENT>for option in options:<EOL><INDENT>opt_label = option[<NUM_LIT:0>]<EOL>if option[<NUM_LIT:1>] and option[<NUM_LIT:0>][:<NUM_LIT:2>] == "<STR_LIT>":<EOL><INDENT>opt_label += '<STR_LIT:=>'<EOL><DEDENT>print(opt_label)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>opts = [i.option_list for i in parser.option_groups]<EOL>opts.append(parser.option_list)<EOL>opts = (o for it in opts for o in it)<EOL>if current.startswith('<STR_LIT:->'):<EOL><INDENT>for opt in opts:<EOL><INDENT>if opt.help != optparse.SUPPRESS_HELP:<EOL><INDENT>subcommands += opt._long_opts + opt._short_opts<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>completion_type = get_path_completion_type(cwords, cword, opts)<EOL>if completion_type:<EOL><INDENT>subcommands = auto_complete_paths(current, completion_type)<EOL><DEDENT><DEDENT>print('<STR_LIT:U+0020>'.join([x for x in subcommands if x.startswith(current)]))<EOL><DEDENT>sys.exit(<NUM_LIT:1>)<EOL>
Entry Point for completion of main and subcommand options.