通用规范缺省值不在PsBoundParameters中的参数?
考虑以下代码:
PS> function Test { param($p='default value') $PsBoundParameters }
PS> Test 'some value'
Key Value
--- -----
p some value
PS> Test
# nothing
我预计$PsBoundParameters
将含有记录在这两种情况下$p
变量。这是正确的行为吗?
问题
我想用泼洒,将工作像这样的很多功能:
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与所列举的所有PARAMS:
SomeFuncWithManyRequiredParams -p1 $p1 -p2 $p2 -p3 $p3 ...
这可能吗?
有两个问题我不得不问在这里,每个参数的问题:为什么SomeFunc不具有与SimplifiedFunc相同的默认值?而且,如果默认值需要不同,那么为什么SimplifiedFunc允许用户覆盖默认值(因为在这种情况下,他们可以直接调用SomeFunc来覆盖)? – jpaugh 2017-02-01 15:55:36