0

I know how to add switch parameters to PowerShell scripts, but the name of the switch is always a (valid) identifier like -Enable because the name is generated from the backing PowerShell variable.

[CmdletBinding()]
param(
  [switch] $Enable = $false
)

Some tools have switches like -2008. Normally, one would name the switch $2008 but this is not a valid identifier.

How can I implement such a switch as a boolean value in PowerShell? Or in other words: How to specify a different parameter name then the backing variable?


Edit 1

I wasn't aware of number only variables (which is very strange for a programming language...). Anyhow, I created that example:

function foo
{ [CmdletBinding()]
  param(
    [switch] $2008
  )
  Write-Host "2008=$2008"
}

For this code, which is accepted as valid PowerShell, I get a auto completion as wanted. But when providing that parameter, I get this error message:

foo : Es wurde kein Positionsparameter gefunden, der das Argument "-2008" akzeptiert.
In Zeile:1 Zeichen:1
+ foo -2008
+ ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [foo], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,foo

Translation: No positional parameter was found, that accepts the argument "-2008".

The purpose of the script is to provide translations / wrappers for command line interfaces.

Here is a set of standardized executables with parameters:

vcom.exe -2008 design.vhdl
vsim.exe -2008 design

Translation:

ghdl.exe -a --std=2008 design.vhdl
ghdl.exe -e --std=2008 design
ghdl.exe -r --std=2008 design

I would like to keep the feature of auto completion for parameters, otherwise I could process all remaining parameters and translate them by hand.

11
  • i dont understand, do you want the switch name to be exactly $true? Commented Sep 8, 2017 at 14:51
  • Normally, you would name the switch $2008 but this is not a valid identifier. So how to specify a different parameter name then the backing variable? Commented Sep 8, 2017 at 14:53
  • 5
    This can't be allowed because it creates ambiguity with parsing negative integers -- is -2008 a value for an unnamed parameter or a switch named 2008? I'm not unhappy PowerShell only allows the former. If these switches make sense in your context, consider just tweaking them so they are valid -- like -Y2008 if it's supposed to be year, -W2008 if it's supposed to enable a warning, etcetera. Commented Sep 8, 2017 at 15:02
  • 1
    funny enough, it works if you splat it so, $p=@{2008=$true}, function @p, you don't even need ${2008}, $2008 also works Commented Sep 8, 2017 at 15:06
  • 1
    @JeroenMostert of course, that makes sense. I'd probably go for a string (or string[] if values are not mutually exclusive) parameter with a limited ValidateSet attribute. You should add an answer btw Commented Sep 8, 2017 at 15:08

1 Answer 1

1

PowerShell doesn't support numeric parameters (See this answer).

Could a validateset be an acceptable solution to your problem ?

Validate set does benefit from the autocompletion feature and this is the next best thing, in my opinion, of what you wanted.

ValidateSet and auto-completion

Foo & Foos — both are the same, except Foos accept multiple parameters.

foo -std 2008
foos -std 2008,2009,2010





    function foo
{ [CmdletBinding()]
  param(
    [ValidateSet("2008",
                 "2009",
                 "2010",
                 "2011","2012")][INT]$std
  )
  Write-Host "std:$std"
}

function foos
{ [CmdletBinding()]
  param(
    [ValidateSet("2008",
                 "2009",
                 "2010",
                 "2011","2012")][INT[]]$std
  )
  $std | foreach {Write-Host "std: $_"}
}





foo -std 2008
foos -std 2008,2009,2010
Sign up to request clarification or add additional context in comments.

3 Comments

The original program e.g. vcom does not have a -std switch in front of the version number. More over the - is missing. So I think I need to use another solution like a Python script with ArgParse.
If it's a wrapper to the real exe, can't you just call it afterward using &"YourWrappedapp.exe -$std" so you are effectively using foo -std 2008 to call "YourWrappedApp.exe -2008" ? or am I missing something ?.
The wrapper (written in PowerShell) shall emulate the interface of vcom so it must provide a -2008 switch. internally it will call ghdl.exe with the switch --std=2008 if -2008 was set on the wrapper. The purpose is that existing simulator scripts for the vcom/vlib/vsim calls can be reused for the GHDL simulator, that uses a different CLI, but offers the same commands (analyze, elaborate, run simulation).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.