繁体   English   中英

默认值不在PsBoundParameters中的参数?

[英]Parameters with default value not in PsBoundParameters?

一般代码

考虑以下代码:

PS> function Test { param($p='default value') $PsBoundParameters }
PS> Test 'some value'
Key                                                               Value
---                                                               -----
p                                                                 some value
PS> Test
# nothing

我希望$PsBoundParameters在两种情况下都包含$p变量的记录。 这是正确的行为吗?

我想使用这样的splatting来实现很多功能:

function SomeFuncWithManyRequiredParams {
  param(
    [Parameter(Mandatory=$true)][string]$p1,
    [Parameter(Mandatory=$true)][string]$p2,
    [Parameter(Mandatory=$true)][string]$p3,
  ...other parameters
  )
  ...
}
function SimplifiedFuncWithDefaultValues {
  param(
    [Parameter(Mandatory=$false)][string]$p1='default for p1',
    [Parameter(Mandatory=$false)][string]$p2='default for p2',
    [Parameter(Mandatory=$false)][string]$p3='default for p3',
  ...other parameters
  )
  SomeFuncWithManyRequiredParams @PsBoundParameters
}

我不想用枚举的所有参数调用SomeFuncWithManyRequiredParams:

  SomeFuncWithManyRequiredParams -p1 $p1 -p2 $p2 -p3 $p3 ...

可能吗?

我知道这个问题已经很老了,但我最近需要这样的东西(想要用很多默认参数进行喷涂)。 我想出了这个并且效果很好:

$params = @{}
foreach($h in $MyInvocation.MyCommand.Parameters.GetEnumerator()) {
    try {
        $key = $h.Key
        $val = Get-Variable -Name $key -ErrorAction Stop | Select-Object -ExpandProperty Value -ErrorAction Stop
        if (([String]::IsNullOrEmpty($val) -and (!$PSBoundParameters.ContainsKey($key)))) {
            throw "A blank value that wasn't supplied by the user."
        }
        Write-Verbose "$key => '$val'"
        $params[$key] = $val
    } catch {}
}

无耻的插件 :我决定把它变成一篇博文,里面有更多的解释和一个示例用法脚本

这取决于你如何定义“绑定”我猜是即从用户提供的值绑定的值或函数提供的默认值? 老实说,它并没有让我感到惊讶的是它的行为方式,因为我认为“绑定”意味着前者 - 从用户输入绑定。 无论如何,你可以通过修补$ PSBoundParameters变量来解决这个问题,例如:

function SimplifiedFuncWithDefaultValues { 
  param( 
    [Parameter(Mandatory=$false)][string]$p1='default for p1', 
    [Parameter(Mandatory=$false)][string]$p2='default for p2', 
    [Parameter(Mandatory=$false)][string]$p3='default for p3', 
  ...other parameters 
  ) 
  if (!$PSBoundParameters.ContainsKey(p1))
  {
    $PSBoundParameters.p1 = 'default for p1'
  }
  # rinse and repeat for other default parameters.
  SomeFuncWithManyRequiredParams @PSBoundParameters 
} 

您可以使用类似于下面的Add-Variable函数的辅助函数:

function SimplifiedFuncWithDefaultValues {
    param(
        [Parameter(Mandatory=$false)][string]$p1='default for p1',
        [Parameter(Mandatory=$false)][string]$p2='default for p2',
        [Parameter(Mandatory=$false)][string]$p3='default for p3',
        ...other parameters
    )

    $PSBoundParameters | Add-Variable p1, p2, p3

    SomeFuncWithManyRequiredParams @PSBoundParameters 
}

function Add-Variable {
    param(
        [Parameter(Position = 0)]
        [AllowEmptyCollection()]
        [string[]] $Name = @(),
        [Parameter(Position = 1, ValueFromPipeline, Mandatory)]
        $InputObject
    )

    $Name |
    ? {-not $InputObject.ContainsKey($_)} |
    % {$InputObject.Add($_, (gv $_ -Scope 1 -ValueOnly))}
}

这就是我喜欢做的事情:

foreach($localP in $MyInvocation.MyCommand.Parameters.Keys)
{
    if(!$PSBoundParameters.ContainsKey($localP))
    {
        $PSBoundParameters.Add($localP, (Get-Variable -Name $localP -ValueOnly))
    }        
}

暂无
暂无

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

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