Add completion for switch cases for `$PSBoundParameters.Keys`
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.
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 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.
Editors like VS code get their completion data from the PowerShell engine
Then it would be a convenient addition.
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.
The interactive working group has reviewed this issue and would like to see it move forward.
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.