检查功能中是否存在参数

时间:2022-12-11 06:30:27

I have a function as follows:

我有如下功能:

function T
{
    Param
    (
        [switch] $IsValueNameRegularExpression
    )

    ..
}

Normally to determine if a parameter exists you would do if ($Param), only seeing as this is a [switch], if the function is called as T -IsValueNameRegularExpression:$false will return false on if ($IsValueNameRegularExpression), even though the parameter exists - i.e., the caller specified a value to the optional parameter.

通常确定参数是否存在,如果($ Param),只会看到这是一个[开关],如果函数被调用为T -IsValueNameRegularExpression:$ false将返回false if if($ IsValueNameRegularExpression),即使参数存在 - 即调用者为可选参数指定了值。

If I change the parameter type from [switch] to [bool] the same thing will happen, obviously.

如果我将参数类型从[switch]更改为[bool],显然会发生同样的事情。

In the code for my function I call some .NET methods which contain a default setting for my [bool]`[switch]` parameters, so unless the user has specified a value (be it true or false) I don't want to pass it to the .NET method.

在我的函数的代码中,我调用一些.NET方法,其中包含我的[bool]`[switch]`参数的默认设置,所以除非用户指定了一个值(是真或假),我不想将它传递给.NET方法。

I could assign default values to the parameter to match those of the default value of the .NET method, but that would be assuming that the default value of the .NET method never changes, which might not be true ...

我可以为参数分配默认值以匹配.NET方法的默认值,但这将假设.NET方法的默认值永远不会更改,这可能不是真的...

So, is there a more elegant way of doing this?

那么,有更优雅的方式吗?

3 个解决方案

#1


34  

Use $PSBoundParameters.ContainsKey() in order to check for a parameter presence:

使用$ PSBoundParameters.ContainsKey()来检查参数是否存在:

function T
{
    Param
    (
        [switch] $IsValueNameRegularExpression
    )

    $PSBoundParameters.ContainsKey('IsValueNameRegularExpression')
}

T
T -IsValueNameRegularExpression
T -IsValueNameRegularExpression:$false

Output:

输出:

False
True
True

#2


-1  

An easier (and more accurate way) is to use the IsPresent property. Using roughly the same code:

更简单(也更准确)的方法是使用IsPresent属性。使用大致相同的代码:

function T {
    Param (
        [switch] $IsValueNameRegularExpression
    )
    $IsValueNameRegularExpression.IsPresent
}

T
T -IsValueNameRegularExpression
T -IsValueNameRegularExpression:$false

yields the following output:

产生以下输出:

False
True
False

Note that binding the switch to false makes it "not present"

请注意,将开关绑定到false会使其“不存在”

#3


-2  

function T 
{
    Param([switch]$IsValueNameRegularExpression)

    $IsValueNameRegularExpression.ToBool()
}

Results

结果

T
False

T -IsValueNameRegularExpression
True

T -IsValueNameRegularExpression:$true
True

T -IsValueNameRegularExpression:$false
False

#1


34  

Use $PSBoundParameters.ContainsKey() in order to check for a parameter presence:

使用$ PSBoundParameters.ContainsKey()来检查参数是否存在:

function T
{
    Param
    (
        [switch] $IsValueNameRegularExpression
    )

    $PSBoundParameters.ContainsKey('IsValueNameRegularExpression')
}

T
T -IsValueNameRegularExpression
T -IsValueNameRegularExpression:$false

Output:

输出:

False
True
True

#2


-1  

An easier (and more accurate way) is to use the IsPresent property. Using roughly the same code:

更简单(也更准确)的方法是使用IsPresent属性。使用大致相同的代码:

function T {
    Param (
        [switch] $IsValueNameRegularExpression
    )
    $IsValueNameRegularExpression.IsPresent
}

T
T -IsValueNameRegularExpression
T -IsValueNameRegularExpression:$false

yields the following output:

产生以下输出:

False
True
False

Note that binding the switch to false makes it "not present"

请注意,将开关绑定到false会使其“不存在”

#3


-2  

function T 
{
    Param([switch]$IsValueNameRegularExpression)

    $IsValueNameRegularExpression.ToBool()
}

Results

结果

T
False

T -IsValueNameRegularExpression
True

T -IsValueNameRegularExpression:$true
True

T -IsValueNameRegularExpression:$false
False