functions/tabexpansion/Register-PSFTeppArgumentCompleter.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function Register-PSFTeppArgumentCompleter
{
    <#
        .SYNOPSIS
            Registers a parameter for a prestored Tepp.
         
        .DESCRIPTION
            Registers a parameter for a prestored Tepp.
            This function allows easily registering a function's parameter for Tepp in the function-file, rather than in a centralized location.
         
        .PARAMETER Command
            Name of the command whose parameter should receive Tepp.
         
        .PARAMETER Parameter
            Name of the parameter that should be Tepp'ed.
         
        .PARAMETER Name
            Name of the Tepp Completioner to use.
   Use the same name as was assigned in Register-PSFTeppScriptblock (which needs to be called first).
         
        .EXAMPLE
            Register-PSFTeppArgumentCompleter -Command Get-Test -Parameter Example -Name MyModule.Example
     
            Registers the parameter 'Example' of the command 'Get-Test' to receive the tab completion registered to 'MyModule.Example'
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [string[]]
        $Command,
        
        [Parameter(Mandatory = $true)]
        [string[]]
        $Parameter,
        
        [Parameter(Mandatory = $true)]
        [string]
        $Name
    )
    
    if (($PSVersionTable["PSVersion"].Major -lt 5) -and (-not (Get-Item function:Register-ArgumentCompleter -ErrorAction Ignore)))
    {
        return
    }
    
    foreach ($Param in $Parameter)
    {
        $scriptBlock = [PSFramework.TabExpansion.TabExpansionHost]::Scripts[$Name.ToLower()].ScriptBlock
        Register-ArgumentCompleter -CommandName $Command -ParameterName $Param -ScriptBlock $scriptBlock
    }
}