modules/ModularAWSPowerShell/AWS.Tools.Common.Completers.psm1 (95 lines of code) (raw):
# Auto-generated argument completers for parameters of SDK ConstantClass-derived type used in cmdlets.
# Do not modify this file; it may be overwritten during version upgrades.
$psMajorVersion = $PSVersionTable.PSVersion.Major
if ($psMajorVersion -eq 2)
{
Write-Verbose "Dynamic argument completion not supported in PowerShell version 2; skipping load."
return
}
# PowerShell's native Register-ArgumentCompleter cmdlet is available on v5.0 or higher. For lower
# version, we can use the version in the TabExpansion++ module if installed.
$registrationCmdletAvailable = ($psMajorVersion -ge 5) -Or !((Get-Command Register-ArgumentCompleter -ea Ignore) -eq $null)
# internal function to perform the registration using either cmdlet or manipulation
# of the options table
function _awsArgumentCompleterRegistration()
{
param
(
[scriptblock]$scriptBlock,
[hashtable]$param2CmdletsMap
)
if ($registrationCmdletAvailable)
{
foreach ($paramName in $param2CmdletsMap.Keys)
{
$args = @{
"ScriptBlock" = $scriptBlock
"Parameter" = $paramName
}
$cmdletNames = $param2CmdletsMap[$paramName]
if ($cmdletNames -And $cmdletNames.Length -gt 0)
{
$args["Command"] = $cmdletNames
}
Register-ArgumentCompleter @args
}
}
else
{
if (-not $global:options) { $global:options = @{ CustomArgumentCompleters = @{ }; NativeArgumentCompleters = @{ } } }
foreach ($paramName in $param2CmdletsMap.Keys)
{
$cmdletNames = $param2CmdletsMap[$paramName]
if ($cmdletNames -And $cmdletNames.Length -gt 0)
{
foreach ($cn in $cmdletNames)
{
$fqn = [string]::Concat($cn, ":", $paramName)
$global:options['CustomArgumentCompleters'][$fqn] = $scriptBlock
}
}
else
{
$global:options['CustomArgumentCompleters'][$paramName] = $scriptBlock
}
}
$function:tabexpansion2 = $function:tabexpansion2 -replace 'End\r\n{', 'End { if ($null -ne $options) { $options += $global:options} else {$options = $global:options}'
}
}
# To allow for same-name parameters of different ConstantClass-derived types
# each completer function checks on command name concatenated with parameter name.
# Additionally, the standard code pattern for completers is to pipe through
# sort-object after filtering against $wordToComplete but we omit this as our members
# are already sorted.
$AWS_RegionCompleter = {
param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$regionHash = @{ }
# Similar to Get-AWSRegion
$regions = [Amazon.RegionEndpoint]::EnumerableAllRegions
foreach ($r in $regions)
{
$regionHash.Add($r.SystemName, $r.DisplayName)
}
$regionHash.Keys |
Sort-Object |
Where-Object { $_ -like "$wordToComplete*" } |
ForEach-Object {
New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $regionHash[$_]
}
}
_awsArgumentCompleterRegistration $AWS_RegionCompleter @{ "Region"=@() }
$AWS_ProfileNameCompleter = {
param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
# allow for new user with no profiles set up yet
$profiles = Get-AWSCredentials -ListProfileDetail | select -expandproperty ProfileName
if ($profiles)
{
$profiles |
Sort-Object |
Where-Object { $_ -like "$wordToComplete*" } |
ForEach-Object {
New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_
}
}
}
_awsArgumentCompleterRegistration $AWS_ProfileNameCompleter @{ "ProfileName"=@() }