如何传递开关变量? [英] How to pass a switch variable?

查看:41
本文介绍了如何传递开关变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

& "$THIS_SCRIPTS_DIRECTORY_PATH\New-NuGetPackage.ps1" -PushOptions "$pushOptions" `
    -Verbose -ProjectFilePath $project -PO "$packOptions" -NPFPPTNG                

因此,如果我在 PowerShell 中提供上面的命令行,则调用可以正常工作.

So if I provide the command line above in PowerShell the call works correctly.

如果我尝试这样的事情:

If I try something like this:

if ($NoPromptForPushPackageToNuGetGallery) {
    $xtraOptions += " -NPFPPTNG "
}

& "$THIS_SCRIPTS_DIRECTORY_PATH\New-NuGetPackage.ps1" -PushOptions "$pushOptions" `
    -Verbose -ProjectFilePath $project -PO "$packOptions" $xtraOptions     

这失败了.如何在变量中传递开关?

this fails. How can I pass a switch in a variable?

推荐答案

您可以使用 喷溅:

$xtraOptions = @{}
if ($NoPromptForPushPackageToNuGetGallery) {
    $xtraOptions.Add("NPFPPTNG",$true)
}

& "$THIS_SCRIPTS_DIRECTORY_PATH\New-NuGetPackage.ps1" -PushOptions "$pushOptions" -Verbose -ProjectFilePath $project -PO "$packOptions" @xtraOptions

如果 $xtraOptions 只是一个空的哈希表,@xtraOptions 将不会对传递的参数产生任何影响.

If $xtraOptions is just an empty hashtable, @xtraOptions will simply have no effect on the parameters passed.

您也可以将所有参数推入带有条件值的 splatting 表:

You could also push all the parameters into the splatting table with a conditional value:

$nuGetOptions = @{
    PushOptions     = "$pushOptions"
    ProjectFilePath = $project 
    PO              = "$packOptions"
    Verbose         = $Verbose
    NPFPPTNG        = if($NoPromptForPushPackageToNuGetGallery) { $true } else { $false }
}

& "$THIS_SCRIPTS_DIRECTORY_PATH\New-NuGetPackage.ps1" @nuGetOptions

这篇关于如何传递开关变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