Repository navigation
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
iSazonov commentedon Jun 26, 2017
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).
SteveL-MSFT commentedon Jun 26, 2017
cc @PowerShell/area-language
ohadschn commentedon Jan 30, 2022
I would take this a step further and suggest that the value of the property could be inspected as well. Something like:
This way you would support
Start-MainCommand -Command:$myFlag, otherwise you would resolve the wrong set forStart-MainCommand -Command:$false.powercode commentedon Jun 7, 2022
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))].microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.