45 lines
2.5 KiB
PowerShell
45 lines
2.5 KiB
PowerShell
|
|
using namespace System.Management.Automation
|
|
using namespace System.Management.Automation.Language
|
|
|
|
Register-ArgumentCompleter -Native -CommandName 'wc' -ScriptBlock {
|
|
param($wordToComplete, $commandAst, $cursorPosition)
|
|
|
|
$commandElements = $commandAst.CommandElements
|
|
$command = @(
|
|
'wc'
|
|
for ($i = 1; $i -lt $commandElements.Count; $i++) {
|
|
$element = $commandElements[$i]
|
|
if ($element -isnot [StringConstantExpressionAst] -or
|
|
$element.StringConstantType -ne [StringConstantType]::BareWord -or
|
|
$element.Value.StartsWith('-') -or
|
|
$element.Value -eq $wordToComplete) {
|
|
break
|
|
}
|
|
$element.Value
|
|
}) -join ';'
|
|
|
|
$completions = @(switch ($command) {
|
|
'wc' {
|
|
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'Print the byte counts')
|
|
[CompletionResult]::new('--bytes', 'bytes', [CompletionResultType]::ParameterName, 'Print the byte counts')
|
|
[CompletionResult]::new('-m', 'm', [CompletionResultType]::ParameterName, 'Print the character counts')
|
|
[CompletionResult]::new('--chars', 'chars', [CompletionResultType]::ParameterName, 'Print the character counts')
|
|
[CompletionResult]::new('-l', 'l', [CompletionResultType]::ParameterName, 'Print the line counts')
|
|
[CompletionResult]::new('--lines', 'lines', [CompletionResultType]::ParameterName, 'Print the line counts')
|
|
[CompletionResult]::new('-L', 'L', [CompletionResultType]::ParameterName, 'Print the maximum display width')
|
|
[CompletionResult]::new('--max-line-length', 'max-line-length', [CompletionResultType]::ParameterName, 'Print the maximum display width')
|
|
[CompletionResult]::new('-w', 'w', [CompletionResultType]::ParameterName, 'Print the word counts')
|
|
[CompletionResult]::new('--words', 'words', [CompletionResultType]::ParameterName, 'Print the word counts')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
|
|
break
|
|
}
|
|
})
|
|
|
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
Sort-Object -Property ListItemText
|
|
}
|