PowerShell 中互斥参数集:为什么会出现歧义?
我一直在尝试让多组互斥发挥作用。 我希望“Width”与“WidthReset”互斥,“Height”与“HeightReset”互斥。
cmdlet 的帮助显示:
Get-ArgTest [-Width <int>] [-Height <int>] [<CommonParameters>]
Get-ArgTest [-Width <int>] [-HeightReset] [<CommonParameters>]
Get-ArgTest [-Height <int>] [-WidthReset] [<CommonParameters>]
Get-ArgTest [-WidthReset] [-HeightReset] [<CommonParameters>]
请参阅以下代码。
一切正常,但 Get-ArgTest -WidthReset 似乎不明确。
为什么这一案子是模棱两可的呢? 其他对称情况不会被标记为不明确。
我怎样才能解决歧义?
将 DefaultParameterSetName 更改为 B 或 C 会更改不明确的大小写。
这向我表明它应该有效。
##########################################
# This cmdlet *should* accept
# [-Width | -WidthReset] [-Height | -HeightReset]
##########################################
function Get-ArgTest
{
[CmdletBinding(DefaultParameterSetName = 'A')]
param(
[Parameter(ParameterSetName='A')]
[Parameter(ParameterSetName='B')]
[int] $Width,
[Parameter(ParameterSetName='C')]
[Parameter(ParameterSetName='A')]
[int] $Height,
[Parameter(ParameterSetName='C')]
[Parameter(ParameterSetName='D')]
[switch] $WidthReset,
[Parameter(ParameterSetName='B')]
[Parameter(ParameterSetName='D')]
[switch] $HeightReset
)
Write-Output "HELLO, Width=$Width, Height=$Height, WidthReset=$WidthReset, HeightReset=$HeightReset"
}
##########################################
# Tests
##########################################
# First, display HELP
Get-ArgTest -?
####
## Try each combination
# First, just the values
Get-ArgTest
Get-ArgTest -Width 2
Get-ArgTest -Height 4
Get-ArgTest -Width 2 -Height 4
# Values and the opposite reset
Get-ArgTest -Width 2 -HeightReset
Get-ArgTest -Height 4 -WidthReset
# The Resets
Get-ArgTest -WidthReset -HeightReset
Get-ArgTest -WidthReset -HeightReset:$False
Get-ArgTest -WidthReset:$False -HeightReset:$True
# I cannot get the following cases to work :-(
Get-ArgTest -WidthReset
Get-ArgTest -HeightReset
I've been trying to get multiple sets of mutual exclusions to work.
I want "Width" mutually exclusive to "WidthReset" and "Height" to be mutually exclusive with "HeightReset".
Help for the cmdlet shows:
Get-ArgTest [-Width <int>] [-Height <int>] [<CommonParameters>]
Get-ArgTest [-Width <int>] [-HeightReset] [<CommonParameters>]
Get-ArgTest [-Height <int>] [-WidthReset] [<CommonParameters>]
Get-ArgTest [-WidthReset] [-HeightReset] [<CommonParameters>]
See the following code.
Everything works, but Get-ArgTest -WidthReset
seems to be ambiguous.
Why is this one case is ambiguous?
Other, symmetrical cases are not flagged as ambiguous.
How can I resolve the ambiguity?
Changing the DefaultParameterSetName to B or C changes which case is ambiguous.
That indicates to me that it should work.
##########################################
# This cmdlet *should* accept
# [-Width | -WidthReset] [-Height | -HeightReset]
##########################################
function Get-ArgTest
{
[CmdletBinding(DefaultParameterSetName = 'A')]
param(
[Parameter(ParameterSetName='A')]
[Parameter(ParameterSetName='B')]
[int] $Width,
[Parameter(ParameterSetName='C')]
[Parameter(ParameterSetName='A')]
[int] $Height,
[Parameter(ParameterSetName='C')]
[Parameter(ParameterSetName='D')]
[switch] $WidthReset,
[Parameter(ParameterSetName='B')]
[Parameter(ParameterSetName='D')]
[switch] $HeightReset
)
Write-Output "HELLO, Width=$Width, Height=$Height, WidthReset=$WidthReset, HeightReset=$HeightReset"
}
##########################################
# Tests
##########################################
# First, display HELP
Get-ArgTest -?
####
## Try each combination
# First, just the values
Get-ArgTest
Get-ArgTest -Width 2
Get-ArgTest -Height 4
Get-ArgTest -Width 2 -Height 4
# Values and the opposite reset
Get-ArgTest -Width 2 -HeightReset
Get-ArgTest -Height 4 -WidthReset
# The Resets
Get-ArgTest -WidthReset -HeightReset
Get-ArgTest -WidthReset -HeightReset:$False
Get-ArgTest -WidthReset:$False -HeightReset:$True
# I cannot get the following cases to work :-(
Get-ArgTest -WidthReset
Get-ArgTest -HeightReset
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了避免歧义,请在您的
D
参数集中将-WidthReset
和-HeightReset
设置为强制:这会导致以下语法图(
Get-Command -Syntax Get-ArgTest
):结果:
如果两者
-WidthReset
且-HeightReset
存在,参数集D
明确暗示。如果
-WidthReset
或-HeightReset
存在,则参数设置C
或B
是明确暗示的。至于您尝试的内容:
根据您的原始函数定义,以下调用不明确:
因为前者匹配两者参数集
C
和D
,后者两者
B
和D
,并且它们都不是默认参数集(它将充当决胜局)。
To avoid the ambiguity, make
-WidthReset
and-HeightReset
mandatory in yourD
parameter set:This results in the following syntax diagram (
Get-Command -Syntax Get-ArgTest
):As a result:
If both
-WidthReset
and-HeightReset
are present, parameter setD
is unambiguously implied.If either
-WidthReset
or-HeightReset
is present, parameter setC
orB
is unambiguously implied.As for what you tried:
With your original function definition, the following calls are ambiguous:
because the former matches both parameter set
C
andD
,and the latter both
B
andD
,and none of them is the default parameter set (which would act as the tie-breaker).