import re


def escape_arg(arg):
    """
    escape_arg(arg) --> escaped_arg

    This performs string escaping that can be used with SimpleTokenizer.
    (It isn't sufficient for passing strings to a shell.)
    """
    if arg.find('"') >= 0:
        if arg.find("'") >= 0:
            s = re.sub(r'\\', r'\\\\', arg)
            s = re.sub("'", "\\'", s)
            return "'%s'" % (s,)
        else:
            return "'%s'" % (arg,)
    elif arg.find("'") >= 0:
        return '"%s"' % (arg,)
    else:
        return arg