# Buggy code
def main(args):
    parser, resolver_options_builder = configure_clp()

    try:
        separator = args.index("--")
        args, cmdline = args[:separator], args[separator + 1 :]
    except ValueError:
        args, cmdline = args, []

    options, reqs = parser.parse_args(args=args)
    if options.pex_root:
        ENV.set("PEX_ROOT", options.pex_root)
    else:
        options.pex_root = (
            ENV.PEX_ROOT
        )  # If option not specified fallback to env variable.

    options.cache_dir = make_relative_to_root(options.cache_dir)
    options.interpreter_cache_dir = make_relative_to_root(options.interpreter_cache_dir)

    with ENV.patch(PEX_VERBOSE=str(options.verbosity)):
        with TRACER.timed("Building pex"):
            pex_builder = build_pex(reqs, options, resolver_options_builder)

        if options.pex_name is not None:
            log("Saving PEX file to %s" % options.pex_name, v=options.verbosity)
            tmp_name = options.pex_name + "~"
            safe_delete(tmp_name)
            pex_builder.build(tmp_name)
            os.rename(tmp_name, options.pex_name)
            return 0

        if options.platform != Platform.current():
            log("WARNING: attempting to run PEX with differing platform!")

        pex_builder.freeze()

        log(
            "Running PEX file at %s with args %s" % (pex_builder.path(), cmdline),
            v=options.verbosity,
        )
        pex = PEX(pex_builder.path(), interpreter=pex_builder.interpreter)
        sys.exit(pex.run(args=list(cmdline)))


# Traceback (TypeError)
$ tox -e py27-package
GLOB sdist-make: /Users/billg/ws/git/pex/setup.py
py27-package inst-nodeps: /Users/billg/ws/git/pex/.tox/dist/pex-1.1.6.zip
py27-package installed: funcsigs==1.0.2,mock==2.0.0,pbr==1.9.1,pex==1.1.6,py==1.4.31,pytest==2.9.1,six==1.10.0,twitter.common.contextutil==0.3.4,twitter.common.dirutil==0.3.4,twitter.common.lang==0.3.4,twitter.common.testing==0.3.4
py27-package runtests: PYTHONHASHSEED='374850053'
py27-package runtests: commands[0] | pex --cache-dir /Users/billg/ws/git/pex/.tox/py27-package/tmp/buildcache wheel requests . -o dist/pex27 -e pex.bin.pex:main -v
Traceback (most recent call last):
File ".tox/py27-package/bin/pex", line 11, in <module>
sys.exit(main())
TypeError: main() takes exactly 1 argument (0 given)
ERROR: InvocationError: '/Users/billg/ws/git/pex/.tox/py27-package/bin/pex --cache-dir /Users/billg/ws/git/pex/.tox/py27-package/tmp/buildcache wheel requests . -o dist/pex27 -e pex.bin.pex:main -v'
________________________________________________________________________________________ summary _________________________________________________________________________________________
ERROR:   py27-package: commands failed

# Fixed code
def main(args=None):
    args = args or sys.argv[1:]
    parser, resolver_options_builder = configure_clp()

    try:
        separator = args.index("--")
        args, cmdline = args[:separator], args[separator + 1 :]
    except ValueError:
        args, cmdline = args, []

    options, reqs = parser.parse_args(args=args)
    if options.pex_root:
        ENV.set("PEX_ROOT", options.pex_root)
    else:
        options.pex_root = (
            ENV.PEX_ROOT
        )  # If option not specified fallback to env variable.

    options.cache_dir = make_relative_to_root(options.cache_dir)
    options.interpreter_cache_dir = make_relative_to_root(options.interpreter_cache_dir)

    with ENV.patch(PEX_VERBOSE=str(options.verbosity)):
        with TRACER.timed("Building pex"):
            pex_builder = build_pex(reqs, options, resolver_options_builder)

        if options.pex_name is not None:
            log("Saving PEX file to %s" % options.pex_name, v=options.verbosity)
            tmp_name = options.pex_name + "~"
            safe_delete(tmp_name)
            pex_builder.build(tmp_name)
            os.rename(tmp_name, options.pex_name)
            return 0

        if options.platform != Platform.current():
            log("WARNING: attempting to run PEX with differing platform!")

        pex_builder.freeze()

        log(
            "Running PEX file at %s with args %s" % (pex_builder.path(), cmdline),
            v=options.verbosity,
        )
        pex = PEX(pex_builder.path(), interpreter=pex_builder.interpreter)
        sys.exit(pex.run(args=list(cmdline)))