|
| 1 | +function Register-PSCommandHelperPrompt { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Registers a prompt wrapper that catches bash-style flag errors on aliased commands. |
| 5 | + .DESCRIPTION |
| 6 | + Commands like ls, rm, cp are aliased in PowerShell, so CommandNotFoundAction |
| 7 | + doesn't fire for them. But when users pass bash-style flags (ls -la, rm -rf), |
| 8 | + PowerShell errors on the unrecognized parameters. This prompt wrapper detects |
| 9 | + those errors and shows educational suggestions. |
| 10 | + #> |
| 11 | + [CmdletBinding()] |
| 12 | + param() |
| 13 | + |
| 14 | + # Prevent double-registration |
| 15 | + if ($script:OriginalPrompt) { |
| 16 | + Write-Verbose "PSCommandHelper prompt handler is already registered." |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + # Save the current prompt |
| 21 | + $script:OriginalPrompt = $function:prompt |
| 22 | + |
| 23 | + # Build the aliased-command lookup from the map |
| 24 | + $map = Get-BashToPowerShellMap |
| 25 | + $aliasedMap = @{} |
| 26 | + foreach ($entry in ($map | Where-Object { $_.Type -eq 'Aliased' })) { |
| 27 | + $baseCmd = ($entry.Bash -split '\s+')[0] |
| 28 | + if (-not $aliasedMap.ContainsKey($baseCmd)) { |
| 29 | + $aliasedMap[$baseCmd] = @() |
| 30 | + } |
| 31 | + $aliasedMap[$baseCmd] += $entry |
| 32 | + } |
| 33 | + $script:AliasedCommandMap = $aliasedMap |
| 34 | + $script:FormatFunc = Get-Command Format-Suggestion |
| 35 | + |
| 36 | + $function:global:prompt = { |
| 37 | + # Check if the last command failed |
| 38 | + if (-not $? -and $global:Error.Count -gt 0) { |
| 39 | + $lastErr = $global:Error[0] |
| 40 | + try { |
| 41 | + # Extract the command name from the error |
| 42 | + $cmdName = $null |
| 43 | + if ($lastErr.InvocationInfo -and $lastErr.InvocationInfo.MyCommand) { |
| 44 | + $cmdName = $lastErr.InvocationInfo.MyCommand.Name |
| 45 | + } |
| 46 | + elseif ($lastErr.CategoryInfo -and $lastErr.CategoryInfo.Activity) { |
| 47 | + $cmdName = $lastErr.CategoryInfo.Activity |
| 48 | + } |
| 49 | + |
| 50 | + if ($cmdName) { |
| 51 | + # Check if an alias resolves to a known command |
| 52 | + $alias = Get-Alias -Name $cmdName -ErrorAction SilentlyContinue |
| 53 | + $lookupName = if ($alias) { $cmdName } else { $null } |
| 54 | + |
| 55 | + # Also check if the failing command itself is in our aliased map |
| 56 | + if (-not $lookupName -and $script:AliasedCommandMap.ContainsKey($cmdName)) { |
| 57 | + $lookupName = $cmdName |
| 58 | + } |
| 59 | + |
| 60 | + if ($lookupName -and $script:AliasedCommandMap.ContainsKey($lookupName)) { |
| 61 | + $errString = $lastErr.ToString() |
| 62 | + # Only show for parameter-binding or argument errors (bash-style flags) |
| 63 | + $isParamError = $lastErr.Exception -is [System.Management.Automation.ParameterBindingException] -or |
| 64 | + $errString -match 'is not recognized as' -or |
| 65 | + $errString -match 'A positional parameter cannot be found' -or |
| 66 | + $errString -match 'Cannot find a parameter' |
| 67 | + |
| 68 | + if ($isParamError) { |
| 69 | + # Find the best matching entry (most specific first) |
| 70 | + $line = if ($lastErr.InvocationInfo) { $lastErr.InvocationInfo.Line.Trim() } else { $lookupName } |
| 71 | + $entries = $script:AliasedCommandMap[$lookupName] |
| 72 | + $bestMatch = $entries | Sort-Object { $_.Bash.Length } -Descending | |
| 73 | + Where-Object { $line -match [regex]::Escape($_.Bash) } | |
| 74 | + Select-Object -First 1 |
| 75 | + |
| 76 | + if (-not $bestMatch) { |
| 77 | + $bestMatch = $entries | Select-Object -First 1 |
| 78 | + } |
| 79 | + |
| 80 | + & $script:FormatFunc -Mapping $bestMatch -OriginalCommand $line |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + catch { |
| 86 | + # Silently ignore errors in the prompt handler |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + # Call the original prompt |
| 91 | + & $script:OriginalPrompt |
| 92 | + } |
| 93 | +} |
0 commit comments