Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell argument completer for the Azure CLI (az).
Register-ArgumentCompleter -CommandName 'az' -Native -ScriptBlock {
[OutputType([System.Collections.Generic.IEnumerable[System.Management.Automation.CompletionResult]])]
param(
[string] $commandName,
[string] $parameterName,
[string] $wordToComplete,
[System.Management.Automation.Language.CommandAst] $commandAst,
[System.Collections.IDictionary] $fakeBoundParameters
)
$fiveSpaces = ' ' * 5
$deprecatedRegex = [regex]::new('^[^\:]*\[Deprecated\][^\:]*:')
$nameRegex = [regex]::new('^\s*(\w+)')
$descriptionRegex = [regex]::new('^[^:]*:\s*(.*)$')
[string[]] $lines = $(az --help) 2> $null
$subgroupsLineIndex = [array]::IndexOf($lines, 'Subgroups:')
if ($subgroupsLineIndex -ge 0) {
$subgroups = [System.Collections.Generic.LinkedList[object]]::new()
$lineIndex = $subgroupsLineIndex + 1
while ($lines[$lineIndex]) {
$subgroupLines = [System.Collections.Generic.List[object]]::new(2)
do {
$subgroupLines.Add($lines[($lineIndex++)])
} while ($lines[$lineIndex] -and $lines[$lineIndex].StartsWith($fiveSpaces))
if ($deprecatedRegex.Match($subgroupLines[0]).IsMatch) {
continue
}
$subgroup = @{
Name = $nameRegex.Match($subgroupLines[0]).Groups[1].Value
Description = ([array] $subgroupLines).ForEach{ $descriptionRegex.Match($_).Groups[1].Value } -join ' '
}
$subgroups.Add($subgroup)
}
}
# TODO commands
# TODO arguments / options
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment