-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2_ArgumentCompleterScript.ps1
118 lines (95 loc) · 2.6 KB
/
2_ArgumentCompleterScript.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#region DPS
throw "Hey, Dory! Forgot to use F8?"
#endregion
#region Any version of PowerShell
New-Module -Name FakeModule {
function Test-Foo {
param (
[String]$Bar
)
}
if (Get-Command -Name Register-ArgumentCompleter -ErrorAction SilentlyContinue) {
Register-ArgumentCompleter -CommandName Test-Foo -ParameterName Bar -ScriptBlock {
$wordToComplete = $args[2]
'One', 'Two', 'Three' |
Where-Object {
$_ -like "$wordToComplete*"
} |
ForEach-Object {
[Management.Automation.CompletionResult]::new($_)
}
}
}
}
#endregion
#region Version 5+
#region Script block in ArgumentCompleter
function Get-Foo {
param (
[ArgumentCompleter(
{
$wordToComplete = $args[2]
'One', 'Two', 'Three' |
Where-Object {
$_ -like "$wordToComplete*"
} |
ForEach-Object {
[Management.Automation.CompletionResult]::new($_)
}
}
)]
[String]$Bar
)
"$Bar"
}
#endregion
#region Function shared between commands
function Get-PropertyCompleter {
param (
[String]$CommandName,
[String]$ParameterName,
[String]$WordToComplete,
[Management.Automation.Language.CommandAst]$CommandAst,
[hashtable]$FakeBoundParameters
)
$splat = @{}
switch ($ParameterName) {
Speaker {
if ($FakeBoundParameters.Contains('Title')) {
$splat.Title = $FakeBoundParameters['Title']
}
}
Title {
if ($FakeBoundParameters.Contains('Speaker')) {
$splat.Speaker = $FakeBoundParameters['Speaker']
}
}
}
Get-ConfEUSession @splat |
Where-Object $ParameterName -Like "$WordToComplete*" |
ForEach-Object $ParameterName |
ForEach-Object {
New-CompletionResult -CompletionText $_
}
}
function Get-ConfEUSession {
param (
[ArgumentCompleter(
{ Get-PropertyCompleter @args }
)]
[String]$Speaker = '*',
[ArgumentCompleter(
{ Get-PropertyCompleter @args }
)]
[String]$Title = '*',
[String]$Abstract = '*'
)
# Implementation...
}
#endregion
#region functions in the module
Import-Module $pwd\PSConfEU.psm1"
Import-Module $pwd\PSConfEU-Tab.psm1" -Force
psEdit $pwd\*.psm1
#endregion
#endregion