def nt_quote_arg(arg):
    """Quote a command line argument according to Windows parsing rules"""
    result = []
    needquote = False
    nb = 0
    needquote = ' ' in arg or '\t' in arg
    if needquote:
        result.append('"')
    for c in arg:
        if c == '\\':
            nb += 1
        elif c == '"':
            result.append('\\' * (nb * 2) + '\\"')
            nb = 0
        else:
            if nb:
                result.append('\\' * nb)
                nb = 0
            result.append(c)
    if nb:
        result.append('\\' * nb)
    if needquote:
        result.append('\\' * nb)
        result.append('"')
    return ''.join(result)
