Skip to content

Commit 3a36bc8

Browse files
AdmiringWormgep13
authored andcommitted
(#3709) Enhance ChocolateyTabExpansion with --order-by support
Add explicit support for the --order-by option in Chocolatey tab completion by introducing Get-ChocoOrderByOptions, which returns the canonical order-by values. Update the search command to accept an explicit --order-by parameter instead of a fixed --order-by-popularity. This change improves flexibility and accuracy of tab completion for the --order-by argument, aligning completions with Chocolatey's enum values and enabling users to discover valid sorting options interactively.
1 parent 2ebed65 commit 3a36bc8

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ $commandOptions = @{
5656
pin = "--name='' --version=''"
5757
push = "--api-key='' --source=''"
5858
rule = "--name=''"
59-
search = "--all-versions --approved-only --by-id-only --by-tag-only --cert='' --certpassword='' --detail --disable-repository-optimizations --download-cache-only --exact --id-only --id-starts-with --include-configured-sources --include-programs --not-broken --order-by-popularity --page='' --page-size='' --password='' --prerelease --source='' --user=''"
59+
search = "--all-versions --approved-only --by-id-only --by-tag-only --cert='' --certpassword='' --detail --disable-repository-optimizations --download-cache-only --exact --id-only --id-starts-with --include-configured-sources --include-programs --not-broken --order-by='' --order-by-popularity --page='' --page-size='' --password='' --prerelease --source='' --user=''"
6060
source = "--admin-only --allow-self-service --bypass-proxy --cert='' --certpassword='' --name='' --password='' --priority='' --source='' --user=''"
6161
support = ""
6262
template = "--name=''"
@@ -155,7 +155,7 @@ function script:chocoRemotePackages($filter) {
155155
if ($filter -and $filter.StartsWith(".")) {
156156
return;
157157
} #file search
158-
@('packages.config|') + @(& $script:choco search $filter --page='0' --page-size='30' -r --id-starts-with --order-by-popularity) |
158+
@('packages.config|') + @(& $script:choco search $filter --page='0' --page-size='30' -r --id-starts-with --order-by='popularity') |
159159
Where-Object { $_ -like "$filter*" } |
160160
ForEach-Object { $_.Split('|')[0] }
161161
}
@@ -166,6 +166,22 @@ function Get-AliasPattern($exe) {
166166
"($($aliases -join '|'))"
167167
}
168168

169+
function Get-ChocoOrderByOptions {
170+
<#
171+
.SYNOPSIS
172+
Returns the list of canonical --order-by values for Chocolatey.
173+
174+
.DESCRIPTION
175+
These values correspond to the distinct, non-aliased entries in the
176+
PackageOrder enum. They are sorted alphabetically and must be updated
177+
manually when the enum changes.
178+
179+
.OUTPUTS
180+
A string in the format "Id|LastPublished|Popularity|Title|Unsorted"
181+
#>
182+
return @("Id", "LastPublished", "Popularity", "Title", "Unsorted")
183+
}
184+
169185
function ChocolateyTabExpansion($lastBlock) {
170186
switch -regex ($lastBlock -replace "^$(Get-AliasPattern choco) ", "") {
171187

@@ -254,6 +270,13 @@ function ChocolateyTabExpansion($lastBlock) {
254270
chocoLocalPackagesUpgrade $matches['package']
255271
}
256272

273+
# Custom completion for --order-by values
274+
"^search.*--order-by='?(?<prefix>.*)'?$" {
275+
$prefix = $matches['prefix']
276+
return Get-ChocoOrderByOptions | Where-Object { $_ -like "$prefix*" } | ForEach-Object { "--order-by='$_'"}
277+
}
278+
279+
257280
# Handles more options after others
258281
"^(?<cmd>$($commandOptions.Keys -join '|'))(?<currentArguments>.*)\s+(?<op>\S*)$" {
259282
chocoCmdOperations $commandOptions $matches['cmd'] $matches['op'] $matches['currentArguments']

0 commit comments

Comments
 (0)