powershell - Add Custom Argument Completer for Cmdlet? -
how add dynamic argument tab completion powershell cmdlet?
when type , hit tab
, i'd tab completion.
pm> paket-add -nuget fsharp.co
these values i'd use in example:
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
i found this answer gave couple of helpful links , said should run get-content function:tabexpansion2
:
it looks commandcompletion.completeinput needs implemented. thought read somewhere there hashtable
of commands functions. if so, , how install custom ones? i'm using chocolatey distribute paket.powershell. here cmdlet code.
update 2015-06-20:
i ended getting work code here: https://github.com/fsprojects/paket/blob/76de1c44853ce09029ba157855525f435d951b85/src/paket.powershell/argumenttabcompletion.ps1
# https://github.com/mariuszwojcik/rabbitmqtools/blob/master/tabexpansions.ps1 function createcompletionresult([string]$text, [string]$value, [string]$tooltip) { if ([string]::isnullorempty($value)) { return } if ([string]::isnullorempty($text)) { $text = $value } if ([string]::isnullorempty($tooltip)) { $tooltip = $value } $completiontext = @{$true="'$value'"; $false=$value }[$value -match "\w"] $completiontext = $completiontext -replace '\[', '``[' -replace '\]', '``]' new-object system.management.automation.completionresult $completiontext, $text, 'parametervalue', $tooltip | write } $findpackages = { param($commandname, $parametername, $wordtocomplete, $commandast, $fakeboundparameter) paket-findpackages -searchtext $wordtocomplete -max 100 | % { createcompletionresult $_ $_ $_ | write } } $findpackageversions = { param($commandname, $parametername, $wordtocomplete, $commandast, $fakeboundparameter) if (-not $fakeboundparameter.nuget){ return } paket-findpackageversions -name $fakeboundparameter.nuget -max 100 | % { createcompletionresult $_ $_ $_ | write } } # create , add $global:options list of completers # http://www.powertheshell.com/dynamicargumentcompletion/ if (-not $global:options) { $global:options = @{customargumentcompleters = @{};nativeargumentcompleters = @{}}} $global:options['customargumentcompleters']['paket-add:nuget'] = $findpackages $global:options['customargumentcompleters']['paket-add:version'] = $findpackageversions $function:tabexpansion2 = $function:tabexpansion2 -replace 'end\r\n{','end { if ($null -ne $options) { $options += $global:options} else {$options = $global:options}'
the completer param names important. renaming them make not work.
you may want @ tabexpansion++ module, designed make extending tab completion easier.
i played few minutes, , think want based on example:
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 property use off result, might work. new-completionresult -completiontext $_ } }
Comments
Post a Comment