2015-06-17 43 views
3

我希望创建一个参数,其默认值是'当前目录'(.)。创建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属性?

+0

你确定不是全部只是函数内部的魔法和一个字面默认值“当前目录”? (我认为它并不那么愚蠢,并且是明确的,但我也认为你可能无法直接在PowerShell中复制它。) –

+0

我认为这是可能的,但对我来说这似乎很sl。。那个值('Current directory')会根据Windows本地化而改变吗? – craig

+0

我假设字符串是本地化的,不管内部如何实现。这就是说,我不指望与内部使用该值相比较。我期望使用“是一个有效的值”测试,因为'$ null'在那里没有意义。 –

回答

7

使用PSDefaultValue属性来定义默认值的自定义描述。使用SupportsWildcards属性将参数标记为Accept wildcard characters?

<# 
.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)] 
     [PSDefaultValue(Help='Description for default value.')] 
     [SupportsWildcards()] 
     [string[]]$Path='.' 
    ) 
} 
+3

我认为Microsoft不会因为更新文档而烦恼吗? * \ *叹息\ ** –

+1

[相关](http://blogs.msdn.com/b/powershell/archive/2012/06/14/new-v3-language-features.aspx)。 – craig

+0

我会将其视为“是”。 ;) –