经典,我用正则表达式。
例如,
function TabExpansion {
param($line, $lastWord)
if ($line -match '(-(\w+))\s+([^-]*$)')
{
### Resolve Command name & parameter name
$_param = $matches[2] + '*'
$_opt = $Matches[3].Split(" ,")[-1] + '*'
$_base = $Matches[3].Substring(0,$Matches[3].Length-$Matches[3].Split(" ,")[-1].length)
$_cmdlet = [regex]::Split($line, '[|;=]')[-1]
if ($_cmdlet -match '\{([^\{\}]*)$')
{
$_cmdlet = $matches[1]
}
if ($_cmdlet -match '\(([^()]*)$')
{
$_cmdlet = $matches[1]
}
$_cmdlet = $_cmdlet.Trim().Split()[0]
$_cmdlet = @(Get-Command -type 'Cmdlet,Alias,Function,Filter,ExternalScript' $_cmdlet)[0]
while ($_cmdlet.CommandType -eq 'alias')
{
$_cmdlet = @(Get-Command -type 'Cmdlet,Alias,Function,Filter,ExternalScript' $_cmdlet.Definition)[0]
}
### Currently target is Get-Alias & "-Name" parameter
if ("Get-Alias" -eq $_cmdlet.Name -and "Name" -like $_param)
{
Get-Alias -Name $_opt | % { $_.Name } | sort | % { $_base + ($_ -replace '\s','` ') }
break;
}
}
}
参考 http://gallery.technet.microsoft.com/scriptcenter/005d8bc7-5163-4a25-ad0d-25cffa90faf5
辣妹-git的重命名TabExpansion到TabExpansionBackup在GitTabExpansion.ps1。
当完成与git命令不匹配时,posh-git的redifined TabExpansion调用原始TabExpansion(TabExpansionBackup)。
所以你所要做的就是重新定义TabExpansionBackup。
(cat。\ GitTabExpansion.ps1 | select -last 18)
============================== GitTabExpansion名为.ps1 ==============================
if (Test-Path Function:\TabExpansion) {
Rename-Item Function:\TabExpansion TabExpansionBackup
}
function TabExpansion($line, $lastWord) {
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
switch -regex ($lastBlock) {
# Execute git tab completion for all git-related commands
"^$(Get-AliasPattern git) (.*)" { GitTabExpansion $lastBlock }
"^$(Get-AliasPattern tgit) (.*)" { GitTabExpansion $lastBlock }
# Fall back on existing tab expansion
default { if (Test-Path Function:\TabExpansionBackup) { TabExpansionBackup $line $lastWord } }
}
}
=========== ================================================== ==================
重新定义TabExpansionBackup(原始TabExpansion)
function TabExpansionBackup {
...
### Resolve Command name & parameter name
...
### Currently target is Get-Alias & "-Name" parameter
...
}
你读过这里:http://www.powertheshell.com/dynamicargumentcompletion/ – 2013-02-13 06:55:03
不,我没有找到。非常丰富的 – 2013-02-13 10:57:22
你也可以看一下http://powertab.codeplex。com /这是一个来自PowerShell v.2的'动态intellissense',但我也用它在3.0非常好 – 2013-02-13 10:59:28