def autocomplete(cmdtable, args):
    """Command and option completion.

    Enable by sourcing one of the completion shell scripts (bash or zsh).
    """

    # Don't complete if user hasn't sourced bash_completion file.
    if not os.environ.has_key('ARGUE_AUTO_COMPLETE'):
        return
    cwords = os.environ['COMP_WORDS'].split()[1:]
    cword = int(os.environ['COMP_CWORD'])

    try:
        current = cwords[cword-1]
    except __HOLE__:
        current = ''

    commands = []
    for k in cmdtable.keys():
        commands += aliases_(k)

    # command
    if cword == 1:
        print ' '.join(filter(lambda x: x.startswith(current), commands))

    # command options
    elif cwords[0] in commands:
        options = []
        aliases, (cmd, opts, usage) = findcmd(cwords[0], cmdtable)
        for (short, long, default, help) in opts:
            options.append('-%s'  % short)
            options.append('--%s' % long)

        options = [o for o in options if o.startswith(current)]
        print ' '.join(filter(lambda x: x.startswith(current), options))

    sys.exit(1)
dataset/ETHPy150Open kennethreitz-archive/argue/argue/core.py/autocomplete