Skip to content

$PSBoundParameters for [switch] integers #19580

@237dmitry

Description

@237dmitry

Summary of the new feature / enhancement

Add possibility for integers as switch parameters.

For example I want to use switch parameters like -8, -16, -256 in script.

param (

    [Parameter()] [switch] $8,
    [Parameter()] [switch] $16,
    [Parameter()] [switch] $256
)

switch ($PSBoundParameters.Keys)
{
      '8' { 8 }
     '16' { 16 }
    '256' { 256 }
}

But I get the error:

./script -8
script.ps1: A positional parameter cannot be found that accepts argument '-8'.

That is, -8 is interpreted as an argument

The workaround:

param (

    [Parameter()] [int] $8,
    [Parameter()] [int] $16,
    [Parameter()] [int] $256
)

switch ($PSBoundParameters.Values)   # $PSBoundParameters.Keys works by parameter position, wrong result.
{
      -8 { 8 }
     -16 { 16 }
    -256 { 256 }
}
./script -256 -16
256
16

But it would be better if the names of switch (and not only) parameters of this kind were perceived as they are. Literally.

Proposed technical implementation details (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugNeeds-TriageThe issue is new and needs to be triaged by a work group.Resolution-No ActivityIssue has had no activity for 6 months or more

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions