PowerShell icon indicating copy to clipboard operation
PowerShell copied to clipboard

Add completion for switch cases for `$PSBoundParameters.Keys`

Open MartinGC94 opened this issue 8 months ago • 5 comments

Summary of the new feature / enhancement

I occasionally write functions where I use a switch based on the bound parameters like this:

function Verb-Noun
{
    Param
    (
        [Parameter()]
        [string]
        $Param1,

        [Parameter()]
        [string]
        $Param2
    )

    switch ($PSBoundParameters.Keys)
    {
        Param1 {}
        Param2 {}
    }
}

To ensure I don't make typos I tend to copy+paste the parameters but tab completing them would be much more convenient. But before I try to add this feature, I want to be sure it would actually get accepted. Do other people use this pattern? Is this a pattern we want to encourage? If not, how do other people check if various parameters have been specified?

Proposed technical implementation details (optional)

The identifier/string completion code would be updated to check if it's completing a switch case, and if so, if the switch is on a variable with a property called Keys.
If the variable is $PSBoundParameters it would look for the nearest param block and get results from there.
If the variable is anything else we could try to look for a hashtable definition to that variable and get results from that, but I don't know if it's worth adding this because I don't think this is a particularly common pattern.
Also we could do some type inference on the variable, and if it's an Enum get the completion values from there.

MartinGC94 avatar Apr 12 '25 18:04 MartinGC94

I suspect that scripting constructs are not used very often interactively. Personally, I load custom functions using dot-sourcing functions.ps1 from $profile. In interactive mode, I use anonymous script blocks &{ } to limit the scope of variables. But these are not really advanced functions. If such a script block is cumbersome and there is a risk of losing it if I accidentally press ESC, then I edit them in $env:VISUAL by pressing F4.

237dmitry avatar Apr 13 '25 06:04 237dmitry

@237dmitry Editors like VS code get their completion data from the PowerShell engine. This suggestion is mainly thought of to improve the editor experience, not the interactive shell experience.

MartinGC94 avatar Apr 13 '25 10:04 MartinGC94

Editors like VS code get their completion data from the PowerShell engine

Then it would be a convenient addition.

237dmitry avatar Apr 13 '25 19:04 237dmitry

I once had a similar idea for a more common purpose in the form of a switch with a -key or a -property parameter. The issue with this is that it is unclear what you expect the current item ($_) to be in the action script block ({}): the current key/name, the current value or even something like a key-value pair? Knowing that they might be both required for any specific action.

iRon7 avatar Apr 17 '25 07:04 iRon7

The interactive working group has reviewed this issue and would like to see it move forward.

theJasonHelmick avatar Apr 30 '25 20:04 theJasonHelmick

This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.