# Copyright 2021 Pex project contributors.
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import absolute_import

import sys
from argparse import ArgumentError, ArgumentTypeError

from pex.cli import commands
from pex.cli.command import BuildTimeCommand
from pex.commands.command import GlobalConfigurationError, Main
from pex.result import catch
from pex.typing import TYPE_CHECKING

if TYPE_CHECKING:
    from typing import Optional, Union


class Pex3(Main[BuildTimeCommand]):
    """Tools for building and working with [P]ython [EX]ecutables.

    N.B: This interface is considered unstable until the release of Pex 3.0 at which point it will
    replace the current `pex` command.
    """


def main(subcommand=None):
    # type: (Optional[str]) -> Union[int, str]

    pex3 = Pex3(command_types=commands.all_commands())
    try:
        args = [subcommand] + sys.argv[1:] if subcommand else None
        with pex3.parsed_command(args=args) as command:
            result = catch(command.run)
            result.maybe_display()
            return result.exit_code
    except (ArgumentError, ArgumentTypeError, GlobalConfigurationError) as e:
        return str(e)


def run():
    # type: () -> Union[int, str]
    return main("run")
