Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In powershell, is it possible to enforce naming of arguments?

Tags:

powershell

Given a script foo.ps1: param($x,$y) return $x/$y

Is it possible to enforce explicit parameter naming when calling it?

./foo.ps1 5 10 would generate an error ./foo.ps1 -x 5 -y 10 would be OK

like image 678
sthiers Avatar asked Jan 20 '23 15:01

sthiers


1 Answers

This code works but it uses something not documented (I could not find anything about negative positions):

function Test
{
    param(
        [Parameter(Position=-1)]
        $x
        ,
        [Parameter(Position=-1)]
        $y
    )
    $x/$y
}

Test -x 1 -y 2
Test -y 2 -x 1
Test 1 2

Output:

0.5
0.5
Test : Cannot bind positional parameters because no names were given.
At C:\TEMP\_110127_170853\q1.ps1:15 char:5
like image 153
Roman Kuzmin Avatar answered Jan 26 '23 00:01

Roman Kuzmin