gpt4 book ai didi

PowerShell:位置参数和 ValueFromPipeline

转载 作者:行者123 更新时间:2023-12-02 23:25:55 26 4
gpt4 key购买 nike

我有一个玩具问题,感觉应该很简单。我想要一个像 Join-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
}

我尝试过以多种方式使用 ParameterSet(没有保存我的尝试)但没有成功,我现在有点难过。

想法?

最佳答案

这不能按您希望的方式工作。您的两个参数都是相同的数据类型,PowerShell 将使用它来绑定(bind)参数,然后再查看它是否进入管道,并尝试立即填充从 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 如何绑定(bind)参数,请使用 Trace-Command:

Trace-Command -Name ParameterBinding -Expression { Build-Path (Get-Location) Test } -Verbose -PSHost

无论您如何切分,都会遇到问题。没有 DefaultParameterSet?无法解析参数集。切换默认值?然后,您只需更改哪些调用有效或无效。

您也不能将 -Right 参数设为 [String[]] 数组,因为在绑定(bind)期间 PowerShell 将尝试强制转换,因此 [String ] 仍将被接受和绑定(bind)。

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

关于PowerShell:位置参数和 ValueFromPipeline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37015804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com