2013-01-10 20 views
1

我希望能有相同的參數的多種形式,像這樣:如何在PowerShell中處理參數的多個選項?

param(
    [string]$p or $path = "C:\", 
    [string]$f or $filter = "*.txt", 
    [switch]$o or $overwrite 
) 

,但我不知道如何做到這一點。大多數情況下,您只能選擇一個(例如,只有$ p或只有$路徑)。是否可以爲同一個變量/參數使用多個名稱?

回答

3

像這樣:

param(
    [Alias('p')] 
    [string]$path = "C:\", 
    [Alias('f')] 
    [string]$filter = "*.txt", 
    [Alias('o')] 
    [switch]$overwrite 
) 

注意,你可以有多個別名太:[Alias('p','thepath')]

3

PowerShell部分參數名稱匹配可能是您要查找的內容。

# test.ps1 
param($path) 
write-host $path 

呼叫。\ test.ps1有兩種.\test.ps1 -path "c:\windows".\test.ps1 -p "c:\windows"都將匹配,並填充,在$ path參數。

相關問題