Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dots in function parameter name

Is a parameter name with dots possible in powershell? The obvious approach - see Dmaven.failsafe.debug fails:

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
}
like image 903
daniel.kahlenberg Avatar asked Dec 01 '25 06:12

daniel.kahlenberg


2 Answers

Windows PowerShell Language Specification Version 3.0. The section 2.3.4 Parameters says

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

Thus, the dot is not really a valid parameter name character.

Interestingly, it is possible to define a parameter with dots like ${...}

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

PowerShell allows the above. But it is difficult to specify such a parameter name on invoking a command.


Some experiments:

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

Output:

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

So with spatting we still can specify such a parameter name.

like image 156
Roman Kuzmin Avatar answered Dec 03 '25 23:12

Roman Kuzmin


The notation you should be able to use to allow this is with curly braces

${Dmaven.failsafe.debug}

This is used when the name contains special characters. Typing in camelCase is usually to preferred method of defining variables.

like image 32
Matt Avatar answered Dec 04 '25 00:12

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!