Parsing 如何处理powershell中参数的多个选项?
我希望能够有相同参数的多种形式,如下所示:Parsing 如何处理powershell中参数的多个选项?,parsing,powershell,parameters,arguments,powershell-2.0,Parsing,Powershell,Parameters,Arguments,Powershell 2.0,我希望能够有相同参数的多种形式,如下所示: param( [string]$p or $path = "C:\", [string]$f or $filter = "*.txt", [switch]$o or $overwrite ) 但我不知道该怎么做。大多数情况下,您只能选择一个(例如,仅$p或仅$path)。是否可以对同一变量/参数使用多个名称?PowerShell部分参数名称匹配可能是您需要的 # test.ps1 param($path) write-host $path
param(
[string]$p or $path = "C:\",
[string]$f or $filter = "*.txt",
[switch]$o or $overwrite
)
但我不知道该怎么做。大多数情况下,您只能选择一个(例如,仅$p或仅$path)。是否可以对同一变量/参数使用多个名称?PowerShell部分参数名称匹配可能是您需要的
# test.ps1
param($path)
write-host $path
使用
\test.ps1-path“c:\windows”
或\test.ps1-p“c:\windows”
调用。\test.ps1都将匹配并填充$path参数。PowerShell部分参数名称匹配可能是您需要的
# test.ps1
param($path)
write-host $path
使用\test.ps1-path“c:\windows”
或\test.ps1-p“c:\windows”
调用。\test.ps1将匹配并填充$path参数。如下:
param(
[Alias('p')]
[string]$path = "C:\",
[Alias('f')]
[string]$filter = "*.txt",
[Alias('o')]
[switch]$overwrite
)
注意:您也可以有多个别名:[别名('p','thepath')]
如下所示:
param(
[Alias('p')]
[string]$path = "C:\",
[Alias('f')]
[string]$filter = "*.txt",
[Alias('o')]
[switch]$overwrite
)
注意:您也可以有多个别名:[别名('p','thepath')]