Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/9456.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Keep ``--version`` available only for pip and prevent its use in subcommands.
1 change: 0 additions & 1 deletion src/pip/_internal/cli/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,6 @@ def check_list_path_option(options: Values) -> None:
require_virtualenv,
python,
verbose,
version,
quiet,
log,
no_input,
Expand Down
1 change: 1 addition & 0 deletions src/pip/_internal/cli/main_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def create_main_parser() -> ConfigOptionParser:
parser.disable_interspersed_args()

parser.version = get_pip_version()
parser.add_option(cmdoptions.version())

# add the general options
gen_opts = cmdoptions.make_option_group(cmdoptions.general_group, parser)
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/test_base_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

from pip._internal.cli import base_command
from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import BROKEN_STDOUT, SUCCESS, VIRTUALENV_NOT_FOUND
from pip._internal.cli.status_codes import (
BROKEN_STDOUT,
SUCCESS,
UNKNOWN_ERROR,
VIRTUALENV_NOT_FOUND,
)
from pip._internal.commands import commands_dict, create_command
from pip._internal.utils import temp_dir
from pip._internal.utils.logging import BrokenStdoutLoggingError
Expand Down Expand Up @@ -256,3 +261,18 @@ def test_commands_ignore_require_virtualenv_is_explicit() -> None:
assert not command_class.ignore_require_venv == (
name in commands_that_require_venv
)


@pytest.mark.parametrize("command_name", commands_dict)
def test_commands_reject_version_option(
command_name: str,
capfd: pytest.CaptureFixture[str],
) -> None:
cmd = create_command(command_name)

with pytest.raises(SystemExit) as excinfo:
cmd.main(["--version"])
stderr = capfd.readouterr().err

assert excinfo.value.code == UNKNOWN_ERROR
assert "no such option: --version" in stderr
Loading