我想用动态数量的参数调用现有的命令行开关。
因此,我不想这样做(以 write-host 为例),而是想以聪明的方式来做。
# these are the dynamic parameters which maybe get passed into my function or script
# they would be $null be default of course
$forecolor = 'Green'
$newline = $true
# now build the "dynamic" write-host...
if ($forecolor) {
if ($newline) {
write-host -fore $forecolor "Hello world"
}
else {
write-host -fore $forecolor "Hello world" -nonewline
}
}
else {
if ($newline) {
write-host "Hello world"
}
else {
write-host "Hello world" -nonewline
}
}
这当然是非常丑陋的。帮我把它变得更漂亮!
我已经尝试设置$forecolor = '-fore Green'
只输出"-fore Green Hello world"。我可以考虑将参数列表传递给函数,并为列表中的每个参数添加相应的参数 - 我只是不知道如何保存参数。