forked from lzybkr/TabExpansionPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrosoft.PowerShell.Management.ArgumentCompleters.ps1
105 lines (94 loc) · 2.95 KB
/
Microsoft.PowerShell.Management.ArgumentCompleters.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
#
# .SYNOPSIS
#
# Complete the -Namespace argument to Wmi cmdlets (similiar to buil-in cim cmdlets namespace completion support)
#
function WmiNamespaceCompleter
{
[ArgumentCompleter(
Parameter = 'Namespace',
Command = {Get-CommandWithParameter -Noun wmi* -ParameterName Namespace},
Description = 'Complete the -Namespace argument to Wmi cmdlets. For example: Get-WmiObject -Namespace <TAB>'
)]
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$nsParent='root'
if ($wordToComplete)
{
[int]$delimiter = $wordToComplete.LastIndexOfAny([char[]]('\','/'))
if($delimiter -ne -1)
{
$nsParent = $wordToComplete.Substring(0,$delimiter)
$nsLeaf = $wordToComplete.Substring($delimiter+1)
}
}
Microsoft.PowerShell.Management\Get-WmiObject -Class __NAMESPACE -Namespace $nsParent | Where-Object Name -like $nsLeaf* | Sort-Object Name | ForEach-Object {
$namespace = '{0}/{1}' -f $nsParent.Replace('\','/'),$_.Name
New-CompletionResult $namespace $namespace
}
}
#
# .SYNOPSIS
#
# Complete the -Attributes argument to Get-ChildItem
#
function DirAttributesParameterNameCompletion
{
[ArgumentCompleter(
Parameter = "Attributes",
Command = "Get-ChildItem",
Description = @"
Complete file attributes like Hidden or ReadOnly, for example:
Get-ChildItem -Attributes <TAB>
"@)]
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
[System.IO.FileAttributes].GetFields('Public,Static').Name |
Where-Object { $_ -like "$wordToComplete*" } |
Sort-Object |
ForEach-Object {
# TODO - use xml docs for tooltip
New-CompletionResult $_
}
}
#
# .SYNOPSIS
#
# Complete the -ItemType argument to New-Item
#
function NewItemItemTypeCompletion
{
[ArgumentCompleter(
Parameter = 'ItemType',
Command = 'New-Item',
Description = @'
Complete item types (in FileSystem/ ActiveDirectory), for example:
New-Item -ItemType <TAB>
'@)]
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$pathProvided = $fakeBoundParameter['Path']
if($pathProvided)
{
$resolvedPath = Resolve-Path -Path $pathProvided
}
else
{
$resolvedPath = $PWD
}
$completionSet = switch ($resolvedPath.Provider.Name) {
FileSystem {
Write-Output File, Directory
}
ActiveDirectory {
Write-Output User, Group, OrganizationalUnit, Container, Computer
}
Default {
# TODO - other providers, check if AD is complete (for useful stuff at least).
$null
}
}
$completionSet |
Where-Object { $_ -like "$wordToComplete*" } |
Sort-Object |
ForEach-Object {
New-CompletionResult $_ -ToolTip "Create $_"
}
}