From 246130c48f195d4bcc7731a89998b745ec1770a3 Mon Sep 17 00:00:00 2001 From: Laszlo Kiss-Kollar Date: Wed, 12 Jun 2019 22:09:00 +0100 Subject: [PATCH] Consolidate `--path` parsing into `cmdoptions.py` This option is used both in the `freeze` and `list` commands. The code to parse the option and validate that it's not used with incompatible arguments should be done in one place. --- src/pip/_internal/cli/cmdoptions.py | 18 ++++++++++++++++++ src/pip/_internal/commands/freeze.py | 14 +++----------- src/pip/_internal/commands/list.py | 12 ++---------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/pip/_internal/cli/cmdoptions.py b/src/pip/_internal/cli/cmdoptions.py index dc00b9a1b..4b766b918 100644 --- a/src/pip/_internal/cli/cmdoptions.py +++ b/src/pip/_internal/cli/cmdoptions.py @@ -822,6 +822,24 @@ require_hashes = partial( ) # type: Callable[..., Option] +list_path = partial( + Option, + '--path', + dest='path', + action='append', + help='Restrict to the specified installation path for listing ' + 'packages (can be used multiple times).' +) # type: Callable[..., Option] + + +def check_list_path_option(options): + # type: (Values) -> None + if options.path and (options.user or options.local): + raise CommandError( + "Cannot combine '--path' with '--user' or '--local'" + ) + + ########## # groups # ########## diff --git a/src/pip/_internal/commands/freeze.py b/src/pip/_internal/commands/freeze.py index 47bd443a4..9fc5b0469 100644 --- a/src/pip/_internal/commands/freeze.py +++ b/src/pip/_internal/commands/freeze.py @@ -3,8 +3,8 @@ from __future__ import absolute_import import sys from pip._internal.cache import WheelCache +from pip._internal.cli import cmdoptions from pip._internal.cli.base_command import Command -from pip._internal.exceptions import CommandError from pip._internal.models.format_control import FormatControl from pip._internal.operations.freeze import freeze from pip._internal.utils.compat import stdlib_pkgs @@ -57,12 +57,7 @@ class FreezeCommand(Command): action='store_true', default=False, help='Only output packages installed in user-site.') - self.cmd_opts.add_option( - '--path', - dest='path', - action='append', - help='Restrict to the specified installation path for listing ' - 'packages (can be used multiple times).') + self.cmd_opts.add_option(cmdoptions.list_path()) self.cmd_opts.add_option( '--all', dest='freeze_all', @@ -84,10 +79,7 @@ class FreezeCommand(Command): if not options.freeze_all: skip.update(DEV_PKGS) - if options.path and (options.user or options.local): - raise CommandError( - "Cannot combine '--path' with '--user' or '--local'" - ) + cmdoptions.check_list_path_option(options) freeze_kwargs = dict( requirement=options.requirements, diff --git a/src/pip/_internal/commands/list.py b/src/pip/_internal/commands/list.py index 2fbc31687..add31c0bf 100644 --- a/src/pip/_internal/commands/list.py +++ b/src/pip/_internal/commands/list.py @@ -62,12 +62,7 @@ class ListCommand(Command): action='store_true', default=False, help='Only output packages installed in user-site.') - self.cmd_opts.add_option( - '--path', - dest='path', - action='append', - help='Restrict to the specified installation path for listing ' - 'packages (can be used multiple times).') + cmd_opts.add_option(cmdoptions.list_path()) cmd_opts.add_option( '--pre', action='store_true', @@ -131,10 +126,7 @@ class ListCommand(Command): raise CommandError( "Options --outdated and --uptodate cannot be combined.") - if options.path and (options.user or options.local): - raise CommandError( - "Cannot combine '--path' with '--user' or '--local'" - ) + cmdoptions.check_list_path_option(options) packages = get_installed_distributions( local_only=options.local,