简体   繁体   English

为什么当有一个参数集但没有参数时参数绑定成功但有两个失败?

[英]Why does parameter binding succeed with no arguments when there is one parameter set but fails with two?

I have two functions.我有两个功能。 The first has one parameter set, the second has two parameter sets as follows:第一个有一个参数集,第二个有两个参数集,如下所示:

function OneSet{
    [CmdletBinding()]
    param ( $NoSet,
            [parameter(ParameterSetName = 'A')]$A )
    process { $PSCmdlet.ParameterSetName }
}

function TwoSets{
    [CmdletBinding()]
    param ( $NoSet,
            [parameter(ParameterSetName = 'A',Mandatory = $true)]$A,
            [parameter(ParameterSetName = 'B',Mandatory = $true)]$B  )
    process { $PSCmdlet.ParameterSetName }
}

Invoking the first one without arguments results in '__AllParameterSets' binding:不带参数调用第一个会导致 '__AllParameterSets' 绑定:

C:\> OneSet
__AllParameterSets

Invoking the second one without arguments throws an exception:不带参数调用第二个会引发异常:

C:\> TwoSets
TwoSets : Parameter set cannot be resolved using the specified named parameters.
+ TwoSets
+ ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [TwoSets], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,TwoSets

I don't see why the second case is any more ambiguous than the first.我不明白为什么第二种情况比第一种情况更模棱两可。 Why doesn't PowerShell bind to TwoSets using the " __AllParameterSets " parameter set?为什么 PowerShell 不使用“ __AllParameterSets ”参数集绑定到TwoSets

Is there a (terse) way to have multiple parameter sets and still be able to call the function with no arguments?有没有一种(简洁的)方法来拥有多个参数集并且仍然能够在没有参数的情况下调用函数?


Edit: Added Mandatory keyword to more accurately represent my predicament.编辑:添加了Mandatory关键字以更准确地代表我的困境。

It's because PowerShell can't figure out which parameter set you're trying to use.这是因为 PowerShell 无法确定您尝试使用的参数集。 You can tell it what to default to in the CmdletBinding attribute.您可以在 CmdletBinding 属性中告诉它默认使用什么。

function TwoSets{
    [CmdletBinding(DefaultParameterSetName='A')]
    param ( $NoSet,
            [parameter(ParameterSetName = 'A')]$A,
            [parameter(ParameterSetName = 'B')]$B  )
    process { $PSCmdlet.ParameterSetName }
}

Edit: I think None could be substituted for __AllParameterSets to achieve the goal of "creating a parameter set with no mandatory parameters at all".编辑:我认为None可以代替__AllParameterSets来实现“创建一个完全没有强制参数的参数集”的目标。 For more details see PowerShell/PowerShell#11237 , #12619 , and #11143有关更多详细信息,请参阅PowerShell/PowerShell#11237#12619#11143


I don't see why the second case is any more ambiguous than the first.我不明白为什么第二种情况比第一种情况更模棱两可。 Why doesn't PowerShell bind to TwoSets using the " __AllParameterSets " parameter set?为什么 PowerShell 不使用“ __AllParameterSets ”参数集绑定到 TwoSets?

PowerShell seems to use the __AllParameterSets parameter set as a default when there is only one user-named parameter set.当只有一个用户命名的参数集时,PowerShell 似乎使用__AllParameterSets参数集作为默认值。 On the other hand, when there is more than one user-named parameter set, PowerShell does not seem to consider any parameter set to be a default unless you specify it.另一方面,当存在多个用户命名的参数集时,除非您指定,否则 PowerShell 似乎不会将任何参数集视为默认值。

Is there a (terse) way to have multiple parameter sets and still be able to call the function with no arguments?有没有一种(简洁的)方法来拥有多个参数集并且仍然能够在没有参数的情况下调用函数?

You can tell PowerShell to use __AllParameterSets as the default as follows:您可以告诉 PowerShell 使用__AllParameterSets作为默认值,如下所示:

function TwoSets{
    [CmdletBinding(DefaultParameterSetName = '__AllParameterSets')]
    param ( $NoSet,
            [parameter(ParameterSetName = 'A', Mandatory = $true)]$A,
            [parameter(ParameterSetName = 'B', Mandatory = $true)]$B  )
    process { $PSCmdlet.ParameterSetName }
}

Then invoking with no parameters, -A , or -B yields causes binding to the each of the three parameter sets as follows:然后不带参数调用-A-B会导致绑定到三个参数集中的每一个,如下所示:

PS C:\> TwoSets
__AllParameterSets

PS C:\> TwoSets -A 'a'
A

PS C:\> TwoSets -B 'b'
B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM