-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegister-PSCommandHelperPrompt.ps1
More file actions
154 lines (138 loc) · 7.22 KB
/
Register-PSCommandHelperPrompt.ps1
File metadata and controls
154 lines (138 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function Register-PSCommandHelperPrompt {
<#
.SYNOPSIS
Registers a prompt wrapper that catches bash-style flag errors on aliased commands.
.DESCRIPTION
Commands like ls, rm, cp are aliased in PowerShell, so CommandNotFoundAction
doesn't fire for them. But when users pass bash-style flags (ls -la, rm -rf),
PowerShell errors on the unrecognized parameters. This prompt wrapper detects
those errors and shows educational suggestions.
#>
[CmdletBinding()]
param()
# Prevent double-registration
if ($script:OriginalPrompt) {
Write-Verbose "PSCommandHelper prompt handler is already registered."
return
}
# Save the current prompt
$script:OriginalPrompt = $function:prompt
# Build the aliased-command lookup from the map
$map = Get-BashToPowerShellMap
$aliasedMap = @{}
$aliasedCmdletMap = @{}
foreach ($entry in ($map | Where-Object { $_.Type -eq 'Aliased' })) {
$baseCmd = ($entry.Bash -split '\s+')[0]
if (-not $aliasedMap.ContainsKey($baseCmd)) {
$aliasedMap[$baseCmd] = @()
}
$aliasedMap[$baseCmd] += $entry
$psCmdlet = ($entry.PowerShell -split '\s+')[0]
if ($psCmdlet) {
if (-not $aliasedCmdletMap.ContainsKey($psCmdlet)) {
$aliasedCmdletMap[$psCmdlet] = @()
}
$aliasedCmdletMap[$psCmdlet] += $entry
}
}
$script:AliasedCommandMap = $aliasedMap
$script:AliasedCmdletMap = $aliasedCmdletMap
$script:FormatFunc = Get-Command Format-Suggestion
$function:global:prompt = {
# Check if the last command failed
if (-not $? -and $global:Error.Count -gt 0) {
$lastErr = $global:Error[0]
try {
# Extract the command name from the error
$cmdName = $null
if ($lastErr.InvocationInfo -and $lastErr.InvocationInfo.MyCommand) {
$cmdName = $lastErr.InvocationInfo.MyCommand.Name
}
elseif ($lastErr.CategoryInfo -and $lastErr.CategoryInfo.Activity) {
$cmdName = $lastErr.CategoryInfo.Activity
}
if ($cmdName) {
# Check if an alias resolves to a known command
$alias = Get-Alias -Name $cmdName -ErrorAction SilentlyContinue
$lookupName = if ($alias) { $cmdName } else { $null }
# Also check if the failing command itself is in our aliased map
if (-not $lookupName -and $script:AliasedCommandMap.ContainsKey($cmdName)) {
$lookupName = $cmdName
}
# Reverse lookup: $cmdName may be the resolved cmdlet (e.g. Remove-Item)
# Find aliases that point TO this cmdlet (e.g. rm → Remove-Item)
if (-not $lookupName) {
$reverseAliases = Get-Alias -Definition $cmdName -ErrorAction SilentlyContinue
foreach ($ra in $reverseAliases) {
if ($script:AliasedCommandMap.ContainsKey($ra.Name)) {
$lookupName = $ra.Name
break
}
}
}
$entries = $null
if ($lookupName -and $script:AliasedCommandMap.ContainsKey($lookupName)) {
$entries = $script:AliasedCommandMap[$lookupName]
}
elseif ($script:AliasedCmdletMap -and $script:AliasedCmdletMap.ContainsKey($cmdName)) {
$entries = $script:AliasedCmdletMap[$cmdName]
$lookupName = $cmdName
}
if ($entries) {
$errString = $lastErr.ToString()
# Only show for parameter-binding or argument errors (bash-style flags)
$isParamError = $lastErr.Exception -is [System.Management.Automation.ParameterBindingException] -or
$errString -match 'is not recognized as' -or
$errString -match 'A positional parameter cannot be found' -or
$errString -match 'Cannot find a parameter'
if ($isParamError) {
# Find the best matching entry using flag-aware comparison
$line = if ($lastErr.InvocationInfo) { $lastErr.InvocationInfo.Line.Trim() } else { $lookupName }
# Extract and normalize flags from the typed line
$lineFlags = [System.Collections.Generic.HashSet[char]]::new()
$lineParts = ($line -split '\s+') | Select-Object -Skip 1
foreach ($part in $lineParts) {
if ($part -match '^-([A-Za-z0-9]+)$') {
foreach ($ch in $Matches[1].ToCharArray()) { [void]$lineFlags.Add($ch) }
}
}
$bestMatch = $null
$bestScore = -1
foreach ($entry in $entries) {
$entryFlags = [System.Collections.Generic.HashSet[char]]::new()
$entryParts = ($entry.Bash -split '\s+') | Select-Object -Skip 1
foreach ($part in $entryParts) {
if ($part -match '^-([A-Za-z0-9]+)$') {
foreach ($ch in $Matches[1].ToCharArray()) { [void]$entryFlags.Add($ch) }
}
}
# Score: entry flags must be a subset of typed flags; more flags = better match
if ($entryFlags.Count -gt 0 -and $entryFlags.IsSubsetOf($lineFlags)) {
if ($entryFlags.Count -gt $bestScore) {
$bestScore = $entryFlags.Count
$bestMatch = $entry
}
}
}
# Fallback: substring match or base command
if (-not $bestMatch) {
$bestMatch = $entries | Sort-Object { $_.Bash.Length } -Descending |
Where-Object { $line -match [regex]::Escape($_.Bash) } |
Select-Object -First 1
}
if (-not $bestMatch) {
$bestMatch = $entries | Select-Object -First 1
}
& $script:FormatFunc -Mapping $bestMatch -OriginalCommand $line
}
}
}
}
catch {
# Silently ignore errors in the prompt handler
}
}
# Call the original prompt
& $script:OriginalPrompt
}
}