【问题标题】:Accepting an optional parameter only as named, not positional仅接受命名的可选参数,而不是位置参数
【发布时间】:2013-06-11 10:48:20
【问题描述】:

我正在编写一个 PowerShell 脚本,它是 .exe 的包装器。我想要一些可选的脚本参数,并将其余的直接传递给 exe。这是一个测试脚本:

param (
    [Parameter(Mandatory=$False)] [string] $a = "DefaultA"
   ,[parameter(ValueFromRemainingArguments=$true)][string[]]$ExeParams   # must be string[] - otherwise .exe invocation will quote
)

Write-Output ("a=" + ($a) + "  ExeParams:") $ExeParams

如果我使用命名参数运行,一切都很好:

C:\ > powershell /command \temp\a.ps1 -a A This-should-go-to-exeparams This-also
a=A  ExeParams:
This-should-go-to-exeparams
This-also

但是,如果我尝试省略我的参数,则会将第一个未命名的参数分配给它:

C:\ > powershell /command \temp\a.ps1 This-should-go-to-exeparams This-also
a=This-should-go-to-exeparams  ExeParams:
This-also

我希望:

a=DefaultA ExeParams:
This-should-go-to-exeparams
This-also

我尝试将Position=0 添加到参数中,但这会产生相同的结果。

有没有办法做到这一点?
也许是不同的参数方案?

【问题讨论】:

    标签: powershell parameters optional-parameters


    【解决方案1】:

    默认情况下,所有函数参数都是位置参数。 Windows PowerShell 按照在函数中声明参数的顺序将位置编号分配给参数。要禁用此功能,请将CmdletBinding 属性的PositionalBinding 参数的值设置为$False

    看看at How to disable positional parameter binding in PowerShell

    function Test-PositionalBinding
    {
        [CmdletBinding(PositionalBinding=$false)]
        param(
           $param1,$param2
        )
    
        Write-Host param1 is: $param1
        Write-Host param2 is: $param2
    }
    

    【讨论】:

    • 我无法让它在 Powershell 2.0 中工作 - 这是预期的吗?
    • 我认为这在 Powershell 2.0 中不起作用(链接的博客文章说它不起作用,并指的是this workaround
    【解决方案2】:

    主要答案在版本 5 中仍然有效(根据 cmets 的说法,它可能在版本 2 中已经被破坏了一段时间)。

    还有另一种选择:将 Position 添加到 ValueFromRemainingArgs 参数中。

    CommandWrapper.ps1 示例:

    param(
        $namedOptional = "default",
        [Parameter(ValueFromRemainingArguments = $true, Position=1)]
        $cmdArgs
    )
    
    write-host "namedOptional: $namedOptional"
    & cmd /c echo cmdArgs: @cmdArgs
    

    样本输出:

    >commandwrapper hello world
    namedOptional: default
    cmdArgs: hello world
    

    这似乎遵循 PowerShell 从指定位置的第一个参数分配参数位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-17
      • 2016-07-06
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      相关资源
      最近更新 更多