# PYTHON_ARGCOMPLETE_OK

"""The command line interface to pipx"""

from __future__ import annotations

import argparse
import contextlib
import logging
import logging.config
import os
import re
import shlex
import sys
import textwrap
import time
import typing
import urllib.parse
from collections.abc import Callable, Sequence
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Any, Final, NoReturn, cast

import argcomplete
import platformdirs
from packaging.utils import canonicalize_name

from pipx import commands, constants, paths
from pipx.animate import hide_cursor, show_cursor
from pipx.backends import KNOWN_BACKENDS, UV, env_default_backend, get_backend, resolve_backend_name
from pipx.colors import bold, green
from pipx.commands.environment import ENVIRONMENT_VALUE_CHOICES, ENVIRONMENT_VARIABLES
from pipx.constants import (
    _FETCH_MISSING_PYTHON_RAW,
    _FETCH_PYTHON,
    _FETCH_PYTHON_INVALID,
    _FETCH_PYTHON_RAW,
    EXIT_CODE_OK,
    EXIT_CODE_SPECIFIED_PYTHON_EXECUTABLE_NOT_FOUND,
    MINIMUM_PYTHON_VERSION,
    WINDOWS,
    ExitCode,
    FetchPythonOptions,
)
from pipx.emojis import hazard
from pipx.interpreter import (
    InterpreterResolutionError,
    find_python_interpreter,
    get_default_python,
    get_default_python_spec,
)
from pipx.package_specifier import valid_pypi_name
from pipx.result import OperationError, OperationResult, OutputFormat, error_envelope, render_result
from pipx.shared_libs import skip_shared_libs_maintenance
from pipx.util import PipxError, mkdir, pipx_wrap, rmdir
from pipx.venv import VenvContainer
from pipx.version import version as __version__

if TYPE_CHECKING:
    from pipx.commands.upgrade import UpgradeData

logger = logging.getLogger(__name__)

VenvCompleter = Callable[[str], list[str]]


class _CompletableAction(typing.Protocol):
    completer: VenvCompleter


def print_version() -> None:
    print(__version__)  # ruff:ignore[print]  # user-facing CLI output


def prog_name() -> str:
    with contextlib.suppress(IndexError):
        prog = Path(sys.argv[0]).name
        return f"{sys.executable} -m pipx" if prog == "__main__.py" else prog
    return "pipx"


SPEC_HELP = textwrap.dedent(
    """\
    The package name or specific installation source passed to pip.
    Runs `pip install -U SPEC`.
    For example `--spec mypackage==2.0.0` or `--spec  git+https://github.com/user/repo.git@branch`
    """
)

PIPX_DESCRIPTION = textwrap.dedent(
    f"""
    Install and execute apps from Python packages.

    Binaries can either be installed globally into isolated Virtual Environments
    or run directly in a temporary Virtual Environment.

    Virtual Environment location is {paths.ctx.venvs!s}.
    Symlinks to apps are placed in {paths.ctx.bin_dir!s}.
    Symlinks to manual pages are placed in {paths.ctx.man_dir!s}.

    """
)
PIPX_DESCRIPTION += pipx_wrap(
    "optional environment variables:\n"
    "  PIPX_HOME              Overrides default pipx location. "
    "Virtual Environments will be installed to $PIPX_HOME/venvs.\n"
    "  PIPX_GLOBAL_HOME       Used instead of PIPX_HOME when the `--global` option is given.\n"
    "  PIPX_BIN_DIR           Overrides location of app installations. Apps are symlinked or copied here.\n"
    "  PIPX_GLOBAL_BIN_DIR    Used instead of PIPX_BIN_DIR when the `--global` option is given.\n"
    "  PIPX_MAN_DIR           Overrides location of manual pages installations. "
    "Manual pages are symlinked or copied here.\n"
    "  PIPX_GLOBAL_MAN_DIR    Used instead of PIPX_MAN_DIR when the `--global` option is given.\n"
    "  PIPX_COMPLETION_DIR    Overrides location of shell completion installations. "
    "Completion scripts are symlinked or copied here.\n"
    "  PIPX_GLOBAL_COMPLETION_DIR\n"
    "                        Used instead of PIPX_COMPLETION_DIR when the `--global` option is given.\n"
    "  PIPX_DEFAULT_PYTHON    Overrides default python used for commands.\n"
    "  PIPX_DEFAULT_BACKEND   Overrides which backend (`pip` or `uv`) is used for new venvs.\n"
    "  PIPX_DISABLE_SHARED_LIBS_AUTO_UPGRADE\n"
    "                        Skips automatic shared library upgrades.\n"
    "  PIPX_USE_EMOJI         Overrides emoji behavior. Default value varies based on platform.",
    subsequent_indent=" " * 24,  # match the indent of argparse options
    keep_newlines=True,
)

DOC_DEFAULT_PYTHON = os.getenv("PIPX__DOC_DEFAULT_PYTHON", get_default_python_spec())

INSTALL_DESCRIPTION = textwrap.dedent(
    f"""
    The install command is the preferred way to globally install apps
    from Python packages and PEP 723 scripts. It creates an isolated virtual
    environment for the source, then ensures its apps are
    accessible on your $PATH. The package's manual pages installed in
    share/man/man[1-9] can be viewed with man on an operating system where
    it is available and the path in the environment variable `PIPX_MAN_DIR`
    (default: {paths.DEFAULT_PIPX_MAN_DIR}) is in the man search path
    ($MANPATH).

    The result: apps you can run from anywhere, located in packages
    you can cleanly upgrade or uninstall. Guaranteed to not have
    dependency version conflicts or interfere with your OS's python
    packages. 'sudo' is not required to do this.

    pipx install PACKAGE_SPEC ...
    pipx install --python PYTHON PACKAGE_SPEC
    pipx install VCS_URL
    pipx install ./LOCAL_PATH
    pipx install ./SCRIPT.py
    pipx install ZIP_FILE
    pipx install TAR_GZ_FILE

    Package specs are passed to the selected backend. PEP 723 scripts are
    installed with their declared dependencies.

    Virtual Environments will be installed to `$PIPX_HOME/venvs`.
    The default pipx home location is {paths.DEFAULT_PIPX_HOME} and can
    be overridden by setting the environment variable `PIPX_HOME`.
    If the `--global` option is used, the default pipx home location
    instead is {paths.DEFAULT_PIPX_GLOBAL_HOME} and can be overridden
    by setting the environment variable `PIPX_GLOBAL_HOME`.

    The default app location is {paths.DEFAULT_PIPX_BIN_DIR} and can be
    overridden by setting the environment variable `PIPX_BIN_DIR`.
    If the `--global` option is used, the default app location instead
    is {paths.DEFAULT_PIPX_GLOBAL_BIN_DIR} and can be overridden by
    setting the environment variable `PIPX_GLOBAL_BIN_DIR`.

    The default manual pages location is {paths.DEFAULT_PIPX_MAN_DIR} and
    can be overridden by setting the environment variable `PIPX_MAN_DIR`.
    If the `--global` option is used, the default manual pages location
    instead is {paths.DEFAULT_PIPX_GLOBAL_MAN_DIR} and can be overridden
    by setting the environment variable `PIPX_GLOBAL_MAN_DIR`.

    The default python executable used to install a package is
    {DOC_DEFAULT_PYTHON} and can be overridden
    by setting the environment variable `PIPX_DEFAULT_PYTHON`.
    """
)


class LineWrapRawTextHelpFormatter(argparse.RawDescriptionHelpFormatter):
    def _split_lines(self, text: str, width: int) -> list[str]:
        text = self._whitespace_matcher.sub(" ", text).strip()
        return textwrap.wrap(text, width)


class InstalledVenvsCompleter:
    def __init__(self, venv_container: VenvContainer) -> None:
        self._venv_container: Final[VenvContainer] = venv_container
        self._packages: list[str] | None = None

    def use(self, prefix: str, **_kwargs: argparse.Action | argparse.ArgumentParser | argparse.Namespace) -> list[str]:
        if self._packages is None:
            self._packages = [path.name for path in sorted(self._venv_container.iter_venv_dirs())]
        canonical_prefix = canonicalize_name(prefix)
        return [
            f"{prefix}{name[len(canonical_prefix) :]}" for name in self._packages if name.startswith(canonical_prefix)
        ]


def get_pip_args(parsed_args: dict[str, str]) -> list[str]:
    pip_args: list[str] = []
    if parsed_args.get("index_url"):
        pip_args += ["--index-url", parsed_args["index_url"]]

    if parsed_args.get("pip_args"):
        # Stripping the single quote that can be parsed from several shells
        pip_args_striped = parsed_args["pip_args"].strip("'")
        pip_args += shlex.split(pip_args_striped, posix=not WINDOWS)

    # make sure --editable is last because it needs to be right before
    #   package specification
    if parsed_args.get("editable"):
        pip_args += ["--editable"]
    return pip_args


