|
| 1 | +# Note: This works for both Windows PowerShell 5.1 and also PowerShell 7 (Core). |
| 2 | +# But beware that in Windows PowerShell 5.1, it has issues with completing args if they start with '-'. |
| 3 | +# For more information about the bug, see: https://github.com/PowerShell/PowerShell/issues/2912 |
| 4 | +# In PowerShell 7+, it should work correctly. |
| 5 | +Register-ArgumentCompleter -Native -CommandName 'dog' -ScriptBlock { |
| 6 | + param($wordToComplete, $commandAst, $cursorPosition) |
| 7 | + |
| 8 | + [string]$argsString = $commandAst.ToString() |
| 9 | + |
| 10 | + # skip the "dog", split the args afterwards as array |
| 11 | + [string[]]$argsArray = $argsString.Split([char[]]@(' ', '=')) | Select-Object -Skip 1 |
| 12 | + if ($argsArray -eq $null) { $argsArray = @() } |
| 13 | + |
| 14 | + # detect if starting a new arg (aka ending with space and asking for a completion) |
| 15 | + [bool]$isNewArg = $cursorPosition -gt $argsString.Length |
| 16 | + if ($isNewArg) { |
| 17 | + # if writing a new arg, add empty arg so that current and previous would be shifted |
| 18 | + $argsArray += '' |
| 19 | + } |
| 20 | + |
| 21 | + # get current arg (empty if starting new) |
| 22 | + [string]$currentArg = $argsArray[-1] |
| 23 | + if ([string]::IsNullOrEmpty($currentArg)) { |
| 24 | + $currentArg = '' |
| 25 | + } |
| 26 | + |
| 27 | + # get previous arg |
| 28 | + [string]$previousArg = $argsArray[-2] |
| 29 | + if ([string]::IsNullOrEmpty($previousArg)) { |
| 30 | + $previousArg = '' |
| 31 | + } |
| 32 | + |
| 33 | + [string[]]$dnsTypeValues = @('A', 'AAAA', 'CAA', 'CNAME', 'HINFO', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT') |
| 34 | + |
| 35 | + [string[]]$completions = @() |
| 36 | + [bool]$isOptionValue = $argsString.EndsWith('=') |
| 37 | + |
| 38 | + # complete option value |
| 39 | + switch -Regex ($previousArg) { |
| 40 | + '^(-q|--query)' { $isOptionValue = $true } |
| 41 | + '^(-t|--type)' { $isOptionValue = $true; $completions += $dnsTypeValues } |
| 42 | + '^(-n|--nameserver)' { $isOptionValue = $true } |
| 43 | + '^(--class)' { $isOptionValue = $true; $completions += @('IN', 'CH', 'HS') } |
| 44 | + '^(--edns)' { $isOptionValue = $true; $completions += @('disable', 'hide', 'show') } |
| 45 | + '^(--txid)' { $isOptionValue = $true } |
| 46 | + '^(-Z)' { $isOptionValue = $true; $completions += @('aa', 'ad', 'bufsize=', 'cd') } |
| 47 | + '^(--color|--colour)' { $isOptionValue = $true; $completions += @('always', 'automatic', 'never') } |
| 48 | + } |
| 49 | + |
| 50 | + # detect whether to complete option value |
| 51 | + if ($isOptionValue) { |
| 52 | + if (!$isNewArg) { |
| 53 | + # if using =, complete including the option name and = |
| 54 | + $completions = $completions | ForEach-Object { "$previousArg=$_" } |
| 55 | + } |
| 56 | + } |
| 57 | + else { |
| 58 | + # if not completing option value, offer DNS type values first |
| 59 | + $completions += $dnsTypeValues |
| 60 | + |
| 61 | + # complete option name |
| 62 | + [string[]]$allOptions = @( |
| 63 | + '-q', '--query', |
| 64 | + '-t', '--type', |
| 65 | + '-n', '--nameserver', |
| 66 | + '--class', |
| 67 | + '--edns', |
| 68 | + '--txid', |
| 69 | + '-Z', |
| 70 | + '-U', '--udp', |
| 71 | + '-T', '--tcp', |
| 72 | + '-S', '--tls', |
| 73 | + '-H', '--https', |
| 74 | + '-1', '--short', |
| 75 | + '-J', '--json', |
| 76 | + '--color', '--colour', |
| 77 | + '--seconds', |
| 78 | + '--time', |
| 79 | + '-?', '--help', |
| 80 | + '-v', '--version' |
| 81 | + ) | Sort-Object |
| 82 | + |
| 83 | + $completions += $allOptions |
| 84 | + } |
| 85 | + |
| 86 | + if ($completions.Count -gt 0) { |
| 87 | + # narrow down completions by like* matching |
| 88 | + return $completions -like "$currentArg*" |
| 89 | + } |
| 90 | +} |
0 commit comments