Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag for 'pip index versions' to output only latest version #10985

Closed
wants to merge 2 commits into from
Closed
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
33 changes: 30 additions & 3 deletions src/pip/_internal/commands/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ def add_options(self) -> None:
self.cmd_opts.add_option(cmdoptions.pre())
self.cmd_opts.add_option(cmdoptions.no_binary())
self.cmd_opts.add_option(cmdoptions.only_binary())
self.cmd_opts.add_option(
"--latest",
dest="latest",
action="store_true",
default=False,
help="Print only the latest version of the given package.",
)
Comment on lines +38 to +44
Copy link
Member

Choose a reason for hiding this comment

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

If we drop this on for now I think this can go in. But the show only latest logic would need much more discussion.

self.cmd_opts.add_option(
"--format",
action="store",
dest="list_format",
default="human",
choices=("human", "json"),
help="Select the output format among: human (default) or json",
)

index_opts = cmdoptions.make_option_group(
cmdoptions.index_group,
Expand Down Expand Up @@ -131,9 +146,21 @@ def get_available_package_versions(self, options: Values, args: List[Any]) -> No
"No matching distribution found for {}".format(query)
)

if options.latest:
# Print only the last version in the list.
write_output(str(max(versions)))
return

if options.list_format == 'human':
formatted_versions = [str(ver) for ver in sorted(versions, reverse=True)]
latest = formatted_versions[0]

write_output("{} ({})".format(query, latest))
write_output("Available versions: {}".format(", ".join(formatted_versions)))
print_dist_installation_info(query, latest)
write_output("{} ({})".format(query, latest))
write_output("Available versions: {}".format(", ".join(formatted_versions)))
print_dist_installation_info(query, latest)
elif options.list_format == 'json':
json_output = {
'name': query,
'versions': [str(version) for version in sorted(versions)]
}
write_output(json_output)