为什么“switch"的值是类型参数传递给字符串参数? [英] Why is the value of "switch" type parameter passed to a string parameter?

查看:105
本文介绍了为什么“switch"的值是类型参数传递给字符串参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试代码(test.ps1):

参数([字符串]$content = $null,[开关]$flag = $false)写主机 $content

输出:

<前>PS C:\Users\powershell> .\test.ps1PS C:\Users\powershell> .\test.ps1 -flag $true真的PS C:\Users\powershell> .\test.ps1 -flag $false错误的PS C:\Users\powershell> .\test.ps1 -flag $true -content "lalala"拉拉拉

无论我将$content设置为$null,还是"",或者没有任何默认值,输出都是一样的.

那么为什么 $content$flag 中取值?

解决方案

@PetSerAl 已经指出你的误解是什么,但可能有点太简短了,所以我再详细说明一下.>

[switch] 参数不像常规参数那样采用值.您通常通过提供或省略参数将其分别设置为 true 或 false:

<前>PS C:\> 获取内容 .\test.ps1参数([字符串]$Content = $null,[开关]$Flag)$flag.IsPresent$内容PS C:\> .\test.ps1错误的PS C:\> .\test.ps1 -Flag真的

或者,您可以通过在 switch 参数和值之间放置一个冒号来显式传递一个值:

<前>PS C:\> .\test.ps1 -Flag:$false错误的PS C:\> .\test.ps1 -Flag:$true真的

允许冒号后有空格,但前不能有空格(-Flag: $false$false 作为开关值,-Flag :$false 没有).

如果您尝试分配开关值没有冒号,则该值实际上会传递给行中的下一个参数,在您的情况下为 -Content 参数:<前>PS C:\> .\test.ps1 -Flag $falseTrue # 参数 $Flag 的值False #(位置)参数的值 $ContentPS C:\> .\test.ps1 -Flag -Content $falseTrue # 参数 $Flag 的值False #(命名)参数的值 $Content

如果您使用 -Flag $true(不带冒号)并且将一个值传递给(命名)参数 -Content 该值$true 作为未命名的第三个参数传递(可通过 自动变量 $args),就像你把值放在语句的末尾一样:

<前>PS C:\> 获取内容 .\test2.ps1参数([字符串]$Content = $null,[开关]$Flag)$flag.IsPresent$内容$args[0]PS C:\> .\test2.ps1 -Flag $false -Content 'something'真的某物False # $false 作为未命名的第三个参数传递PS C:\> .\test2.ps1 -Flag:$false -Content 'something'False # $false 作为开关参数 $Flag 的显式值传递某物PS C:\> .\test2.ps1 -Flag -Content 'something' $false真的某物False # $false 作为未命名的第三个参数传递

The test code (test.ps1):

Param(
    [string]$content = $null,
    [switch]$flag = $false
)
Write-Host $content

Output:

PS C:\Users\powershell> .\test.ps1

PS C:\Users\powershell> .\test.ps1 -flag $true    
True
PS C:\Users\powershell> .\test.ps1 -flag $false
False
PS C:\Users\powershell> .\test.ps1 -flag $true -content "lalala"
lalala

No matter I set $content to $null, or "", or without any default value, the output is the same.

So why does $content take the value from $flag?

解决方案

@PetSerAl already indicated what your misunderstanding is, but maybe it was a bit too brief, so I'll elaborate a little more.

A [switch] parameter doesn't take a value like regular parameters. You normally set it to true or false respectively by providing or omitting the parameter:

PS C:\> Get-Content .\test.ps1
Param(
    [string]$Content = $null,
    [switch]$Flag
)
$flag.IsPresent
$Content
PS C:\> .\test.ps1
False

PS C:\> .\test.ps1 -Flag
True

Alternatively you can explicitly pass a value by putting a colon between switch parameter and value:

PS C:\> .\test.ps1 -Flag:$false
False

PS C:\> .\test.ps1 -Flag:$true
True

Whitespace after the colon is allowed, but not before (-Flag: $false passes $false as the switch value, -Flag :$false doesn't).

If you try to assign the switch value without the colon the value is actually passed to the next parameter in line, in your case the -Content parameter:

PS C:\> .\test.ps1 -Flag $false
True         # value of the parameter $Flag
False        # value of the (positional) parameter $Content
PS C:\> .\test.ps1 -Flag -Content $false
True         # value of the parameter $Flag
False        # value of the (named) parameter $Content

If you use -Flag $true (without the colon) and pass a value to the (named) parameter -Content the value $true is passed as an unnamed third parameter (accessible via the automatic variable $args), same as if you put the value at the end of the statement:

PS C:\> Get-Content .\test2.ps1
Param(
    [string]$Content = $null,
    [switch]$Flag
)
$flag.IsPresent
$Content
$args[0]
PS C:\> .\test2.ps1 -Flag $false -Content 'something'
True
something
False        # $false passed as unnamed 3rd parameter
PS C:\> .\test2.ps1 -Flag:$false -Content 'something'
False        # $false passed as explicit value of switch parameter $Flag
something
PS C:\> .\test2.ps1 -Flag -Content 'something' $false
True
something
False        # $false passed as unnamed 3rd parameter

这篇关于为什么“switch"的值是类型参数传递给字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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