函数参数名称中的点

Dots in function parameter name

powershell 中可以使用带点的参数名称吗?显而易见的方法 - 请参阅 Dmaven.failsafe.debug 失败:

function mvn-failsafe-debug {
  param (
    [string] $Dmaven.failsafe.debug="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_shmem,server=y,address=Maven,suspend=n",
    [parameter(Position=0, ValueFromRemainingArguments=$true)]
    $args
  )
  & "$env:M2_HOME\bin\mvn.bat" $args
}

你应该能够使用的符号是花括号

${Dmaven.failsafe.debug}

当名称包含特殊字符时使用。输入 camelCase 通常是定义变量的首选方法。

Windows PowerShell Language Specification Version 3.02.3.4 参数 部分说

parameter-char:
Any Unicode character except
    {   }   (   )   ;   ,   |   &   .   [
    colon
    whitespace
    new-line-character

因此,点并不是真正有效的参数名称字符。

有趣的是,可以用点来定义参数,例如 ${...}

param (
    [string] ${Dmaven.failsafe.debug}
)

PowerShell 允许上述操作。但是在调用命令时很难指定这样的参数名称。


一些实验:

function Test-ParameterWithDots {
    param(
        [string]${Parameter.With.Dots}
    )
    "Parameter : ${Parameter.With.Dots}"
}

# OK
Test-ParameterWithDots value1

# not OK
Test-ParameterWithDots -Parameter.With.Dots value2

# workaround with splatting
$params = @{ 'Parameter.With.Dots' = 'value3' }
Test-ParameterWithDots @params

输出:

Parameter : value1
Parameter : .With.Dots
Parameter : value3

所以对于 spatting 我们仍然可以指定这样的参数名称。