Powershell: Feature Request: Format-* View parameter argument completers

Created on 28 Jul 2020  路  4Comments  路  Source: PowerShell/PowerShell

Summary of the new feature/enhancement

PowerShell has a rich formatting and output system that allows for multiple views to be defined for a given type. This is a powerful but under utilized feature due to view name discovery. The user is unaware of what views are available for a given type. There is Get-FormatData added in PSv3 to get imported format views but is a lot of work to just find out what view names exist in the system. To solve the discovery problem and increase usage of the diverse format views available, an argument completer should be added to the Format-* command -View parameter.

Proposed technical implementation details (optional)

This is a proof of concept for Format-Table -View parameter. It works when using the -InputObject parameter outside of the pipleline. Not sure if there is a way to get $fakeBoundParameters to be defined when using the pipleline. (If this needs to be a new issue I can open one)

$s = {
    param ($commandName,$parameterName,$wordToComplete,$commandAst,$fakeBoundParameters)

    $views = Get-FormatData -TypeName (@($fakeBoundParameters['InputObject'])[0].PSTypeNames -split '`r`n') |
    Select-Object -ExpandProperty FormatViewDefinition |
    Where-Object { $_.Control.GetType().Name -eq 'TableControl'} |
    Select-Object -ExpandProperty Name

    foreach ($view in $views) {
        New-Object -Type System.Management.Automation.CompletionResult -ArgumentList $view, $view, "ParameterValue", $view
    }
}

Register-ArgumentCompleter -CommandName Format-Table -ParameterName View -ScriptBlock $s
Issue-Enhancement Up-for-Grabs WG-Interactive-IntelliSense

Most helpful comment

Yeah as @vexx32 said all that logic is currently built directly into the completions logic. Instead of adding more to that (especially for an edge case like View) I'd love to see a public interface for this. Like [PSTypeName[]] $inferredInputType added to argument completers maybe.

All 4 comments

We already do pipeline input type-aware completions for things like Select-Object -Property <tab> if I recall correctly, so there is logic already built into the argument completion logic somewhere.

This could use some of that and expand it to the Format-Table / Format-List cmdlets.

@vexx32 You're right that about Select-Object -Property. Format-* -Property has it as well. Not sure where the logic is since there isn't an argument completer attribute for it.

It'll be buried in some of the CompletionCompleters.cs code paths, I imagine. There's a bunch of internal logic coded for that that we'd want to hook into / add onto I'd imagine.

Yeah as @vexx32 said all that logic is currently built directly into the completions logic. Instead of adding more to that (especially for an edge case like View) I'd love to see a public interface for this. Like [PSTypeName[]] $inferredInputType added to argument completers maybe.

Was this page helpful?
0 / 5 - 0 ratings