Skip to content

Support for parameter set selection by parameter value. #4102

Closed

Description

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?

Activity

  1. iSazonov commented on Jun 26, 2017

    @iSazonov
    Collaborator

    Staffan Gustafsson (@powercode) Thanks for opening the discussion! I've waited a long time for this.

    I think we should start with trying to formalize the description (notation, terms).

    statement := command [parameterset]
    
    parameterset := parametersetParameterList
    
    parametersetParameterList := parametersetParameter parametersetParameterList
    
    parametersetParameter  := Parameter [SubParameterlist]
    
    SubParameterlist := parametersetParameterList 
    
    Parameter := valueParameter | Switch
    
    
  2. added
    WG-Languageparser, language semantics
    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif
    on Jun 26, 2017
  3. SteveL-MSFT commented on Jun 26, 2017

    @SteveL-MSFT
    Member

    cc @PowerShell/area-language

  4. ohadschn commented on Jan 30, 2022

    @ohadschn

    I would take this a step further and suggest that the value of the property could be inspected as well. Something like:

    [Parameter(ParameterSetSelector="MyParamSetName", ConditionalValue=$true)]
    [Switch] $Command

    This way you would support Start-MainCommand -Command:$myFlag, otherwise you would resolve the wrong set for Start-MainCommand -Command:$false.

  5. powercode commented on Jun 7, 2022

    @powercode
    CollaboratorAuthor

    What would the effect be of having a callback to user code to determine parameter set?

    We have a few different scenarios that PowerShell doesn't handle well, and this could potentially open up for handling the more difficult cases.

    I'm thinking in the way of an attribute on Cmdlet, [ParameterSetResolver(typeof(MyResolver))].

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  9. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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