def _non_negative_int(value: str) -> int:
    msg = "--cooldown must be a non-negative integer"
    try:
        result: Final[int] = int(value)
    except ValueError as exc:
        raise argparse.ArgumentTypeError(msg) from exc
    if result < 0:
        raise argparse.ArgumentTypeError(msg)
    return result


def get_runpip_args(pip_args: list[str]) -> list[str]:
    if len(pip_args) != 1:
        return pip_args

    candidate = pip_args[0]
    # Allow a single quoted string like "install black".
    if not any(char.isspace() for char in candidate):
        return pip_args

    split_args = shlex.split(candidate, posix=not WINDOWS)
    return split_args or pip_args


def _split_python_args(python_args: str) -> list[str]:
    try:
        return shlex.split(python_args, posix=not WINDOWS)
    except ValueError as exc:
        msg = f"invalid Python arguments: {exc}"
        raise argparse.ArgumentTypeError(msg) from exc


def get_venv_args(parsed_args: dict[str, str]) -> list[str]:
    venv_args: list[str] = []
    if parsed_args.get("system_site_packages"):
        venv_args += ["--system-site-packages"]
    return venv_args


def get_backend_arg(parsed_args: dict[str, str]) -> str | None:
    return parsed_args.get("backend") or None


def package_is_url(package: str, *, raise_error: bool = True) -> bool:
    url_parse_package = urllib.parse.urlparse(package)
    if url_parse_package.scheme and url_parse_package.netloc:
        if not raise_error:
            return True
        msg = "Package cannot be a URL. A valid package name should be passed instead."
        raise PipxError(msg)
    return False


def package_is_path(package: str) -> None:
    if any(separator in package for separator in (os.path.sep, os.path.altsep) if separator):
        raise PipxError(
            pipx_wrap(
                f"""
                Error: '{package}' looks like a path.
                Expected the name of an installed package.
                """
            )
        )


def _validate_fetch_python() -> None:
    if _FETCH_PYTHON_INVALID:
        valid = ", ".join(str(option) for option in FetchPythonOptions)
        msg = f"PIPX_FETCH_PYTHON must be unset or one of: {valid}."
        raise PipxError(msg)
    if _FETCH_MISSING_PYTHON_RAW is not None:
        if _FETCH_PYTHON_RAW is not None:
            msg = "Setting both PIPX_FETCH_MISSING_PYTHON and PIPX_FETCH_PYTHON is invalid."
            raise PipxError(msg)
        print(  # ruff:ignore[print]  # user-facing CLI output
            f"{hazard} PIPX_FETCH_MISSING_PYTHON is deprecated; "
            f'use PIPX_FETCH_PYTHON="{FetchPythonOptions.MISSING}" instead.',
            file=sys.stderr,
        )


def run_pipx_command(args: argparse.Namespace) -> ExitCode:
    if "package" in args:
        package_is_url(args.package)
        package_is_path(args.package)
    if "packages" in args:
        for package in args.packages:
            package_is_url(package)
            package_is_path(package)

    spec: str | None = getattr(args, "spec", None)
    if "package" in args and spec is not None and package_is_url(spec, raise_error=False) and "#egg=" not in spec:
        spec = f"{spec}#egg={args.package}"

    python = get_default_python()
    python_flag_passed = False
    if "python" in args:
        python_flag_passed = bool(args.python)
        try:
            python = find_python_interpreter(args.python or get_default_python(), fetch_python=args.fetch_python)
        except InterpreterResolutionError as e:
            logger.debug("Failed to resolve interpreter:", exc_info=True)
            print(pipx_wrap(f"{hazard} {e}", subsequent_indent=" " * 4))  # ruff:ignore[print]  # user-facing CLI output
            return EXIT_CODE_SPECIFIED_PYTHON_EXECUTABLE_NOT_FOUND

    cli_backend = get_backend_arg(vars(args))
    env_backend = env_default_backend()
    _validate_backend_available(cli_backend, env_backend)

    ctx = DispatchContext(
        fetch_python=args.fetch_python if "fetch_python" in args else FetchPythonOptions.NEVER,
        verbose=bool(args.verbose) if "verbose" in args else False,
        pip_args=get_pip_args(vars(args)),
        venv_args=get_venv_args(vars(args)),
        venv_container=VenvContainer(paths.ctx.venvs),
        skip_list=[canonicalize_name(x) for x in args.skip] if "skip" in args else [],
        python=python,
        python_flag_passed=python_flag_passed,
        spec=spec,
        backend=cli_backend,
        env_backend=env_backend,
        cooldown_days=getattr(args, "cooldown", None),
    )
    output: Final[OutputFormat] = _output_format(args)
    with skip_shared_libs_maintenance(enabled=getattr(args, "skip_maintenance", False)):
        try:
            result = args.func(args, ctx)
        except PipxError as error:
            # once a JSON-enabled command was selected, a dispatch failure must still speak the result envelope
            if output is OutputFormat.JSON:
                print(  # ruff:ignore[print]  # user-facing CLI output
                    error_envelope(
                        _command_tokens(args), OperationError(code="pipx_error", message=str(error)), ExitCode(1)
                    )
                )
                return ExitCode(1)
            raise
        if isinstance(result, OperationResult):
            return render_result(result, output=output, quiet=getattr(args, "quiet", 0))
        return result


def _command_tokens(args: argparse.Namespace) -> tuple[str, ...]:
    tokens: Final[list[str]] = [args.command]
    for subcommand in ("cache_command", "interpreter_command", "manifest_command"):
        if value := getattr(args, subcommand, None):
            tokens.append(value)
    return tuple(tokens)


def _output_format(args: argparse.Namespace) -> OutputFormat:
    return OutputFormat(getattr(args, "output", OutputFormat.HUMAN))


def _validate_backend_available(cli_backend: str | None, env_backend: str | None) -> None:
    """Eagerly surface a missing uv when ``--backend uv`` was passed.

    Env-source and auto-detect stay silent so diagnostic commands (which never
    instantiate a backend) keep working, and so unrelated PATH ``uv`` doesn't
    pay a ``uv --version`` subprocess on every command.
    """
    name, source = resolve_backend_name(cli_value=cli_backend, env_value=env_backend)
    logger.info("Backend resolved to %s (source: %s)", name, source)
    if name == UV and source == "cli":
        get_backend(name)


@dataclass(frozen=True)
class DispatchContext:
    verbose: bool
    pip_args: list[str]
    venv_args: list[str]
    venv_container: VenvContainer
    skip_list: list[str]
    python: str
    python_flag_passed: bool
    spec: str | None
    backend: str | None = None
    env_backend: str | None = None
    cooldown_days: int | None = None
    fetch_python: FetchPythonOptions = FetchPythonOptions.NEVER

    @property
    def effective_backend(self) -> str | None:
        # Use ``backend`` and ``env_backend`` separately when building a
        # :class:`pipx.venv.Venv`; this only collapses them for code paths
        # without metadata to consult.
        return self.backend or self.env_backend


def add_pip_venv_args(parser: argparse.ArgumentParser) -> None:
    parser.add_argument(
        "--system-site-packages",
        action="store_true",
        help="Give the virtual environment access to the system site-packages dir.",
    )
    parser.add_argument("--index-url", "-i", help="Base URL of Python Package Index")
    parser.add_argument(
        "--editable",
        "-e",
        help="Install a project in editable mode",
        action="store_true",
    )
    parser.add_argument(
        "--pip-args",
        help=(
            "Arbitrary pip arguments to pass directly to pip install/upgrade commands. "
            "Under the uv backend a small subset is translated (--index-url, --extra-index-url, "
            "--find-links, --pre); other flags raise an error so behaviour stays explicit."
        ),
    )
    parser.add_argument(
        "--cooldown",
        type=_non_negative_int,
        metavar="DAYS",
        help="Ignore index artifacts uploaded fewer than DAYS days ago.",
    )


def add_backend_arg(parser: argparse.ArgumentParser) -> None:
    parser.add_argument(
        "--backend",
        choices=list(KNOWN_BACKENDS),
        default=None,
        help=(
            "Backend used for creating venvs and managing packages. Defaults to "
            "PIPX_DEFAULT_BACKEND when set, else 'uv' when uv is available "
            "(via the `pipx[uv]` extra or on PATH), else 'pip'. Existing venvs "
            "always honor their recorded backend unless this flag is given."
        ),
    )


