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?
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
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?