disambiguate a command (expanding, for eg, lis into list) and validate the number of args passed for it

def match(command, options, args):
    """disambiguate a command (expanding, for eg, lis into list) and validate the number of args passed for it"""
    build = ""
    possible = commands
    for l in command:
        build += l
        possible = filter(lambda w: w.startswith(build), possible)
    
    if len(possible) == 0:
        raise ArgError("Command invalid: %s" % command)
    
    if len(possible) > 1:
        raise ArgError("Ambiguous command: %s" % command)

    command = possible.pop()

    if not num_args[command](len(args)):
        raise ArgError("Bad number of args for command %s" % command)

    return command