Powershell 是否为Cmdlet添加自定义参数完成符?

Powershell 是否为Cmdlet添加自定义参数完成符?,powershell,cmdlet,chocolatey,Powershell,Cmdlet,Chocolatey,如何将动态参数选项卡完成添加到PowerShell Cmdlet 当我键入此项并点击选项卡时,我希望它完成选项卡 PM> Paket-Add -NuGet FSharp.Co 以下是我想在本例中使用的值: PM> Paket-FindPackages -SearchText FSharp.Co FSharp.Core FSharp.Core.3 FSharp.Configuration FSharp.Core.Fluent-3.1 FSharp.Core.Fluent-4.0 FS

如何将动态参数选项卡完成添加到PowerShell Cmdlet

当我键入此项并点击
选项卡时,我希望它完成选项卡

PM> Paket-Add -NuGet FSharp.Co
以下是我想在本例中使用的值:

PM> Paket-FindPackages -SearchText FSharp.Co
FSharp.Core
FSharp.Core.3
FSharp.Configuration
FSharp.Core.Fluent-3.1
FSharp.Core.Fluent-4.0
FSharp.Compiler.Tools
FSharp.Compatibility.Scala
FSharp.Compatibility.OCaml
FSharp.Compiler.CodeDom
FSharp.Compiler.Service
FSharp.Control.Reactive
FSharp.Compatibility.Haskell
FSharp.Compatibility.OCaml.Numerics
FSharp.Compatibility.OCaml.Format
FSharp.Compatibility.OCaml.System
FSharp.Collections.ParallelSeq
FSharp.Compatibility.StandardML
FSharp.Compatibility.OCaml.LexYacc
FSharp.Control.AsyncSeq
我发现它提供了一些有用的链接,并说我应该运行
getcontent函数:TabExpansion2

看起来需要实施。我想我在什么地方读到过,有一个
哈希表
,它包含了函数的命令。如果是,它在哪里?如何安装自定义的?我用巧克力分发。这是你的电话号码

更新2015-06-20:

我最终让它与这里的代码一起工作:

完成符参数名称很重要。重命名它们将使其无法工作。

这些被称为

下面的示例显示了一个带有标准 名为Name和Path的参数,以及可选的动态参数 命名为DP1。DP1参数位于PSet1参数集中,具有 Int32的类型。DP1参数在Sample函数中可用 仅当Path参数的值包含“HKLM:”时,表示 它正在HKEY_LOCAL_机器注册表驱动器中使用

函数获取示例{
[CmdletBinding()]
参数([String]$Name,[String]$Path)
动态存储器
{
如果($path-match.“*HKLM.*”)
{
$attributes=新对象System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName=“\uu所有参数集”
$attributes.Mandatory=$false
$attributeCollection=新对象类型System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attributes)
$dynParam1=新对象类型System.Management.Automation.RuntimeDefinedParameter(“dp1”,[Int32],$attributeCollection)
$paramDictionary=新对象类型System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add(“dp1”,$dynParam1)
返回$paramDictionary
}
}
}


我重读了你的问题,看起来你可能只是想要一个特定参数的静态、预定义的制表符完成值列表。如果是这样,那么您可以简单地:

函数获取某物{
[CmdletBinding()]
param(
[ValidateSet('1','2','3')]
[字符串]
$MyParam
)
}

但是,如果需要在运行时确定这些值,请参见上面关于动态参数的部分。

您可能需要查看,它旨在使扩展选项卡完成变得更容易

我只是用它玩了几分钟,我想根据这个例子,你想要这样的东西:

Import-Module TabExpansion++

function PaketAddNugetCompletion
{
    [ArgumentCompleter(Parameter = 'Nuget', Command = 'Paket-Add')]
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Paket-FindPackages -SearchText $wordToComplete |
        ForEach-Object {
            # not quite sure what property to use off the result, but this might work.
            New-CompletionResult -CompletionText $_ 
        }   
}

我把我的解决方案贴在上面。我将此标记为答案,因为它引导我找到了解决方案。另一个关键部分是在加载模块时运行该脚本。这是通过将其添加到.psd1:
ScriptsToProcess='ArgumentTabCompletion.ps1'
Import-Module TabExpansion++

function PaketAddNugetCompletion
{
    [ArgumentCompleter(Parameter = 'Nuget', Command = 'Paket-Add')]
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Paket-FindPackages -SearchText $wordToComplete |
        ForEach-Object {
            # not quite sure what property to use off the result, but this might work.
            New-CompletionResult -CompletionText $_ 
        }   
}