Можно ли иметь [Nullable [bool]] в bool [] в PowerShell - PullRequest
0 голосов
/ 06 июля 2018

Возможно ли иметь [Nullable [bool]] в bool [] в PowerShell? Я пробовал другое решение, подходы, но не смог получить правильный $ null, $ true, $ false для параметра? Также кажется, что [cmdletbinding] меняет то, как все работает.

enum BoolNull {
    null = $null
    true = $true
    false = $false
}
function Test-Array0 {
    param (
        [bool[]] $thisValue
    )
    if ($thisValue -eq $null) {
        Write-Output 'Null found'
    }
}
function Test-Array1 {
    param (
        [bool[]] $thisValue
    )
    foreach ($value in $thisValue) {
        if ($value -eq $null) {
            Write-Output 'Null found'
        }
    }
}
function Test-Array2 {
    [CmdletBinding()]
    param (
        [bool[]] $thisValue
    )
    if ($thisValue -eq $null) {
        Write-Output 'Null found'
    }
}


function Test-Array {
    [CmdletBinding()]
    param (
        [AllowEmptyCollection()] [AllowNull()][ValidateSet($null, $true, $false)] $thisValue
    )
    if ($thisValue -eq $null) {
        Write-Output 'Null found'
    }
}

# this works
function Test-Test {
    [CmdletBinding()]
    param (
        [nullable[bool]] $thisValue
    )
    if ($thisValue -eq $null) {
        Write-Output 'Null found'
    }
}

function Test-Array5 {
    param (
        [boolnull[]] $thisValue
    )
    foreach ($value in $thisValue) {
        if ($value -eq 'null') {
            Write-Output 'Null found'
        }
    }
}

Test-Array0 -thisValue $null #this works
Test-Array -thisValue $null # this doesn't work
Test-Array -thisValue $null, $null, $true # this doesn't work
Test-Array1 -thisValue $null
Test-Array2 -thisValue $null # this
Test-Test -thisValue $null # this works 
Test-Array5 -thisValue null, true, null # this works but is completely useless

enter image description here

1 Ответ

0 голосов
/ 06 июля 2018

Это ограничение типа bool. Когда вы строго вводите параметр, он может принимать только $true, $false, 0 и 1. Чтобы достичь желаемого, вы можете использовать атрибут [ValidateSet]:

[CmdletBinding()]
param(
    [Parameter(Position = 0, Mandatory)]
    [ValidateSet($null, $true, $false)]
    [object] $ThisValue
)

Как примечание, раньше была ошибка с powershell (она все еще может присутствовать), когда сравнение $null с правой стороны не приводило к возврату ничего, вызывая выпадение логики из оператора, поэтому лучше всего сравнить слева:

if ($null -eq $ThisValue) {

После тестирования вашего примера мне не удалось воспроизвести вашу проблему, однако:

function Test-Nullable {
    [CmdletBinding()]
    param(
        [nullable[bool]] $Value
    )

    if ($null -eq $Value) {
        'Yes'
    } else {
        $Value
    }
}

и в формате массива:

function Test-Nullable {
    [CmdletBinding()]
    param(
        [nullable[bool][]] $Value
    )

    foreach ($bool in $Value) {
        if ($null -eq $bool) {
            'Yes'
        } else {
            $bool
        }
    }
}

Test-Nullable 5, 3, $null, $true, $false, 0
True
True
Yes
True
False
False
...