代码之家  ›  专栏  ›  技术社区  ›  zzzggghhh

最简单/最理想的PowerShell函数,可以接受所有可能的输入

  •  0
  • zzzggghhh  · 技术社区  · 11 月前

    PowerShell函数/cmdlet是否有任何简单快捷的方法来接受和处理以管道/位置/未声明的方式给定的所有参数。。。同时仍然维护和接受任何命名的参数?本质上,命名参数应该单独处理,然后任何和所有其他参数都应该作为常规输入项处理。下面是我为一个可以做到这一点的函数编写的一个小测试套件,switch参数“lower”表示结果应该全部小写:

    $tests = @(
        @{
            run = { test "a" }
            expected = 'A'
        },
    
        @{
            run = { test "a" -lower }
            expected = 'a'
        },
    
        @{
            run = { test "a" "b" }
            expected = 'AB'
        },
    
        @{
            run = { test "a" "b" "c" }
            expected = 'ABC'
        },
    
        @{
            run = { test  -lower @("a","b","c") }
            expected = 'abc'
        },
    
        @{
            run = { test "a" @("b","c") }
            expected = 'ABC'
        },
    
        @{
            run = { test @("a","b") "c" }
            expected = 'ABC'
        },
    
        @{
            run = { test "a" @("b") "c" }
            expected = 'ABC'
        },
    
        @{
            run = { test "a" @("b") @("c") }
            expected = 'ABC'
        },
    
    
        @{
            run = { "a" | test }
            expected = 'A'
        },
    
        @{
            run = { @("a") | test }
            expected = 'A'
        },
    
        @{
            run = { @("a", "b") | test -lower }
            expected = 'ab'
        },
    
        @{
            run = { @("a", "b", "c") | test }
            expected = 'ABC'
        },
    
        @{
            run = { @(@("a", "b", "c")) | test }
            expected = 'ABC'
        },
    
        @{
            run = { @("a", @("b", "c")) | test }
            expected = 'ABC'
        },
    
        @{
            run = { @(@("a", "b"), "c") | test }
            expected = 'ABC'
        },
    
        @{
            run = { @("a", @("b"), "c") | test }
            expected = 'ABC'
        },
    
        @{
            run = { @("a", @("b"), @("c")) | test }
            expected = 'ABC'
        },
    
    
        @{
            run = { "b" | test "a" }
            expected = 'AB'
        },
    
        @{
            run = { @("b") | test "a" }
            expected = 'AB'
        },
    
        @{
            run = { @("c", "d") | test @("a", "b") }
            expected = 'ABCD'
        },
    
        @{
            run = { @("d", "e", "f") | test "a" @("b", "c") }
            expected = 'ABCDEF'
        },
    
        @{
            run = { "c" | test -lower "a" "b" }
            expected = 'abc'
        },
    
        @{
            run = { @("e", @("f", "g")) | test @("a", "b") "c" "d" }
            expected = 'ABCDEFG'
        },
    
        @{
            run = { @(@("e", "f"), "g") | test "a" "b" @("c", "d") }
            expected = 'ABCDEFG'
        },
    
        @{
            run = { @("d", @("e"), "f") | test @("a", "b") "c" -lower }
            expected = 'abcdef'
        },
    
        @{
            run = { @("e", @("f"), @("g")) | test "a" @("b", "c") "d" }
            expected = 'ABCDEFG' }
    )
    

    运行它:

    $tests | % {
        $actual  =  & $_.run
        if($actual -ne ($_.expected)){ Write-Host "expected: '$($_.expected)', actual: '$actual'" }
    }
    
    1 回复  |  直到 11 月前
        1
  •  1
  •   Santiago Squarzon    11 月前

    使用这个简单的函数可以实现您的预期结果 $input $args 自动变量:

    function test {
        param([switch] $lower)
    
        $result = -join ($args + $input | Write-Output)
        if ($lower.IsPresent) {
            return $result.ToLower()
        }
    
        return $result.ToUpper()
    }