From ef70a775f4ccbf6ea3743d3bab8644f2d6204811 Mon Sep 17 00:00:00 2001 From: Chris Barnes Date: Tue, 24 Sep 2019 15:41:23 -0400 Subject: [PATCH] Refactor argument parser construction Each subparser is added in its own function --- pipx/main.py | 93 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 28 deletions(-) diff --git a/pipx/main.py b/pipx/main.py index 129a182ad1..12c4295f8f 100644 --- a/pipx/main.py +++ b/pipx/main.py @@ -2,6 +2,7 @@ # PYTHON_ARGCOMPLETE_OK """The command line interface to pipx""" +from pathlib import Path import argcomplete # type: ignore import argparse @@ -30,6 +31,7 @@ __version__ = "0.14.0.0" +__version_info__ = tuple(int(n) for n in __version__.split('.')) def print_version() -> None: @@ -266,21 +268,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(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", @@ -307,6 +295,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", @@ -336,6 +326,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", @@ -353,6 +345,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. " @@ -371,6 +365,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", @@ -379,6 +375,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", @@ -386,6 +384,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, @@ -410,6 +410,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", @@ -417,6 +419,8 @@ def get_command_parser(): ) p.add_argument("--verbose", action="store_true") + +def _add_run(subparsers): p = subparsers.add_parser( "run", formatter_class=LineWrapRawTextHelpFormatter, @@ -427,17 +431,17 @@ def get_command_parser(): ), 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 re-used for up to {TEMP_VENV_EXPIRATION_THRESHOLD_DAYS} days. This - means subsequent calls to 'run' for the same package will be faster - since they can re-use 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 is a acts as a companion tool to pythonloc. It may be modified or - removed in the future. See https://github.com/cs01/pythonloc. - """ + Download the latest version of a package to a temporary virtual environment, + then run an app from it. The environment will be cached + and re-used for up to {TEMP_VENV_EXPIRATION_THRESHOLD_DAYS} days. This + means subsequent calls to 'run' for the same package will be faster + since they can re-use 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 is a acts as a companion tool to pythonloc. It may be modified or + removed in the future. See https://github.com/cs01/pythonloc. + """ ), ) p.add_argument( @@ -466,6 +470,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", @@ -483,6 +489,8 @@ def get_command_parser(): ) p.add_argument("--verbose", action="store_true") + +def _add_ensurepath(subparsers): p = subparsers.add_parser( "ensurepath", help=( @@ -500,10 +508,39 @@ def get_command_parser(): f"PATH already has {str(LOCAL_BIN_DIR)}" ), ) + + +def get_command_parser(): + venv_container = VenvContainer(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( + subparsers.add_parser( "completions", - help=("Print instructions on enabling shell completions for pipx"), + help="Print instructions on enabling shell completions for pipx", ) return parser