Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accepting an optional parameter only as named, not positional

I'm writing a PowerShell script that's a wrapper to an .exe. I want to have some optional script params, and pass the rest directly to the exe. Here's a test script:

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

If I run with the a named param, everything is great:

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

However, if I try to omit my param, the first unnamed param is assigned to it:

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

I would expect:

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

I tried adding Position=0 to the param, but that produces the same result.

Is there a way to achieve this?
Maybe a different parameter scheme?

like image 620
Jonathan Avatar asked Jun 11 '13 10:06

Jonathan


People also ask

What is the difference between named and positional parameters?

A positional parameter is linked by its position. Positional parameters must be specified in the order in which they appear. Named parameters are specified by assigning values to their names. named parameters can be assigned initial values by using their names.

How do you pass optional parameters while omitting some other optional parameters?

you can use optional variable by ? or if you have multiple optional variable by ... , example: function details(name: string, country="CA", address?: string, ...hobbies: string) { // ... } In the above: name is required.

What is the difference between named and positional parameters in PowerShell?

Positional and Named ParametersA named parameter requires that you type the parameter name and argument when calling the cmdlet. A positional parameter requires only that you type the arguments in relative order. The system then maps the first unnamed argument to the first positional parameter.

How do you give optional parameters in darts?

A parameter wrapped by { } is a named optional parameter. Also, it is necessary for you to use the name of the parameter if you want to pass your argument.


1 Answers

By default, all function parameters are positional. Windows PowerShell assigns position numbers to parameters in the order in which the parameters are declared in the function. To disable this feature, set the value of the PositionalBinding argument of the CmdletBinding attribute to $False.

have a look 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
}
like image 172
JPBlanc Avatar answered Nov 16 '22 00:11

JPBlanc