Skip to content

Add completion for switch cases for $PSBoundParameters.Keys #25349

@MartinGC94

Description

@MartinGC94

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Interactive-IntelliSensetab completionWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions