diff --git a/pipx/main.py b/pipx/main.py index 97dc4255b0..9a87c93b52 100644 --- a/pipx/main.py +++ b/pipx/main.py @@ -6,6 +6,7 @@ import argparse import functools import logging +from pkg_resources import parse_version import shlex import sys import textwrap @@ -13,13 +14,19 @@ from typing import Dict, List import argcomplete # type: ignore - -from . import commands, constants from .colors import bold, green +from . import commands +from . import constants from .util import PipxError, mkdir from .Venv import VenvContainer + __version__ = "0.14.0.0" +_, __version_info__, subver, *_ = parse_version(__version__)._key # type: ignore +if isinstance(subver, tuple): + while len(__version_info__) < 4: + __version_info__ += (0,) + __version_info__ += subver def print_version() -> None: @@ -257,21 +264,7 @@ def _autocomplete_list_of_installed_packages( return list(str(p.name) for p in sorted(venv_container.iter_venv_dirs())) -def get_command_parser(): - venv_container = VenvContainer(constants.PIPX_LOCAL_VENVS) - - autocomplete_list_of_installed_packages = functools.partial( - _autocomplete_list_of_installed_packages, venv_container - ) - - parser = argparse.ArgumentParser( - formatter_class=LineWrapRawTextHelpFormatter, description=PIPX_DESCRIPTION - ) - - subparsers = parser.add_subparsers( - dest="command", description="Get help for commands with pipx COMMAND --help" - ) - +def _add_install(subparsers): p = subparsers.add_parser( "install", help="Install a package", @@ -298,6 +291,8 @@ def get_command_parser(): ) add_pip_venv_args(p) + +def _add_inject(subparsers, autocomplete_list_of_installed_packages): p = subparsers.add_parser( "inject", help="Install packages into an existing Virtual Environment", @@ -327,6 +322,8 @@ def get_command_parser(): ) p.add_argument("--verbose", action="store_true") + +def _add_upgrade(subparsers, autocomplete_list_of_installed_packages): p = subparsers.add_parser( "upgrade", help="Upgrade a package", @@ -344,6 +341,8 @@ def get_command_parser(): add_pip_venv_args(p) p.add_argument("--verbose", action="store_true") + +def _add_upgrade_all(subparsers): p = subparsers.add_parser( "upgrade-all", help="Upgrade all packages. " @@ -362,6 +361,8 @@ def get_command_parser(): ) p.add_argument("--verbose", action="store_true") + +def _add_uninstall(subparsers, autocomplete_list_of_installed_packages): p = subparsers.add_parser( "uninstall", help="Uninstall a package", @@ -370,6 +371,8 @@ def get_command_parser(): p.add_argument("package").completer = autocomplete_list_of_installed_packages p.add_argument("--verbose", action="store_true") + +def _add_uninstall_all(subparsers): p = subparsers.add_parser( "uninstall-all", help="Uninstall all packages", @@ -377,6 +380,8 @@ def get_command_parser(): ) p.add_argument("--verbose", action="store_true") + +def _add_reinstall_all(subparsers): p = subparsers.add_parser( "reinstall-all", formatter_class=LineWrapRawTextHelpFormatter, @@ -401,6 +406,8 @@ def get_command_parser(): p.add_argument("--skip", nargs="+", default=[], help="skip these packages") p.add_argument("--verbose", action="store_true") + +def _add_list(subparsers): p = subparsers.add_parser( "list", help="List installed packages", @@ -408,6 +415,8 @@ def get_command_parser(): ) p.add_argument("--verbose", action="store_true") + +def _add_run(subparsers): p = subparsers.add_parser( "run", formatter_class=LineWrapRawTextHelpFormatter, @@ -457,6 +466,8 @@ def get_command_parser(): ) add_pip_venv_args(p) + +def _add_runpip(subparsers, autocomplete_list_of_installed_packages): p = subparsers.add_parser( "runpip", help="Run pip in an existing pipx-managed Virtual Environment", @@ -474,6 +485,8 @@ def get_command_parser(): ) p.add_argument("--verbose", action="store_true") + +def _add_ensurepath(subparsers): p = subparsers.add_parser( "ensurepath", help=( @@ -491,10 +504,38 @@ def get_command_parser(): f"PATH already has {str(constants.LOCAL_BIN_DIR)}" ), ) + + +def get_command_parser(): + venv_container = VenvContainer(constants.PIPX_LOCAL_VENVS) + + autocomplete_list_of_installed_packages = functools.partial( + _autocomplete_list_of_installed_packages, venv_container + ) + + parser = argparse.ArgumentParser( + formatter_class=LineWrapRawTextHelpFormatter, description=PIPX_DESCRIPTION + ) + + subparsers = parser.add_subparsers( + dest="command", description="Get help for commands with pipx COMMAND --help" + ) + + _add_install(subparsers) + _add_inject(subparsers, autocomplete_list_of_installed_packages) + _add_upgrade(subparsers, autocomplete_list_of_installed_packages) + _add_upgrade_all(subparsers) + _add_uninstall(subparsers, autocomplete_list_of_installed_packages) + _add_uninstall_all(subparsers) + _add_reinstall_all(subparsers) + _add_list(subparsers) + _add_run(subparsers) + _add_runpip(subparsers, autocomplete_list_of_installed_packages) + _add_ensurepath(subparsers) + parser.add_argument("--version", action="store_true", help="Print version and exit") - p = subparsers.add_parser( - "completions", - help=("Print instructions on enabling shell completions for pipx"), + subparsers.add_parser( + "completions", help="Print instructions on enabling shell completions for pipx" ) return parser