PowerShell:如何对代理函数进行自动补全?

问题描述 投票:0回答:1

我有一个类似的功能:

function helf {
    Get-Help -Full @args
}

这会导致

helf -<tab>
的自动补全功能不再起作用:

> TabExpansion2 "helf -" | select -Expand CompletionMatches
[ NO RESULT ]

我注意到,

oss
实际上不是
Out-String
的别名,而是
Out-String -Stream
的代理函数,确实具有工作自动完成功能:

> gcm oss

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        oss

> TabExpansion2 "Out-String -Stream -" | select -Expand CompletionMatches

CompletionText       ListItemText           ResultType ToolTip
--------------       ------------           ---------- -------
-Width               Width               ParameterName [int] Width
-InputObject         InputObject         ParameterName [psobject] InputObject
-Verbose             Verbose             ParameterName [switch] Verbose
-Debug               Debug               ParameterName [switch] Debug
-ErrorAction         ErrorAction         ParameterName [ActionPreference] ErrorAction
-WarningAction       WarningAction       ParameterName [ActionPreference] WarningAction
-InformationAction   InformationAction   ParameterName [ActionPreference] InformationAction
-ProgressAction      ProgressAction      ParameterName [ActionPreference] ProgressAction
-ErrorVariable       ErrorVariable       ParameterName [string] ErrorVariable
-WarningVariable     WarningVariable     ParameterName [string] WarningVariable
-InformationVariable InformationVariable ParameterName [string] InformationVariable
-OutVariable         OutVariable         ParameterName [string] OutVariable
-OutBuffer           OutBuffer           ParameterName [int] OutBuffer
-PipelineVariable    PipelineVariable    ParameterName [string] PipelineVariable

> TabExpansion2 "oss -" | select -Expand CompletionMatches

CompletionText       ListItemText           ResultType ToolTip
--------------       ------------           ---------- -------
-Width               Width               ParameterName [int] Width
-InputObject         InputObject         ParameterName [psobject] InputObject
-Verbose             Verbose             ParameterName [switch] Verbose
-Debug               Debug               ParameterName [switch] Debug
-ErrorAction         ErrorAction         ParameterName [ActionPreference] ErrorAction
-WarningAction       WarningAction       ParameterName [ActionPreference] WarningAction
-InformationAction   InformationAction   ParameterName [ActionPreference] InformationAction
-ProgressAction      ProgressAction      ParameterName [ActionPreference] ProgressAction
-ErrorVariable       ErrorVariable       ParameterName [string] ErrorVariable
-WarningVariable     WarningVariable     ParameterName [string] WarningVariable
-InformationVariable InformationVariable ParameterName [string] InformationVariable
-OutVariable         OutVariable         ParameterName [string] OutVariable
-OutBuffer           OutBuffer           ParameterName [int] OutBuffer
-PipelineVariable    PipelineVariable    ParameterName [string] PipelineVariable

问题是:我如何为我自己的代理函数完成这个任务?

powershell
1个回答
0
投票

制表符补全不起作用,因为您的

hef
函数没有任何参数。如果你想创建一个真正的代理函数,你需要使用
ProxyCommand.Create
或添加到
hef
那些你认为需要的参数。

所以:

[System.Management.Automation.ProxyCommand]::Create((Get-Command Get-Help))

要获取完全自动生成的代理函数体或:

[System.Management.Automation.ProxyCommand]::GetParamBlock((Get-Command Get-Help))

获取命令的

param
块并从那里选择您认为需要的参数。

© www.soinside.com 2019 - 2024. All rights reserved.