我希望创建一个参数,其默认值是'当前目录'(.
)。创建powershell参数默认值是当前目录
例如,Path
参数的Get-ChildItem
:
PS> Get-Help Get-ChildItem -Full
-Path 指定到一个或多个位置的路径。通配符是允许的。默认位置是当前的 目录(。)。
Required? false Position? 1 Default value Current directory Accept pipeline input? true (ByValue, ByPropertyName) Accept wildcard characters? true
我创建与从管道接受输入一个Path
参数的函数,具有.
一个缺省值:
<#
.SYNOPSIS
Does something with paths supplied via pipeline.
.PARAMETER Path
Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).
#>
Function Invoke-PipelineTest {
[cmdletbinding()]
param(
[Parameter(Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
[string[]]$Path='.'
)
BEGIN {}
PROCESS {
$Path | Foreach-Object {
$Item = Get-Item $_
Write-Host "Item: $Item"
}
}
END {}
}
然而,.
不被解释为“当前目录'in help:
PS> Get-Help Invoke-PipelineTest -Full
-Path 指定一个或多个位置的路径。通配符是允许的。默认位置是当前目录(。)。
Required? false Position? 1 Default value . Accept pipeline input? true (ByValue, ByPropertyName) Accept wildcard characters? false
什么是对Path
参数的默认值设置为当前目录的正确方法?
顺便提一下,在哪里设置Accept wildcard character
属性?
你确定不是全部只是函数内部的魔法和一个字面默认值“当前目录”? (我认为它并不那么愚蠢,并且是明确的,但我也认为你可能无法直接在PowerShell中复制它。) –
我认为这是可能的,但对我来说这似乎很sl。。那个值('Current directory')会根据Windows本地化而改变吗? – craig
我假设字符串是本地化的,不管内部如何实现。这就是说,我不指望与内部使用该值相比较。我期望使用“是一个有效的值”测试,因为'$ null'在那里没有意义。 –