Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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.
3 changes: 3 additions & 0 deletions src/pip/_internal/cli/base_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ def _main(self, args: list[str]) -> int:
)
options.cache_dir = None

if options.version:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even with this check, --version still shows up in the subcommand's --help.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it stays in subcommands since --version lives in general_group. I already tried removing it from there for the subcommand, but that broke some tests. Maybe there are other options

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.
found a way to suppress the hint without removing it :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that feels a bit off to me.

current implementation adds special-case handling just for --version. If possible, wouldn't it be better to avoid this kind of special case?

--version is only meaningful on the top-level pip command. So rather than keeping it in general_group, defining and handling it only in the top-level parser seems like a more natural structure to me.

That way:

  • --version no longer shows up in pip <subcommand> --help
  • there's no need for separate special-case handling anymore
  • and future changes related to --version can be handled consistently at the parser level.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already tried removing it from there for the subcommand, but that broke some tests. Maybe there are other options

My guess is the broken tests came from removing version from general_group. pip --version itself could no longer be handled. Looking at the code where pip processes the top-level command might help.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that feels a bit off to me.

current implementation adds special-case handling just for --version. If possible, wouldn't it be better to avoid this kind of special case?

--version is only meaningful on the top-level pip command. So rather than keeping it in general_group, defining and handling it only in the top-level parser seems like a more natural structure to me.

That way:

* `--version` no longer shows up in `pip <subcommand> --help`

* there's no need for separate special-case handling anymore

* and future changes related to `--version` can be handled consistently at the parser level.

Damn, yeah, agreed. Much better this way.
Found how to do it. I ll remove it from general_options and add it as an option for pip, instead

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

self.parser.error("no such option: --version")

return self._run_wrapper(level_number, options, args)

def handler_map(self) -> dict[str, Callable[[Values, list[str]], None]]:
Expand Down
Loading