我有一个玩具问题,感觉应该很简单。我想拥有一个支持管道的Join-Path等功能。称之为Build-Path。因此,这些陈述(我的测试1-4)应该是等同的:
Get-Location | Build-Path Test
Get-Location | Build-Path -Right Test
Build-Path -Left (Get-Location) -Right Test
Build-Path (Get-Location) Test
我已经尝试了很多东西,最接近我可以获得这两个功能(互斥):
A:两个值的显式位置;未通过测试1
function Build-Path
(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[String] $Left,
[Parameter(Mandatory=$true, Position=1)]
[String] $Right
)
{
Join-Path $Left $Right
}
B:管道参数没有位置,Right的位置为0;未通过测试4
function Build-Path
(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[String] $Left,
[Parameter(Mandatory=$true, Position=0)]
[String] $Right
)
{
Join-Path $Left $Right
}
我尝试过多种方式使用ParameterSets(没有保存我的尝试)但没有成功,我现在有点难过。
思想?
答案 0 :(得分:3)
这不能按你想要的方式工作。你的两个参数都是相同的数据类型,PowerShell会在查看它是否进入管道之前使用它来绑定参数,并且它会尝试立即从0开始填充位置。
我甚至尝试使用参数集重做它:
function Build-Path {
[CmdletBinding(DefaultParameterSetName='Default',PositionalBinding=$false)]
param(
[Parameter(
ParameterSetName='Default',
Mandatory=$true,
ValueFromPipeline=$true
)]
[Parameter(
ParameterSetName='LooseyGoosey',
Mandatory=$true,
Position=0
)]
[String]
$Left,
[Parameter(
ParameterSetName='Default',
Mandatory=$true,
Position=0
)]
[Parameter(
ParameterSetName='LooseyGoosey',
Mandatory=$true,
Position=1
)]
[String]
$Right
)
Join-Path $Left $Right
}
要了解PowerShell如何绑定参数,请使用Trace-Command
:
Trace-Command -Name ParameterBinding -Expression { Build-Path (Get-Location) Test } -Verbose -PSHost
无论你如何切片,你都会遇到问题。没有DefaultParameterSet
?参数集无法解析。切换默认值?然后你只是改变哪些调用工作或不工作。
您也无法将-Right
参数设为[String[]]
数组,因为在绑定期间PowerShell会尝试强制执行,因此[String]
仍将被接受并绑定。
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Get-Location]
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Get-Location]
DEBUG: ParameterBinding Information: 0 : BIND cmd line args to DYNAMIC parameters.
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Get-Location]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Build-Path]
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Build-Path]
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\Users\Briantist] to parameter [Right]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: C:\Users\Briantist
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\Users\Briantist] to param [Right] SKIPPED
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\Users\Briantist] to parameter [Left]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: C:\Users\Briantist
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\Users\Briantist] to param [Left] SKIPPED
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\Users\Briantist] to parameter [Right]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: C:\Users\Briantist
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\Users\Briantist] to param [Right] SKIPPED
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\Users\Briantist] to parameter [Right]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: C:\Users\Briantist
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\Users\Briantist] to param [Right] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [Test] to parameter [Right]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: Test
DEBUG: ParameterBinding Information: 0 : BIND arg [Test] to param [Right] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Build-Path]
DEBUG: ParameterBinding Information: 0 : PROMPTING for missing mandatory parameters using the host