def _add_dependency_app_options(parser: argparse.ArgumentParser) -> None:
    group: Final[argparse._MutuallyExclusiveGroup] = parser.add_mutually_exclusive_group()
    group.add_argument(
        "--include-deps",
        help="Include apps and manual pages from all dependencies",
        action="store_true",
    )
    group.add_argument(
        "--include-resources-from",
        action="append",
        default=[],
        metavar="PACKAGE",
        help="Include apps and manual pages from this dependency. Repeat for multiple dependencies",
    )


def _add_output_option(parser: argparse.ArgumentParser, *, snapshot_json: bool = False) -> None:
    group: Final[argparse._MutuallyExclusiveGroup] = parser.add_mutually_exclusive_group()
    group.add_argument(
        "--output",
        choices=[output.value for output in OutputFormat],
        default=OutputFormat.HUMAN,
        help="Select the output format.",
    )
    # only ``list`` keeps a ``--json`` flag: it prints the versioned package snapshot that ``install-all`` reads back,
    # which predates and differs from the ``--output json`` result envelope
    if snapshot_json:
        group.add_argument(
            "--json",
            action="store_const",
            const=OutputFormat.JSON,
            dest="output",
            help="Print the installed-package snapshot as JSON, the format install-all consumes.",
        )


class _DeprecatedFetchMissingPython(argparse.Action):
    def __init__(self, option_strings: list[str], dest: str, **kwargs: Any) -> None:  # ruff:ignore[any-type]  # argparse kwargs
        super().__init__(option_strings, dest, nargs=0, **kwargs)

    def __call__(
        self,
        parser: argparse.ArgumentParser,
        namespace: argparse.Namespace,
        values: str | Sequence[str] | None,
        option_string: str | None = None,
    ) -> None:
        del parser, values
        if not {"--help", "-h"}.intersection(sys.argv):
            print(  # ruff:ignore[print]  # user-facing CLI output
                f"{hazard} {option_string} is deprecated; use --fetch-python={FetchPythonOptions.MISSING} instead.",
                file=sys.stderr,
            )
        setattr(namespace, self.dest, FetchPythonOptions.MISSING)


def add_python_options(parser: argparse.ArgumentParser) -> None:
    parser.add_argument(
        "--python",
        help=(
            "Python to install with. Possible values can be the executable name (python3.11), "
            "the version of an available system Python or to pass to py launcher (3.11), "
            f"or the full path to the executable. Requires Python {MINIMUM_PYTHON_VERSION} or above."
        ),
    )
    fetch_python_group = parser.add_mutually_exclusive_group()
    fetch_python_group.add_argument(
        "--fetch-python",
        type=FetchPythonOptions,
        choices=list(FetchPythonOptions),
        default=_FETCH_PYTHON,
        help=(
            f"When to fetch a standalone Python build from python-build-standalone. "
            f"'{FetchPythonOptions.ALWAYS}' always downloads (ignores any system Python); "
            f"'{FetchPythonOptions.MISSING}' downloads only when the requested version is not found locally; "
            f"'{FetchPythonOptions.NEVER}' never downloads. "
            "Defaults to PIPX_FETCH_PYTHON, or 'never'."
        ),
    )
    fetch_python_group.add_argument(
        "--fetch-missing-python",
        dest="fetch_python",
        action=_DeprecatedFetchMissingPython,
        help="Deprecated: alias for --fetch-python=missing.",
    )


