2

Maybe I'm looking at this all wrong. But, I have a PowerShell function that I would like to test for the presence of an input parameter much like a switch. If the parameter is present, simply use/assign it a default value. Additionally, I would like for the user to be able to override the default and specify their own value. I can get it to work with some but not all of the above criteria with the following...

Function test {

[CmdletBinding()]
    Param (
        [Parameter(Mandatory=$false)]
        [ValidateRange(1,25)]
        [Int]
        $OptionalNumber = 1
    )

    if($PSBoundParameters.keys -contains "OptionalNumber"){
        Write-Output "The optional number parameter is present"
        Write-Output "The Optional Number is: $OptionalNumber"
    }else{Write-Output "The optional number parameter was not specified"}
}

Running "Test" works.

Running "Test -OptionalNumber 3" works.

Running "Test -OptionalNumber" fails.

I want to be able to use the "-OptionalNumber" and essentially use it as a switch without having to explicitly passing in the default value of 1 as specifying the value defeats the purpose of a default value.

I've tried various combinations of [ValidationScript()], [AllowNull()], [Nullable[int]] and a few others to no avail. I know I could break it up into two parameters, one as the switch and the other as a value... [Switch]$OptionalNumber and [Int]$OptionalNumberValue for instance. But that seems very clunky. I feel like there is a better/cleaner way.

EDIT: As suggested, I opened a feature request for this functionality to be added to PowerShell. https://github.com/PowerShell/PowerShell/issues/12104

Craig
  • 627
  • 7
  • 14
  • 1
    i cannot think of any way to do what you describe ... nor can i think of any reason WHY, but that is another subject. [*grin*] **_use two parameters since that is how the system is designed_**. – Lee_Dailey Feb 29 '20 at 00:17
  • @Lee_Dailey: What Craig is asking for _is_ useful, but currently (v7.0) unsupported in PowerShell; the [linked answer](https://stackoverflow.com/a/58848736/45375) hopefully provides an explanation and background information. – mklement0 Feb 29 '20 at 00:38
  • @mklement0 - the _rationale_ does not make sense to me. the code makes sense, just not the _why_ of it. that is just a limit on my part ... and not important. [*grin*] – Lee_Dailey Feb 29 '20 at 14:34
  • @Lee_Dailey: It's the concept of an _optional_ parameter _value_ - so you can use `-foo` alone to rely on a default value, or override it with `-foo:bar` if needed. That is how `[switch]` parameters technically already work, and this is the _generalization_ of that concept. A prominent example in the Linux world is GNU `sed`'s `-i` option, which creates a backup of the input file before modification: `-i` _by itself_ defaults to `.bak`, i.e. it creates the backup file with extension `.bak`, but you can use something like `-i.tmp` to specify a custom extension. – mklement0 Feb 29 '20 at 14:40
  • @mklement0 - that i understand. i simply cannot think of any use for it in the case the OP described. thank you for trying to help me understand, tho! [*grin*] – Lee_Dailey Feb 29 '20 at 15:15
  • @Lee_Dailey: Craig's question doesn't describe any particular real-world use case, just the desired logic. If the `sed -i` real-world example makes sense to you, that's all that's needed to understand that having support for this logic in PowerShell is desirable (if unlikely to be implemented). – mklement0 Feb 29 '20 at 16:46
  • @mklement0 - The use case I was looking for is to have a function that accepts several "switch" style parameters. If the parameter isn't set/present, then simply don't use it... essentially switch:$false. However, if it's specified, then essentially switch:$true... But, the parameter in question is also a variable. I would like to have a default value set for the variable... say 4 as an example. But, allow for the user to override that with say 8 for example. Now, 99% of the time the function will be run with default values, so I was just looking for a way to do that cleanly for the end user. – Craig Mar 11 '20 at 15:44
  • @mklement0 - Will do. thanks. :) Also, as I have little experience with using them, do you know if such capabilities might be possible through the use of dynamic parameters? – Craig Mar 11 '20 at 16:14
  • 1
    For anyone's reference.... https://github.com/PowerShell/PowerShell/issues/12104 – Craig Mar 11 '20 at 16:52
  • @Craig, no, I don’t think you can do it with dynamic parameters either. – mklement0 Mar 11 '20 at 18:24

0 Answers0