Skip to content

Support for parameter set selection by parameter value. #4102

@powercode

Description

@powercode

A common pattern with native binaries is to have a first parameter indicating some general command, and the rest of the commandline is options to that first command.
Like

p4 opened ...
git commit -a --amend 

#pattern
exe command flag1 option2=1

Today, this is something that I find cumbersome to express in PowerShell, since the allowed verbs are often a mismatch with the command, and we have no nice way to match parameter sets with the value of a parameter. (It can be done with dynamic parameters, but that quickly becomes unwieldy and seems like an abuse of the feature).

I'd like to start a discussion to see if we can come up with a better way.

An initial suggestion is to allow an enum or string parameter at the first position with a Parameter flag (here: ParameterSetSelector)be the selector of the parameter set.

enum SubCommandTypes {
    SomeSubCommand
   AnotherSubCommand
}

function Start-MainCommand {
    param(
        [Parameter(ParameterSetSelector=$true, Mandatory=$true)]
        [SubCommandTypes ] $Command
        ,
        [Parmameter(ParametersetName="SomeSubCommand")]
        [int] $Count
        ,
        [Parmameter(ParametersetName="AnotherSubCommand")]
        [int] $Size
    )
}
# or 
function Start-MainCommand {
    param(
        [Parameter(ParameterSetSelector=$true, Mandatory=$true)]
        [ValidateSet("SomeSubCommand", "AnotherSubCommand")]
        [string] $Command
        ,
        [Parmameter(ParametersetName="SomeSubCommand")]
        [int] $Count
        ,
        [Parmameter(ParametersetName="AnotherSubCommand")]
        [int] $Size
    )
}

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions