简体   繁体   English

布尔参数上的 PowerShell ValidateSet

[英]PowerShell ValidateSet on Boolean Parameter

I am attempting to use ValidateSet with a boolean parameter however I cannot get it to function as expect.我试图将 ValidateSet 与布尔参数一起使用,但是我无法让它按预期运行。

An example to replicate the issue:复制问题的示例:

function Set-Boolean
{
    [CmdletBinding()]
    [OutputType([Bool])]
    Param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet($false,$true)]
        [Bool] $Bool
    )

    Process
    {
        $Bool
    }
}

At run time PowerShell expands the $true and $false variables upon tab completion to True and False respectively however this then cause the parameter validation to fail as only $true , $false , 1 and 0 are valid boolean types.在运行时,PowerShell 在 Tab 完成时将$true$false变量分别扩展为TrueFalse但这会导致参数验证失败,因为只有$true$false10是有效的布尔类型。

I tried changing ValidateSet to [ValidateSet('$false','$true')] and the tab completion works as expected however the parameter validation still fails as the parameter is expecting the strings '$false' and '$true' .我尝试将 ValidateSet 更改为[ValidateSet('$false','$true')]并且选项卡完成按预期工作,但是参数验证仍然失败,因为参数需要字符串'$false''$true'

I could change ValidateSet to [ValidateSet([bool]0,[bool]1)] and have it function as I expect but I find 0 and 1 a poorer user experience over $true and $false for completion of a boolean parameter.我可以将 ValidateSet 更改为[ValidateSet([bool]0,[bool]1)]并让它按照我的预期运行,但我发现01$true$false更差的用户体验是完成布尔参数。

The expected output of the function should be True when $true is chosen and False when $false is chosen.该函数的预期产出应为True时, $true选择和False时, $false选择。

I have implemented a workaround in my code but I want to know how I can use ValidateSet with a boolean parameter, if it is possible, to enable the use of tab completion for the user.我已经在我的代码中实现了一种解决方法,但我想知道如何使用带有布尔参数的 ValidateSet(如果可能)来为用户启用选项卡完成功能。

Joining some answers, the best option will depend on the needs, if the parameter is type boolean it can be replaced by one of type switch, but if you want to specify the value to see in the code or by style;加入一些答案,最好的选择取决于需要,如果参数是boolean类型,它可以用类型switch之一替换,但如果你想在代码或样式中指定要查看的值; It can be done as follows.可以按如下方式完成。

function Write-SwitchParam {
    [CmdletBinding()]
    param (
        [Switch] #[Switch] or [System.Management.Automation.SwitchParameter]
        $SwitchParam
    )

    if($SwitchParam)
    {
        Write-Host "Switch is present. Do Positive Action."
    }
    else {
        Write-Host "Switch isn't present. Do Negative Action."
    }
}

function Write-BooleanParam {
    [CmdletBinding()]
    Param(
    [parameter(Position=0, ValueFromPipeline=$true)]
    [ValidateNotNullOrEmpty()]
    [ValidateSet($true, $false, 0, 1)]
    $BoolParam = $true
    )

    Begin{  
    }

    Process{
        $value = [System.Convert]::ToBoolean($BoolParam)
        Write-Host $value
    }

    End{
    }   
}

Write-Host "TESTS BOOLEAN PARAM" -ForegroundColor Magenta
Write-BooleanParam -BoolParam $true
Write-BooleanParam -BoolParam $false
Write-Host

Write-BooleanParam $false
Write-BooleanParam $true
Write-Host

Write-BooleanParam True
Write-BooleanParam False
Write-Host

Write-BooleanParam 0
Write-BooleanParam 1
Write-Host

Write-BooleanParam
Write-Host

$true, $false, "True", "False", 1, 0 | Write-BooleanParam
Write-Host

Write-Host "TESTS SWITCH PARAM" -ForegroundColor Magenta
Write-SwitchParam 
Write-SwitchParam -SwitchParam

控制台输出

You can use ValidateSet for autocompletion without specifying the input variable type.您可以使用ValidateSet进行自动完成,而无需指定输入变量类型。 Though, you cannot use 0 and 1 as input in this case:但是,在这种情况下,您不能使用01作为输入:

function Set-Boolean
{
    [CmdletBinding()]
    [OutputType([Bool])]
    Param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet($false,$true)]
        $Bool
    )

    Process
    {
        [System.Convert]::ToBoolean($Bool)
    }
}

This gives the tab completion for the user, as desired using ValidateSet, but is achieved by registering an argument completer instead (especially in the ISE);这会根据需要使用 ValidateSet 为用户提供制表符补全,但通过注册参数补全器来实现(尤其是在 ISE 中);

function Set-Boolean
{
    [CmdletBinding()]
    [OutputType([Bool])]
    Param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [ValidateNotNullOrEmpty()]
        [Bool] $Bool
    )

    Process
    {
        $Bool
    }
}
Register-ArgumentCompleter -CommandName 'Set-Boolean' -ParameterName 'Bool' -ScriptBlock {
    [System.Management.Automation.CompletionResult]::new(
        '$False',
        '$False',
        'ParameterValue',
        '[bool] False'
    )
    [System.Management.Automation.CompletionResult]::new(
        '$True',
        '$True',
        'ParameterValue',
        '[bool] True'
    )
}

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

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