What is the best way to pass your param

Currently i have two ways to pass a parameter :


[CmdletBinding()]
param (
    

    #find the nearest places between you
    [Parameter(parametersetname="find")]
    [validateset([places])]
    [ArgumentCompleter({ MyArgumentCompleter @args })]
    [string]
    $find


,
# add a place in powershell
[Parameter(parametersetname="add")]
[validateset([places])]
[ArgumentCompleter({ MyArgumentCompleter @args })]
[string]
$add
,
# remove a place in powershell
[Parameter(parametersetname="remove")]
[int]
$remove
,
# Parameter help description
[Parameter()]
[bool]
$force

  )


switch ($pscmdlet.parametersetname) {
    
    
    
    find {   find -type $find}
    action{$action}
    add {add -type $type -force $force }
    remove {remove -id $remove }
    Default {}
}

and call it like this
GPS.ps1-find places
GPS.ps1 -calc road

[CmdletBinding()]
param (
    [Parameter()]
    [string]
    $type
    ,
    # Parameter help description
    [Parameter()]
    [string]
    $value
)
switch ($type) {
    calc {calc -type $value }
    find { find -type $value}
    remove {remove -id $value  }
    add { add -type $value -force $force }
    Default {}
}

and call it like this

GPS.ps1 -type find -value places
or
GPS.ps1 find places

The first one is simpler
you only need to pass one parameter
but you can’t add a none value parameter like this
GPS.ps1 -calcroad

The second one is more complicated
but you can pass a none value parameter like this
GPS.ps1 -type calcroad
and you could pass the parameter without its name

So i am struggle to choose between two of them ,or anyother suggestion?

Not 100% sure I’ve grasped the question, but my feeling is you’re doing two different things in one script.

I would separate each action into its own function. Save the script containing the functions as a module file* (GPS.psm1), import the module, then call the individual functions with their own parameters.

Example

function Find-Place {
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $PlaceName
    )

    "$PlaceName is in England."
}

function Add-Place {
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $PlaceName
    )

    "$PlaceName has been added to the database with record id 42."
}

function Remove-Place {
    [CmdletBinding()]
    param (
        [Parameter()]
        [int]
        $RecordId
    )

    "Record Id: $RecordId has been removed from the database."
}

function Get-Distance {
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $Start,
        [Parameter()]
        [string]
        $End
    )

    "The distance between $Start and $End is 19 miles"
}
PS E:\Temp\Test> Import-Module .\GPS.psm1
PS E:\Temp\Test> Add-Place -PlaceName 'Sunnydale'
Sunnydale has been added to the database with record id 42.
PS E:\Temp\Test> Remove-Place 42
Record Id: 42 has been removed from the database.
PS E:\Temp\Test> Get-Distance -Start 'London' -End 'Sydney'
The distance between London and Sydney is 19 miles
PS E:\Temp\Test>

*This is the simplest type of module, you could create a ‘proper’ module with a manifest and separate files for each function and a structure to separate public/private functions. I would encourage you to research this.

2 Likes

What i want to do is to decrease the parmeters i need to input ,so my script looks cleaner and easy to call

as for your answer

this is excatly what i do for the script i am writting right now

i create a gps.psm1 for my script the seperate the function for each method i use

which seperate the $type parameter in my script into different functions
i find it easy and simple to achieve.

function find-place{
param (
[array]$coordinate
[string]$Dimensions
[string]$map
[string]$type

)

}

function add-place{
param (
[array]$coordinate
[string]$Dimensions
[string]$map
[string]$type

)

}

function calc-route{
param (
[array]$thecoordinate
[string]$Dimensions
[string]$map
[string]$type


}

)

function remove-place{
param (
[int]$ID
[string]$Dimensions
[string]$map
[string]$type

)

}

i have also try combine parameter into one like this

 function teleportation-route {
      [CmdletBinding()]
      param (
        #parameter dimentions for overworld
          [Parameter()]
          [string]
          $overworld
          ,
    #parameter dimentions for nether
       [Parameter()]
       [string]
       $nether
       ,
    #parameter dimentions for the end
       [Parameter()]
       [string]
       $the_End
       ,
       # Parameter help description
       [Parameter()]
       [string]
       $map
       ,
       # the coordinate for destination
       [Parameter()]
       [array]
       $destination

      )

{
...
}
teleportation-route -overworld home -map newword -destinaton (1,2,3)

which combine $type paramter and $dimension parameter into one parameter

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.