No Parameter Completion Support?

Aug 13, 2011 at 11:46 PM

We maintain a set of CLI scripts for administering our product.  I'd like to add parameter-completion support.  However, it looks like PowerTab only supports completing arguments, not parameters - is that right?  At the least, when I register a handler, like this:

Register-TabExpansion "fake" -Type "Command" {
	param ($Context, [ref]$TabExpansionHasOutput, [ref]$QuoteSpaces)

	# Return the possible parameters
	'-a','--archive'
	
	$TabExpansionHasOutput.Value = $true
}

It works fine in many cases:

"fake <TAB>" gives me the expected list.

"fake anyvalue<TAB>" yields the expected list, too ("anyvalue" is just discarded)

But on "fake -<TAB>", my handler is never called at all.

Any advice on how to support tab-expansion of parameters (not arguments) for a custom CLI?

Thanks,

Dathan

Coordinator
Aug 23, 2011 at 7:26 AM
Edited Aug 23, 2011 at 7:26 AM

Apologies for the wait.  I don't have this documented yet, but the way to go about doing this is to register a "ParameterName" handler.  Here is an example that is already in PowerTab.

 

Register-TabExpansion powershell.exe -Type ParameterName {
    param($Context, $Parameter)
    $Parameters = "-Command","-EncodedCommand","-ExecutionPolicy","-File","-InputFormat","-NoExit","-NoLogo",
        "-NonInteractive","-NoProfile","-OutputFormat","-PSConsoleFile","-Sta","-Version","-WindowStyle"
    $Parameters | Where-Object {$_ -like "$Parameter*"}
}

 

Basically it is just a function that is given a context object plus what the user typed (including the dash).  And you return the list of parameters that you want to display.

If you want to also have a handler that deals with values to any of those parameters, you need to register another type of handler called a "CommandInfo".  This shows a PowerTab a CommandInfo object like any PowerShell command would have.  Here is an example.

 

Function powershellexeparameters {
    param(
        [String]$Command
        ,
        [String]$EncodedCommand
        ,
        [Microsoft.PowerShell.ExecutionPolicy]$ExecutionPolicy
        ,
        [String]$File
        ,
        [ValidateSet("Text","XML")]
        [String]$InputFormat
        ,
        [Switch]$NoExit
        ,
        [Switch]$NonInteractive
        ,
        [Switch]$NoLogo
        ,
        [Switch]$NoProfile
        ,
        [ValidateSet("Text","XML")]
        [String]$OutputFormat
        ,
        [String]$PSConsoleFile
        ,
        [Switch]$Sta
        ,
        [ValidateSet("1.0","2.0")]
        [String]$Version
        ,
        [ValidateSet("Normal","Minimized","Maximized","Hidden")]
        [String]$WindowStyle
    )
}

$PowershellCommandInfo = Get-Command powershellexeparameters
Register-TabExpansion powershell.exe -Type CommandInfo {
    param($Context)
    $PowershellCommandInfo
}
Aug 23, 2011 at 7:30 AM

Sweet.  Thanks for such a complete reply.  I'll try this immediately.