Skip to content

Commit

Permalink
Refactor argument parser construction
Browse files Browse the repository at this point in the history
Each subparser is added in its own function
  • Loading branch information
clbarnes committed Oct 16, 2019
1 parent de1a6f3 commit 1486517
Showing 1 changed file with 64 additions and 30 deletions.
94 changes: 64 additions & 30 deletions pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# PYTHON_ARGCOMPLETE_OK

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

import argcomplete # type: ignore
import argparse
import functools
Expand Down Expand Up @@ -30,6 +29,7 @@


__version__ = "0.14.0.0"
__version_info__ = tuple(int(n) for n in __version__.split("."))


def print_version() -> None:
Expand Down Expand Up @@ -266,21 +266,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",
Expand All @@ -307,6 +293,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",
Expand Down Expand Up @@ -336,6 +324,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",
Expand All @@ -353,6 +343,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. "
Expand All @@ -371,6 +363,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",
Expand All @@ -379,13 +373,17 @@ 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",
description="Uninstall all pipx-managed packages",
)
p.add_argument("--verbose", action="store_true")


def _add_reinstall_all(subparsers):
p = subparsers.add_parser(
"reinstall-all",
formatter_class=LineWrapRawTextHelpFormatter,
Expand All @@ -410,13 +408,17 @@ 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",
description="List packages and apps installed with pipx",
)
p.add_argument("--verbose", action="store_true")


def _add_run(subparsers):
p = subparsers.add_parser(
"run",
formatter_class=LineWrapRawTextHelpFormatter,
Expand All @@ -427,17 +429,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(
Expand Down Expand Up @@ -466,6 +468,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",
Expand All @@ -483,6 +487,8 @@ def get_command_parser():
)
p.add_argument("--verbose", action="store_true")


def _add_ensurepath(subparsers):
p = subparsers.add_parser(
"ensurepath",
help=(
Expand All @@ -500,10 +506,38 @@ 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(
"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

Expand Down

0 comments on commit 1486517

Please sign in to comment.