def _add_install(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    p = subparsers.add_parser(
        "install",
        help="Install a package",
        formatter_class=LineWrapRawTextHelpFormatter,
        description=INSTALL_DESCRIPTION,
        parents=[shared_parser],
    )
    p.add_argument("package_spec", help="package name(s), PEP 723 script(s), or installation spec(s)", nargs="+")
    _add_dependency_app_options(p)
    p.add_argument(
        "--force",
        "-f",
        action="store_true",
        help="Modify existing virtual environment and files in PIPX_BIN_DIR and PIPX_MAN_DIR",
    )
    p.add_argument(
        "--upgrade",
        "-U",
        action="store_true",
        help="Upgrade or downgrade an existing package when its version does not satisfy the supplied spec",
    )
    p.add_argument(
        "--upgrade-strategy",
        choices=["only-if-needed", "eager"],
        help="How dependency upgrades are handled when --upgrade changes an existing package",
    )
    p.add_argument(
        "--suffix",
        default="",
        help="Optional suffix for virtual environment and executable names.",
    )
    add_python_options(p)
    p.add_argument(
        "--preinstall",
        action="append",
        help=(
            "Optional package to be installed into the Virtual Environment before "
            "installing the main package. Use this flag multiple times if you want to preinstall multiple packages."
        ),
    )
    p.add_argument(
        "--app",
        action="append",
        help="Require this application entry point after installation. Repeat to require multiple entry points.",
    )
    p.add_argument(
        "--lock",
        type=Path,
        help="Install the environment from an explicit pylock.toml file.",
    )
    add_pip_venv_args(p)
    add_backend_arg(p)
    _add_output_option(p)
    p.set_defaults(func=_cmd_install)


def _cmd_install(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.InstallData]:
    return commands.install(
        None,
        None,
        args.package_spec,
        paths.ctx.bin_dir,
        paths.ctx.man_dir,
        ctx.python,
        ctx.pip_args,
        ctx.venv_args,
        verbose=ctx.verbose,
        force=args.force,
        upgrade=args.upgrade,
        reinstall=False,
        include_dependencies=args.include_deps,
        include_resources_from=args.include_resources_from,
        preinstall_packages=args.preinstall,
        expected_apps=args.app or (),
        lock_file=args.lock,
        suffix=args.suffix,
        python_flag_passed=ctx.python_flag_passed,
        fetch_python=ctx.fetch_python,
        backend=ctx.backend,
        env_backend=ctx.env_backend,
        upgrade_strategy=args.upgrade_strategy,
        cooldown_days=ctx.cooldown_days,
        emit_output=False,
    )


def _add_install_all(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    p = subparsers.add_parser(
        "install-all",
        help="Install all packages",
        formatter_class=LineWrapRawTextHelpFormatter,
        description="Installs all the packages according to spec metadata file.",
        parents=[shared_parser],
    )
    p.add_argument("spec_metadata_file", help="Spec metadata file generated from pipx list --output json")
    p.add_argument(
        "--force",
        "-f",
        action="store_true",
        help="Modify existing virtual environment and files in PIPX_BIN_DIR and PIPX_MAN_DIR",
    )
    add_python_options(p)
    add_pip_venv_args(p)
    add_backend_arg(p)
    p.set_defaults(func=_cmd_install_all)


def _cmd_install_all(args: argparse.Namespace, ctx: DispatchContext) -> ExitCode:
    return commands.install_all(
        args.spec_metadata_file,
        paths.ctx.bin_dir,
        paths.ctx.man_dir,
        ctx.python,
        ctx.pip_args,
        ctx.venv_args,
        verbose=ctx.verbose,
        force=args.force,
        backend=ctx.backend,
        env_backend=ctx.env_backend,
        cooldown_days=ctx.cooldown_days,
    )


def _add_manifest(
    subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser
) -> argparse.ArgumentParser:
    parser: Final[argparse.ArgumentParser] = subparsers.add_parser(
        "manifest",
        help="Manage tools declared in an explicit manifest",
        description="Manage tools declared in an explicit manifest",
        parents=[shared_parser],
    )
    subcommands: Final[argparse._SubParsersAction] = parser.add_subparsers(
        title="subcommands",
        description="Get help for commands with pipx manifest COMMAND --help",
        dest="manifest_command",
    )
    lock_parser: Final[argparse.ArgumentParser] = subcommands.add_parser(
        "lock",
        help="Resolve a tool manifest with nab",
        description="Resolve each locked tool in an explicit manifest into a separate PEP 751 lock file.",
        parents=[shared_parser],
    )
    lock_parser.add_argument("manifest", type=Path, help="Path to the tool manifest.")
    _add_output_option(lock_parser)
    lock_parser.set_defaults(func=_cmd_lock)
    sync_parser: Final[argparse.ArgumentParser] = subcommands.add_parser(
        "sync",
        help="Apply a tool manifest",
        description="Install, upgrade, or downgrade the tools declared in an explicit manifest.",
        parents=[shared_parser],
    )
    sync_parser.add_argument("manifest", type=Path, help="Path to the tool manifest.")
    sync_parser.add_argument("--prune", action="store_true", help="Uninstall environments absent from the manifest.")
    add_backend_arg(sync_parser)
    _add_output_option(sync_parser)
    sync_parser.set_defaults(func=_cmd_sync)
    parser.set_defaults(func=_make_print_help(parser))
    return parser


def _cmd_lock(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.ManifestData]:
    del ctx
    return commands.lock_manifest(args.manifest)


def _cmd_sync(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.ManifestData]:
    return commands.sync_manifest(
        args.manifest,
        ctx.venv_container,
        paths.ctx.bin_dir,
        paths.ctx.man_dir,
        ctx.python,
        verbose=ctx.verbose,
        prune=args.prune,
        backend=ctx.backend,
        env_backend=ctx.env_backend,
    )


def _add_inject(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    p = subparsers.add_parser(
        "inject",
        help="Install packages into an existing Virtual Environment",
        description="Installs packages to an existing pipx-managed virtual environment.",
        parents=[shared_parser],
    )
    p.add_argument(
        "package",
        metavar="ENVIRONMENT",
        help="Name of the existing pipx-managed environment to inject into",
    ).completer = venv_completer
    p.add_argument(
        "dependencies",
        nargs="*",
        help="the packages to inject into the environment--either package name or pip package spec",
    )
    p.add_argument(
        "-r",
        "--requirement",
        dest="requirements",
        action="append",
        default=[],
        metavar="file",
        help=(
            "file containing the packages to inject into the Virtual Environment--"
            "one package name or pip package spec per line. "
            "May be specified multiple times."
        ),
    )
    p.add_argument(
        "--include-apps",
        action="store_true",
        help="Add apps from the injected packages onto your PATH and expose their manual pages",
    )
    _add_dependency_app_options(p)
    add_pip_venv_args(p)
    p.add_argument(
        "--force",
        "-f",
        action="store_true",
        help="Modify existing virtual environment and files in PIPX_BIN_DIR and PIPX_MAN_DIR",
    )
    p.add_argument(
        "--with-suffix",
        action="store_true",
        help="Add the suffix (if given) of the Virtual Environment to the packages to inject",
    )
    add_backend_arg(p)
    _add_output_option(p)
    p.set_defaults(func=_cmd_inject)


def _cmd_inject(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.InjectionData]:
    venv_dir = _venv_dir(args, ctx)
    with ctx.venv_container.venv_lock(venv_dir):
        return commands.inject(
            venv_dir,
            args.dependencies,
            args.requirements,
            ctx.pip_args,
            verbose=ctx.verbose,
            include_apps=args.include_apps,
            include_dependencies=args.include_deps,
            include_resources_from=args.include_resources_from,
            force=args.force,
            suffix=args.with_suffix,
            backend=ctx.backend,
            env_backend=ctx.env_backend,
            cooldown_days=ctx.cooldown_days,
            emit_output=False,
        )


def _add_uninject(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    p = subparsers.add_parser(
        "uninject",
        help="Uninstall injected packages from an existing Virtual Environment",
        description="Uninstalls injected packages from an existing pipx-managed virtual environment.",
        parents=[shared_parser],
    )
    p.add_argument(
        "package",
        metavar="ENVIRONMENT",
        help="Name of the existing pipx-managed environment to uninject from",
    ).completer = venv_completer
    p.add_argument(
        "dependencies",
        nargs="+",
        help="the package names to uninject from the environment",
    )
    p.add_argument(
        "--leave-deps",
        action="store_true",
        help="Only uninstall the main injected package but leave its dependencies installed.",
    )
    _add_output_option(p)
    p.set_defaults(func=_cmd_uninject)


def _cmd_uninject(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.InjectionData]:
    venv_dir = _venv_dir(args, ctx)
    with ctx.venv_container.venv_lock(venv_dir):
        return commands.uninject(
            venv_dir,
            args.dependencies,
            local_bin_dir=paths.ctx.bin_dir,
            local_man_dir=paths.ctx.man_dir,
            leave_deps=args.leave_deps,
            verbose=ctx.verbose,
        )


def _add_expose(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    parser = subparsers.add_parser(
        "expose",
        help="Restore resources from a hidden environment",
        description="Relink each recorded app and manual page without rebuilding its environment.",
        parents=[shared_parser],
    )
    parser.add_argument("package", metavar="ENVIRONMENT", help="Environment to expose").completer = venv_completer
    _add_output_option(parser)
    parser.set_defaults(func=_cmd_expose)


def _cmd_expose(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.ExposureData]:
    venv_dir = _venv_dir(args, ctx)
    with ctx.venv_container.venv_lock(venv_dir):
        return commands.expose(venv_dir, paths.ctx.bin_dir, paths.ctx.man_dir, verbose=ctx.verbose)


def _add_unexpose(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    parser = subparsers.add_parser(
        "unexpose",
        help="Hide resources without removing an environment",
        description="Remove global links or copies while retaining the managed environment.",
        parents=[shared_parser],
    )
    parser.add_argument("package", metavar="ENVIRONMENT", help="Environment to hide").completer = venv_completer
    _add_output_option(parser)
    parser.set_defaults(func=_cmd_unexpose)


def _cmd_unexpose(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.ExposureData]:
    venv_dir = _venv_dir(args, ctx)
    with ctx.venv_container.venv_lock(venv_dir):
        return commands.unexpose(venv_dir, paths.ctx.bin_dir, paths.ctx.man_dir, verbose=ctx.verbose)


def _add_pin(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    p = subparsers.add_parser(
        "pin",
        help="Pin the specified package to prevent it from being upgraded",
        description="Pin the specified package to prevent it from being upgraded",
        parents=[shared_parser],
    )
    p.add_argument("package", metavar="ENVIRONMENT", help="Environment to pin")
    p.add_argument(
        "--injected-only",
        action="store_true",
        help=(
            "Pin injected packages in venv only, so that they will not be upgraded during upgrade operations. "
            "Note that this should not be passed if you wish to pin both main package and injected packages."
        ),
    )
    p.add_argument(
        "--skip",
        nargs="+",
        default=[],
        help="Skip these packages. Implies `--injected-only`.",
    )
    _add_output_option(p)
    p.set_defaults(func=_cmd_pin)


def _cmd_pin(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.PinData]:
    venv_dir = _venv_dir(args, ctx)
    with ctx.venv_container.venv_lock(venv_dir):
        return commands.pin(venv_dir, ctx.skip_list, verbose=ctx.verbose, injected_only=args.injected_only)


def _add_unpin(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    p = subparsers.add_parser(
        "unpin",
        help="Unpin the specified package",
        description="Unpin the specified package and all injected packages in its venv to allow them to be upgraded",
        parents=[shared_parser],
    )
    p.add_argument("package", metavar="ENVIRONMENT", help="Environment to unpin")
    _add_output_option(p)
    p.set_defaults(func=_cmd_unpin)


def _cmd_unpin(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.PinData]:
    venv_dir = _venv_dir(args, ctx)
    with ctx.venv_container.venv_lock(venv_dir):
        return commands.unpin(venv_dir, verbose=ctx.verbose)


def _add_upgrade(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    p = subparsers.add_parser(
        "upgrade",
        help="Upgrade a package",
        description=(
            "Upgrade package(s) in pipx-managed Virtual Environment(s) by running 'pip install --upgrade PACKAGE'"
        ),
        parents=[shared_parser],
    )
    p.add_argument("packages", help="package names(s) to upgrade", nargs="+").completer = venv_completer
    p.add_argument(
        "--include-injected",
        action="store_true",
        help="Also upgrade packages injected into the main app's environment",
    )
    p.add_argument(
        "--force",
        "-f",
        action="store_true",
        help="Modify existing virtual environment and files in PIPX_BIN_DIR and PIPX_MAN_DIR",
    )
    add_pip_venv_args(p)
    p.add_argument(
        "--install",
        action="store_true",
        help="Install package spec if missing",
    )
    add_python_options(p)
    add_backend_arg(p)
    _add_output_option(p)
    p.set_defaults(func=_cmd_upgrade)


def _cmd_upgrade(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[UpgradeData]:
    return commands.upgrade(
        _venv_dirs(args, ctx),
        ctx.python,
        ctx.pip_args,
        ctx.venv_args,
        verbose=ctx.verbose,
        include_injected=args.include_injected,
        force=args.force,
        install=args.install,
        python_flag_passed=ctx.python_flag_passed,
        backend=ctx.backend,
        env_backend=ctx.env_backend,
        cooldown_days=ctx.cooldown_days,
    )


def _add_upgrade_all(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    p = subparsers.add_parser(
        "upgrade-all",
        help="Upgrade all packages. Runs `pip install -U <pkgname>` for each package.",
        description=(
            "Upgrades all packages within their virtual environments by running 'pip install --upgrade PACKAGE'"
        ),
        parents=[shared_parser],
    )
    p.add_argument(
        "--include-injected",
        action="store_true",
        help="Also upgrade packages injected into the main app's environment",
    )
    p.add_argument("--skip", nargs="+", default=[], help="skip these packages")
    p.add_argument(
        "--force",
        "-f",
        action="store_true",
        help="Modify existing virtual environment and files in PIPX_BIN_DIR and PIPX_MAN_DIR",
    )
    add_pip_venv_args(p)
    add_backend_arg(p)
    _add_output_option(p)
    p.set_defaults(func=_cmd_upgrade_all)


def _cmd_upgrade_all(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[UpgradeData]:
    return commands.upgrade_all(
        ctx.venv_container,
        verbose=ctx.verbose,
        include_injected=args.include_injected,
        skip=ctx.skip_list,
        force=args.force,
        pip_args=ctx.pip_args,
        python_flag_passed=ctx.python_flag_passed,
        backend=ctx.backend,
        env_backend=ctx.env_backend,
        cooldown_days=ctx.cooldown_days,
    )


def _add_upgrade_shared(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    p = subparsers.add_parser(
        "upgrade-shared",
        help="Upgrade shared libraries.",
        description="Upgrade shared libraries.",
        parents=[shared_parser],
    )
    p.add_argument(
        "--pip-args",
        help="Arbitrary pip arguments to pass directly to pip install/upgrade commands",
    )
    _add_output_option(p)
    p.set_defaults(func=_cmd_upgrade_shared)


def _cmd_upgrade_shared(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.SharedData]:
    del args
    return commands.upgrade_shared(ctx.pip_args, verbose=ctx.verbose)


def _add_uninstall(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    p = subparsers.add_parser(
        "uninstall",
        help="Uninstall a package",
        description=(
            "Uninstalls a pipx-managed Virtual Environment by deleting it and any files that point to its apps."
        ),
        parents=[shared_parser],
    )
    p.add_argument("package", metavar="ENVIRONMENT").completer = venv_completer
    _add_output_option(p)
    p.set_defaults(func=_cmd_uninstall)


def _cmd_uninstall(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.UninstallData]:
    venv_dir = _venv_dir(args, ctx)
    with ctx.venv_container.venv_lock(venv_dir):
        return commands.uninstall(venv_dir, paths.ctx.bin_dir, paths.ctx.man_dir, verbose=ctx.verbose)


def _add_uninstall_all(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    p = subparsers.add_parser(
        "uninstall-all",
        help="Uninstall all packages",
        description="Uninstall all pipx-managed packages",
        parents=[shared_parser],
    )
    _add_output_option(p)
    p.set_defaults(func=_cmd_uninstall_all)


def _cmd_uninstall_all(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.UninstallData]:
    del args
    return commands.uninstall_all(ctx.venv_container, paths.ctx.bin_dir, paths.ctx.man_dir, verbose=ctx.verbose)


def _add_reset(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    parser: Final[argparse.ArgumentParser] = subparsers.add_parser(
        "reset",
        help="Return pipx to a fresh install",
        description=(
            "Uninstall every pipx-managed package and remove the shared libraries, the caches, the standalone "
            "interpreters, the logs and the trash, which returns pipx to the state it had when it was installed."
        ),
        parents=[shared_parser],
    )
    parser.add_argument("--yes", "-y", action="store_true", help="Reset without asking for confirmation")
    parser.add_argument("--dry-run", action="store_true", help="Report what a reset would remove and stop")
    _add_output_option(parser)
    parser.set_defaults(func=_cmd_reset)


def _cmd_reset(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.ResetData]:
    if not args.dry_run and not args.yes and not _confirmed_reset():
        msg = "Reset cancelled."
        raise PipxError(msg)
    return commands.reset(
        ctx.venv_container,
        paths.ctx.bin_dir,
        paths.ctx.man_dir,
        verbose=ctx.verbose,
        dry_run=args.dry_run,
    )


def _confirmed_reset() -> bool:
    if not sys.stdin or not sys.stdin.isatty():
        msg = "Pass --yes to reset when no terminal can confirm it."
        raise PipxError(msg)
    answer: Final[str] = input(
        f"Remove every pipx-installed package and reset pipx's data under {paths.ctx.home}? [y/N] "
    )
    return answer.strip().casefold() in {"y", "yes"}


def _add_reinstall(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    p = subparsers.add_parser(
        "reinstall",
        formatter_class=LineWrapRawTextHelpFormatter,
        help="Reinstall a package",
        description=textwrap.dedent(
            """
            Reinstalls a package.

            Package is uninstalled, then installed with pipx install PACKAGE
            with the same options used in the original install of PACKAGE.

            """
        ),
        parents=[shared_parser],
    )
    p.add_argument("package", metavar="ENVIRONMENT").completer = venv_completer
    add_python_options(p)
    add_backend_arg(p)
    _add_output_option(p)
    p.set_defaults(func=_cmd_reinstall)


def _cmd_reinstall(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.ReinstallData]:
    venv_dir = _venv_dir(args, ctx)
    with ctx.venv_container.venv_lock(venv_dir) as venv_lock:
        return commands.reinstall(
            venv_dir=venv_dir,
            local_bin_dir=paths.ctx.bin_dir,
            local_man_dir=paths.ctx.man_dir,
            python=ctx.python,
            verbose=ctx.verbose,
            python_flag_passed=ctx.python_flag_passed,
            backend=ctx.backend,
            env_backend=ctx.env_backend,
            venv_lock=venv_lock,
        )


def _add_reinstall_all(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    p = subparsers.add_parser(
        "reinstall-all",
        formatter_class=LineWrapRawTextHelpFormatter,
        help="Reinstall all packages",
        description=textwrap.dedent(
            """
            Reinstalls all packages.

            Packages are uninstalled, then installed with pipx install PACKAGE
            with the same options used in the original install of PACKAGE.
            This is useful if you upgraded to a new version of Python and want
            all your packages to use the latest as well.

            """
        ),
        parents=[shared_parser],
    )
    add_python_options(p)
    p.add_argument("--skip", nargs="+", default=[], help="skip these packages")
    add_backend_arg(p)
    _add_output_option(p)
    p.set_defaults(func=_cmd_reinstall_all)


def _cmd_reinstall_all(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.ReinstallData]:
    del args
    return commands.reinstall_all(
        ctx.venv_container,
        paths.ctx.bin_dir,
        paths.ctx.man_dir,
        ctx.python,
        verbose=ctx.verbose,
        skip=ctx.skip_list,
        python_flag_passed=ctx.python_flag_passed,
        backend=ctx.backend,
        env_backend=ctx.env_backend,
    )


def _add_health(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    parser = subparsers.add_parser(
        "health",
        help="Check installed package environments",
        description="Check whether installed package environments can run their Python interpreter.",
        parents=[shared_parser],
    )
    parser.add_argument("packages", nargs="*", help="Installed packages to check").completer = venv_completer
    _add_output_option(parser)
    parser.set_defaults(func=_cmd_health)


def _cmd_health(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.HealthData]:
    return commands.health(ctx.venv_container, _selected_venv_dirs(args, ctx))


def _add_repair(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    parser = subparsers.add_parser(
        "repair",
        help="Repair broken package environments",
        description="Reinstall packages whose environments cannot run their Python interpreter.",
        parents=[shared_parser],
    )
    parser.add_argument("packages", nargs="*", help="Installed packages to repair").completer = venv_completer
    add_python_options(parser)
    add_backend_arg(parser)
    _add_output_option(parser)
    parser.set_defaults(func=_cmd_repair)


def _cmd_repair(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.RepairData]:
    return commands.repair(
        ctx.venv_container,
        _selected_venv_dirs(args, ctx),
        paths.ctx.bin_dir,
        paths.ctx.man_dir,
        ctx.python,
        verbose=ctx.verbose,
        python_flag_passed=ctx.python_flag_passed,
        backend=ctx.backend,
        env_backend=ctx.env_backend,
    )


def _selected_venv_dirs(args: argparse.Namespace, ctx: DispatchContext) -> tuple[Path, ...]:
    if args.packages:
        # spellings such as "black" and "Black" resolve to one venv, so drop the repeats
        return tuple(dict.fromkeys(_venv_dirs(args, ctx).values()))
    return tuple(sorted(ctx.venv_container.iter_venv_dirs()))


def _add_list(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    p = subparsers.add_parser(
        "list",
        help="List installed packages",
        description="List packages and apps installed with pipx",
        parents=[shared_parser],
    )
    p.add_argument(
        "--include-injected",
        action="store_true",
        help="Show packages injected into the main app's environment",
    )
    p.add_argument("--outdated", action="store_true", help="List packages with an available upgrade.")
    g = p.add_mutually_exclusive_group()
    g.add_argument("--short", action="store_true", help="List packages only.")
    g.add_argument(
        "--pinned",
        action="store_true",
        help=(
            "List pinned packages only. Pass --include-injected at the same time to list injected packages "
            "that were pinned."
        ),
    )
    _add_output_option(p, snapshot_json=True)
    p.add_argument("packages", nargs="*", help="Installed packages to list").completer = venv_completer
    p.set_defaults(func=_cmd_list)


def _cmd_list(args: argparse.Namespace, ctx: DispatchContext) -> ExitCode | OperationResult[commands.OutdatedData]:
    venv_dirs: Final[tuple[Path, ...]] = _installed_venv_dirs(args, ctx)
    if args.outdated:
        if args.short or args.pinned:
            msg = "--outdated cannot be combined with --short or --pinned."
            raise PipxError(msg)
        return commands.list_outdated(ctx.venv_container, venv_dirs, include_injected=args.include_injected)
    output: Final[OutputFormat] = _output_format(args)
    if output is OutputFormat.JSON and (args.short or args.pinned):
        msg = "--output json cannot be combined with --short or --pinned."
        raise PipxError(msg)
    return commands.list_packages(
        ctx.venv_container,
        venv_dirs,
        include_injected=args.include_injected,
        json_format=output is OutputFormat.JSON,
        short_format=args.short,
        pinned_only=args.pinned,
    )


def _installed_venv_dirs(args: argparse.Namespace, ctx: DispatchContext) -> tuple[Path, ...]:
    venv_dirs: Final[tuple[Path, ...]] = _selected_venv_dirs(args, ctx)
    for venv_dir in venv_dirs:
        if not venv_dir.is_dir():
            name = venv_dir.name
            msg = f"venv for {name!r} was not found. Was {name!r} installed with pipx?"
            raise PipxError(msg)
    return venv_dirs


def _add_interpreter(
    subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser
) -> argparse.ArgumentParser:
    p: argparse.ArgumentParser = subparsers.add_parser(
        "interpreter",
        help="Interact with interpreters managed by pipx",
        description="Interact with interpreters managed by pipx",
        parents=[shared_parser],
    )
    s = p.add_subparsers(
        title="subcommands",
        description="Get help for commands with pipx interpreter COMMAND --help",
        dest="interpreter_command",
    )
    list_p = s.add_parser(
        "list",
        help="List available interpreters",
        description="List available interpreters",
        parents=[shared_parser],
    )
    prune_p = s.add_parser(
        "prune",
        help="Prune unused interpreters",
        description="Prune unused interpreters",
        parents=[shared_parser],
    )
    upgrade_p = s.add_parser(
        "upgrade",
        help="Upgrade installed interpreters to the latest available micro/patch version",
        description="Upgrade installed interpreters to the latest available micro/patch version",
        parents=[shared_parser],
    )
    _add_output_option(list_p)
    list_p.set_defaults(func=_cmd_interpreter_list)
    _add_output_option(prune_p)
    prune_p.set_defaults(func=_cmd_interpreter_prune)
    _add_output_option(upgrade_p)
    upgrade_p.set_defaults(func=_cmd_interpreter_upgrade)
    p.set_defaults(func=_make_print_help(p))
    return p


def _cmd_interpreter_list(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.InterpreterData]:
    del args
    return commands.list_interpreters(ctx.venv_container)


def _cmd_interpreter_prune(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.InterpreterData]:
    del args
    return commands.prune_interpreters(ctx.venv_container)


def _cmd_interpreter_upgrade(
    args: argparse.Namespace, ctx: DispatchContext
) -> OperationResult[commands.InterpreterData]:
    del args
    return commands.upgrade_interpreters(ctx.venv_container, verbose=ctx.verbose)


def _add_cache(
    subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser
) -> argparse.ArgumentParser:
    parser: Final[argparse.ArgumentParser] = subparsers.add_parser(
        "cache",
        help="Manage cached run environments",
        description="Manage cached run environments",
        parents=[shared_parser],
    )
    subcommands: Final[argparse._SubParsersAction] = parser.add_subparsers(
        title="subcommands",
        description="Get help for commands with pipx cache COMMAND --help",
        dest="cache_command",
    )
    dir_parser: Final[argparse.ArgumentParser] = subcommands.add_parser(
        "dir",
        help="Show the cache directory",
        description="Show the cache directory",
        parents=[shared_parser],
    )
    purge_parser: Final[argparse.ArgumentParser] = subcommands.add_parser(
        "purge",
        help="Remove cached run environments",
        description="Remove cached run environments",
        parents=[shared_parser],
    )
    _add_output_option(dir_parser)
    dir_parser.set_defaults(func=_cmd_cache_dir)
    _add_output_option(purge_parser)
    purge_parser.set_defaults(func=_cmd_cache_purge)
    parser.set_defaults(func=_make_print_help(parser))
    return parser


def _cmd_cache_dir(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.CacheData]:
    del args, ctx
    return commands.print_cache_dir(VenvContainer(paths.ctx.venv_cache))


def _cmd_cache_purge(args: argparse.Namespace, ctx: DispatchContext) -> OperationResult[commands.CacheData]:
    del args, ctx
    return commands.purge_cache(VenvContainer(paths.ctx.venv_cache))


def _add_run(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    p = subparsers.add_parser(
        "run",
        formatter_class=LineWrapRawTextHelpFormatter,
        help=(
            "Download the latest version of a package to a temporary virtual environment, "
            "then run an app from it. Also compatible with local `__pypackages__` "
            "directory (experimental)."
        ),
        description=textwrap.dedent(
            f"""
            Download the latest version of a package to a temporary virtual environment,
            then run an app from it. The environment will be cached
            and reused for up to {constants.TEMP_VENV_EXPIRATION_THRESHOLD_DAYS} days. This
            means subsequent calls to 'run' for the same package will be faster
            since they can reuse the cached Virtual Environment.

            In support of PEP 582 'run' will use apps found in a local __pypackages__
            directory, if present. Please note that this behavior is experimental,
            and acts as a companion tool to pythonloc. It may be modified or
            removed in the future. See https://github.com/cs01/pythonloc.
            """
        ),
        parents=[shared_parser],
    )
    cache_group: Final[argparse._MutuallyExclusiveGroup] = p.add_mutually_exclusive_group()
    cache_group.add_argument(
        "--no-cache",
        action="store_true",
        help="Do not reuse cached virtual environment if it exists",
    )
    cache_group.add_argument(
        "--refresh",
        action="store_true",
        help="Rebuild and cache the virtual environment",
    )
    p.add_argument(
        "--no-path-check",
        action="store_true",
        help="Do not check whether the app is already on PATH",
    )
    p.add_argument(
        "--python-args",
        metavar="ARGS",
        type=_split_python_args,
        default=[],
        help="Arguments to pass to the Python interpreter that runs the application",
    )
    p.add_argument(
        "app_with_args",
        metavar="app ...",
        nargs=argparse.REMAINDER,
        help="app/package name and any arguments to be passed to it",
        default=[],
    )
    p.add_argument("--path", action="store_true", help="Interpret app name as a local path")
    p.add_argument(
        "--pypackages",
        action="store_true",
        help="Require app to be run from local __pypackages__ directory",
    )
    p.add_argument(
        "--with",
        dest="with_",
        action="append",
        default=[],
        help="Extra dependencies to add to the temporary environment",
    )
    p.add_argument("--spec", help=SPEC_HELP)
    add_python_options(p)
    add_pip_venv_args(p)
    add_backend_arg(p)
    p.set_defaults(subparser=p, func=_cmd_run)

    # modify usage text to show required app argument
    p.usage = re.sub(r"^usage: ", "", p.format_usage())
    # add a double-dash to usage text to show requirement before app
    p.usage = re.sub(r"\.\.\.", "app ...", p.usage)


def _cmd_run(args: argparse.Namespace, ctx: DispatchContext) -> NoReturn:
    commands.run(
        args.app_with_args[0],
        ctx.spec,
        args.with_,
        args.app_with_args[1:],
        ctx.python,
        ctx.pip_args,
        ctx.venv_args,
        is_path=args.path,
        pypackages=args.pypackages,
        verbose=ctx.verbose,
        use_cache=not args.no_cache,
        refresh=args.refresh,
        no_path_check=args.no_path_check,
        backend=ctx.backend,
        env_backend=ctx.env_backend,
        cooldown_days=ctx.cooldown_days,
        python_args=args.python_args,
        python_flag_passed=ctx.python_flag_passed,
        fetch_python=ctx.fetch_python,
    )


def _add_execute(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    parser: Final[argparse.ArgumentParser] = subparsers.add_parser(
        "exec",
        help="Run an application from an existing pipx environment",
        description="Run an application from an existing pipx environment",
        parents=[shared_parser],
    )
    cast(
        "_CompletableAction",
        parser.add_argument("package", metavar="ENVIRONMENT", help="Name of the existing pipx environment"),
    ).completer = venv_completer
    parser.add_argument("app", metavar="APP", help="Application to run")
    parser.add_argument(
        "app_args",
        nargs=argparse.REMAINDER,
        default=[],
        help="Arguments to pass to the application",
    )
    parser.set_defaults(func=_cmd_execute)


def _cmd_execute(args: argparse.Namespace, ctx: DispatchContext) -> NoReturn:
    venv_dir: Final[Path] = _venv_dir(args, ctx)
    with ctx.venv_container.venv_lock(venv_dir):
        commands.execute(args.package, venv_dir, args.app, args.app_args)


def _add_runpip(
    subparsers: argparse._SubParsersAction,
    venv_completer: VenvCompleter,
    shared_parser: argparse.ArgumentParser,
) -> None:
    p = subparsers.add_parser(
        "runpip",
        help="Run pip in an existing pipx-managed Virtual Environment",
        description="Run pip in an existing pipx-managed Virtual Environment",
        parents=[shared_parser],
    )
    p.add_argument(
        "package",
        help="Name of the existing pipx-managed Virtual Environment to run pip in",
    ).completer = venv_completer
    p.add_argument(
        "pipargs",
        nargs=argparse.REMAINDER,
        default=[],
        help="Arguments to forward to pip command",
    )
    p.set_defaults(func=_cmd_runpip)


def _cmd_runpip(args: argparse.Namespace, ctx: DispatchContext) -> ExitCode:
    venv_dir = _venv_dir(args, ctx)
    with ctx.venv_container.venv_lock(venv_dir):
        return commands.run_pip(args.package, venv_dir, get_runpip_args(args.pipargs), verbose=ctx.verbose)


def _add_ensurepath(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    p = subparsers.add_parser(
        "ensurepath",
        help=("Ensure directories necessary for pipx operation are in your PATH environment variable."),
        description=(
            "Ensure directory where pipx stores apps is in your "
            "PATH environment variable. Also if pipx was installed via "
            "`pip install --user`, ensure pipx itself is in your PATH. "
            "This command may modify your shell configuration, such as '~/.bashrc', "
            "or the system PATH configuration when used with `--global`."
        ),
        parents=[shared_parser],
    )
    p.add_argument(
        "--prepend",
        action="store_true",
        help=(
            "Prepend directories to your PATH instead of appending. "
            "This is useful if you want to prioritize pipx apps over system apps."
        ),
    )
    p.add_argument(
        "--force",
        "-f",
        action="store_true",
        help=(
            "Add text to your shell's config file even if it looks like your "
            "PATH already contains paths to pipx and pipx-install apps."
        ),
    )
    p.add_argument(
        "--all-shells",
        action="store_true",
        help=("Add directories to PATH in all shells instead of just the current one."),
    )
    p.add_argument(
        "--dry-run",
        action="store_true",
        help=("Report what would be added to PATH without modifying PATH or any shell configuration file."),
    )
    p.set_defaults(func=_cmd_ensurepath)


def _cmd_ensurepath(args: argparse.Namespace, ctx: DispatchContext) -> ExitCode:
    del ctx
    try:
        return commands.ensure_pipx_paths(
            prepend=args.prepend,
            force=args.force,
            all_shells=args.all_shells,
            dry_run=args.dry_run,
            is_global=getattr(args, "is_global", False),
        )
    except Exception as e:
        logger.debug("Uncaught Exception:", exc_info=True)
        raise PipxError(str(e), wrap_message=False) from None


def _add_environment(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
    p = subparsers.add_parser(
        "environment",
        formatter_class=LineWrapRawTextHelpFormatter,
        help="Print a list of environment variables and paths used by pipx.",
        description=textwrap.dedent(
            """
            Prints the names and current values of environment variables used by pipx,
            followed by internal pipx variables which are derived from the environment
            variables and platform specific default values.

            Available variables:
            """
        )
        + textwrap.fill(", ".join(ENVIRONMENT_VARIABLES), break_long_words=False),
        parents=[shared_parser],
    )
    p.add_argument(
        "--value",
        "-V",
        choices=ENVIRONMENT_VALUE_CHOICES,
        metavar="VARIABLE",
        help="Print the value of the variable.",
    )
    p.set_defaults(func=_cmd_environment)


def _cmd_environment(args: argparse.Namespace, ctx: DispatchContext) -> ExitCode:
    del ctx
    return commands.environment(value=args.value)


def _venv_dir(args: argparse.Namespace, ctx: DispatchContext) -> Path:
    venv_dir = ctx.venv_container.get_venv_dir(valid_pypi_name(args.package) or args.package)
    logger.info("Virtual Environment location is %s", venv_dir)
    return venv_dir


def _venv_dirs(args: argparse.Namespace, ctx: DispatchContext) -> dict[str, Path]:
    venv_dirs = {pkg: ctx.venv_container.get_venv_dir(valid_pypi_name(pkg) or pkg) for pkg in args.packages}
    venv_dirs_msg = "\n".join(f"- {key} : {value}" for key, value in venv_dirs.items())
    logger.info("Virtual Environment locations are:\n%s", venv_dirs_msg)
    return venv_dirs


def _make_print_help(
    target_parser: argparse.ArgumentParser,
) -> Callable[[argparse.Namespace, DispatchContext], ExitCode]:
    def _print_help(args: argparse.Namespace, ctx: DispatchContext) -> ExitCode:
        del args, ctx
        target_parser.print_help()
        return EXIT_CODE_OK

    return _print_help


def get_command_parser() -> tuple[argparse.ArgumentParser, dict[str, argparse.ArgumentParser]]:
    venv_container = VenvContainer(paths.ctx.venvs)

    completer_venvs = InstalledVenvsCompleter(venv_container)

    shared_parser = argparse.ArgumentParser(add_help=False)

    shared_parser.add_argument(
        "--quiet",
        "-q",
        action="count",
        default=0,
        help=(
            "Give less output. May be used multiple times corresponding to the"
            " ERROR and CRITICAL logging levels. The count maxes out at 2."
        ),
    )

    shared_parser.add_argument(
        "--verbose",
        "-v",
        action="count",
        default=0,
        help=(
            "Give more output. May be used multiple times corresponding to the"
            " INFO, DEBUG and NOTSET logging levels. The count maxes out at 3."
        ),
    )

    shared_parser.add_argument(
        "--skip-maintenance",
        action="store_true",
        help="Do not upgrade shared libraries; use bundled pip when creating them.",
    )

    if not constants.WINDOWS:
        shared_parser.add_argument(
            "--global",
            action="store_true",
            dest="is_global",
            help="Perform action globally for all users.",
        )

    parser = argparse.ArgumentParser(
        prog=prog_name(),
        formatter_class=LineWrapRawTextHelpFormatter,
        description=PIPX_DESCRIPTION,
    )
    vars(parser)["man_short_description"] = PIPX_DESCRIPTION.splitlines()[1]

    subparsers = parser.add_subparsers(dest="command", description="Get help for commands with pipx COMMAND --help")

    subparsers_with_subcommands = {}
    _add_install(subparsers, shared_parser)
    _add_install_all(subparsers, shared_parser)
    _add_uninject(subparsers, completer_venvs.use, shared_parser)
    _add_inject(subparsers, completer_venvs.use, shared_parser)
    _add_expose(subparsers, completer_venvs.use, shared_parser)
    _add_unexpose(subparsers, completer_venvs.use, shared_parser)
    _add_pin(subparsers, shared_parser)
    _add_unpin(subparsers, shared_parser)
    _add_upgrade(subparsers, completer_venvs.use, shared_parser)
    _add_upgrade_all(subparsers, shared_parser)
    _add_upgrade_shared(subparsers, shared_parser)
    _add_uninstall(subparsers, completer_venvs.use, shared_parser)
    _add_uninstall_all(subparsers, shared_parser)
    _add_reset(subparsers, shared_parser)
    _add_reinstall(subparsers, completer_venvs.use, shared_parser)
    _add_reinstall_all(subparsers, shared_parser)
    _add_health(subparsers, completer_venvs.use, shared_parser)
    _add_repair(subparsers, completer_venvs.use, shared_parser)
    _add_list(subparsers, completer_venvs.use, shared_parser)
    subparsers_with_subcommands["interpreter"] = _add_interpreter(subparsers, shared_parser)
    subparsers_with_subcommands["cache"] = _add_cache(subparsers, shared_parser)
    subparsers_with_subcommands["manifest"] = _add_manifest(subparsers, shared_parser)
    _add_run(subparsers, shared_parser)
    _add_execute(subparsers, completer_venvs.use, shared_parser)
    _add_runpip(subparsers, completer_venvs.use, shared_parser)
    _add_ensurepath(subparsers, shared_parser)
    _add_environment(subparsers, shared_parser)

    parser.add_argument("--version", action="store_true", help="Print version and exit")
    completions_p = subparsers.add_parser(
        "completions",
        help="Print instructions on enabling shell completions for pipx",
        description="Print instructions on enabling shell completions for pipx",
        parents=[shared_parser],
    )
    completions_p.set_defaults(func=_cmd_completions)
    help_p = subparsers.add_parser(
        "help",
        help="Show help for pipx or a command",
        description="Show help for pipx or a command",
        parents=[shared_parser],
    )
    help_p.set_defaults(func=_make_print_help(parser))
    return parser, subparsers_with_subcommands


def build_parser() -> argparse.ArgumentParser:
    # sphinx-argparse-cli renders the CLI reference from a parser-returning callable; expose the root parser alone
    # so the docs build cannot drift from the argument definitions.
    return get_command_parser()[0]


def _cmd_completions(args: argparse.Namespace, ctx: DispatchContext) -> ExitCode:
    del args, ctx
    print(constants.completion_instructions)  # ruff:ignore[print]  # user-facing CLI output
    return ExitCode(0)


def delete_oldest_logs(file_list: list[Path], keep_number: int) -> None:
    file_list = sorted(file_list)
    # Count from the front rather than slicing with -keep_number: file_list[:-0] is
    # file_list[:0], so keeping none used to delete none. max() guards the other end,
    # where keep_number exceeds the list and a negative bound would delete instead.
    for existing_file in file_list[: max(0, len(file_list) - keep_number)]:
        existing_file.unlink(missing_ok=True)


def _setup_log_file(pipx_log_dir: Path | None = None) -> Path:
    max_logs = int(os.getenv("PIPX_MAX_LOGS", "10"))
    pipx_log_dir = pipx_log_dir or paths.ctx.logs
    # don't use utils.mkdir, to prevent emission of log message
    pipx_log_dir.mkdir(parents=True, exist_ok=True)

    delete_oldest_logs(list(pipx_log_dir.glob("cmd_*[0-9].log")), max_logs)
    delete_oldest_logs(list(pipx_log_dir.glob("cmd_*_pip_errors.log")), max_logs)

    datetime_str = time.strftime("%Y-%m-%d_%H.%M.%S")
    log_file = pipx_log_dir / f"cmd_{datetime_str}.log"
    counter = 1
    max_same_second_logs: Final = 10
    while log_file.exists() and counter < max_same_second_logs:
        log_file = pipx_log_dir / f"cmd_{datetime_str}_{counter}.log"
        counter += 1

    log_file.touch()

    return log_file


def setup_log_file() -> Path:
    try:
        return _setup_log_file()
    except PermissionError:
        return _setup_log_file(platformdirs.user_log_path("pipx"))


def setup_logging(verbose: int) -> None:
    pipx_str = bold(green("pipx >")) if sys.stdout and sys.stdout.isatty() else "pipx >"
    paths.ctx.log_file = setup_log_file()

    # Determine logging level, a value between 0 and 50
    level_number = min(max(0, logging.WARNING - 10 * verbose), 50)

    level = logging.getLevelName(level_number)

    # "incremental" is False so previous pytest tests don't accumulate handlers
    logging_config = {
        "version": 1,
        "formatters": {
            "stream_nonverbose": {
                "class": "logging.Formatter",
                "format": "{message}",
                "style": "{",
            },
            "stream_verbose": {
                "class": "logging.Formatter",
                "format": pipx_str + "({funcName}:{lineno}): {message}",
                "style": "{",
            },
            "file": {
                "class": "logging.Formatter",
                "format": "{relativeCreated: >8.1f}ms ({funcName}:{lineno}): {message}",
                "style": "{",
            },
        },
        "handlers": {
            "stream": {
                "class": "logging.StreamHandler",
                "formatter": "stream_verbose" if verbose else "stream_nonverbose",
                "level": level,
            },
            "file": {
                "class": "logging.FileHandler",
                "formatter": "file",
                "filename": str(paths.ctx.log_file),
                "encoding": "utf-8",
                "level": "DEBUG",
            },
        },
        "loggers": {"pipx": {"handlers": ["stream", "file"], "level": "DEBUG"}},
        "incremental": False,
    }
    logging.config.dictConfig(logging_config)


def setup(args: argparse.Namespace) -> None:
    if "version" in args and args.version:
        print_version()
        sys.exit(0)

    if not constants.WINDOWS and getattr(args, "is_global", False):
        paths.ctx.make_global()

    verbose: Final[int] = (
        -2 if _output_format(args) is OutputFormat.JSON else getattr(args, "verbose", 0) - getattr(args, "quiet", 0)
    )

    setup_logging(verbose)
    paths.ctx.log_warnings()

    logger.debug("%s", time.strftime("%Y-%m-%d %H:%M:%S"))
    logger.debug("%s", " ".join(sys.argv))
    logger.info("pipx version is %s", __version__)
    logger.info("Default python interpreter is '%s'", get_default_python())

    mkdir(paths.ctx.venvs)
    mkdir(paths.ctx.bin_dir)
    mkdir(paths.ctx.man_dir)
    mkdir(paths.ctx.venv_cache)
    mkdir(paths.ctx.standalone_python_cachedir)

    for cachedir in [
        paths.ctx.venv_cache,
        paths.ctx.standalone_python_cachedir,
    ]:
        cachedir_tag = cachedir / "CACHEDIR.TAG"
        if not cachedir_tag.exists():
            logger.debug("Adding CACHEDIR.TAG to cache directory")
            signature = (
                "Signature: 8a477f597d28d172789f06886806bc55\n"
                "# This file is a cache directory tag created by pipx.\n"
                "# For information about cache directory tags, see:\n"
                "#       https://bford.info/cachedir/\n"
            )
            cachedir_tag.write_text(signature, encoding="utf-8")

    rmdir(paths.ctx.trash, safe_rm=False)

    old_pipx_venv_location = paths.ctx.venvs / "pipx-app"
    if old_pipx_venv_location.exists():
        logger.warning(
            pipx_wrap(
                f"""
                {hazard}  A virtual environment for pipx was detected at
                {old_pipx_venv_location!s}. The 'pipx-app' package has been
                renamed back to 'pipx'
                (https://github.com/pypa/pipx/issues/82).
                """,
                subsequent_indent=" " * 4,
            )
        )


def check_args(parsed_pipx_args: argparse.Namespace) -> None:
    if parsed_pipx_args.command == "run":
        # argparse.REMAINDER preserves the separator; discard it before app invocation.
        if parsed_pipx_args.app_with_args and parsed_pipx_args.app_with_args[0] == "--":
            parsed_pipx_args.app_with_args.pop(0)
        # The app shares argparse.REMAINDER, so enforce its required status after parsing.
        if not parsed_pipx_args.app_with_args:
            parsed_pipx_args.subparser.error("the following arguments are required: app")


def normalize_help_command(args: list[str]) -> list[str]:
    if args and args[0] == "help":
        if len(args) == 1:
            return ["--help"]
        return [*args[1:], "--help"]
    return args


def _get_subparser(parser: argparse.ArgumentParser, command: str) -> argparse.ArgumentParser:
    subparsers_action = next(
        action
        for action in parser._actions  # ruff:ignore[private-member-access]  # no public API for subparsers
        if isinstance(action, argparse._SubParsersAction)  # ruff:ignore[private-member-access]  # no public API for subparsers
    )
    return cast("argparse.ArgumentParser", subparsers_action.choices[command])


def parse_pipx_args(parser: argparse.ArgumentParser, args: list[str]) -> argparse.Namespace:
    args = normalize_help_command(args)
    if args and args[0] == "inject":
        parsed_args = _get_subparser(parser, "inject").parse_intermixed_args(args[1:])
        parsed_args.command = "inject"
        return parsed_args
    return parser.parse_args(args)


def _dispatch(argv: list[str]) -> ExitCode:
    parser, _ = get_command_parser()
    argcomplete.autocomplete(parser, always_complete_options=False)
    parsed_pipx_args = parse_pipx_args(parser, argv)
    _validate_fetch_python()
    setup(parsed_pipx_args)
    check_args(parsed_pipx_args)
    if not parsed_pipx_args.command:
        parser.print_help()
        return ExitCode(1)
    return run_pipx_command(parsed_pipx_args)


def cli() -> ExitCode:
    """Entry point from command line"""
    try:
        hide_cursor()
        return _dispatch(sys.argv[1:])
    except PipxError as e:
        print(str(e), file=sys.stderr)  # ruff:ignore[print]  # user-facing CLI output
        logger.debug("PipxError: %s", e, exc_info=True)
        return ExitCode(1)
    except KeyboardInterrupt:
        return ExitCode(1)
    except Exception:
        logger.debug("Uncaught Exception:", exc_info=True)
        raise
    finally:
        logger.debug("pipx finished.")
        show_cursor()


if __name__ == "__main__":
    sys.exit(cli())


__all__ = [
    "build_parser",
    "cli",
]
