如何在环境中启用 -one?如果我未指定“-one”,则它最终会出现在 opts 字符串中,或者会被忽略。
function test-run {
Param (
[switch]$one = $false,
[switch]$two = $false,
[switch]$tre = $false,
[string]$branch = "release",
[int]$forkid = -1,
[string]$opts = ""
)
"one: $one, two: $two, tre: $tre" | Out-Host
"branch: $branch, forkid: $forkid, opts: $opts" | Out-Host
}
$env:setupflag = "-one"
test-run $env:setupflag -two -tre -branch "test" -forkid 0 -opts ""
测试参数
一:False,二:True,tre:True
分支:测试,forkid:0,opts:
由于您正在调用 PowerShell 本机命令(函数),因此您尝试执行的操作需要基于哈希表进行展开。
如果 splatting 源是一个环境变量,那么这就不那么简单了:
对于主要问题,我将参考@mklement0 的回答,但不建议使用默认
[switch]
值$false
。这 几乎违背了他们的全部观点,因为它们是“默认关闭”的。使用
.IsPresent
开关参数的属性来代替:...除非你特别需要一个真正的布尔值,在这种情况下使用
[bool]
而不是[switch]