Switch Parameters and FlagAttribute Enumerations

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 4 months ago.

  • Author
    Posts
  • #5933

    by jonhtyler at 2012-10-17 07:30:13

    I am trying to find an efficient way to process switch parameters into a FlagAttribute enumeration. For instance, consider the following enumeration in the Microsoft.TeamFoundation.VersionControl.Client namespace:

    GetOptions.None
    GetOptions.GetAll
    GetOptions.Overwrite
    GetOptions.Preview
    GetOptions.Remap

    I would like to put switch params in the function signature for each option:

    Function Initialize-MyFunction
    {
    Param(
    [switch]$GetAll,
    [switch]$Overwrite,
    [switch]$Preview,
    [switch]$Remap
    )

    }

    I know that I can use a series of "if" statements to perform the "-bor" operation against each enumeration option and assign back to a variable. However, I would like to find something a little more elegant and/or efficient to handle this. Any ideas?

    by mjolinor at 2012-10-17 07:37:35

    Make it an Advanced Function, enable cmdletbinding, and run the $psboundparameter keys thorugh a switch statement.

    by jonhtyler at 2012-10-17 07:56:09

    Based on mjolinor's advice, this is a snippet of what I came up with:

    Function Initialize-MyFunction
    {
    [CmdletBinding()]
    Param(
    [switch]$GetAll,
    [switch]$Overwrite,
    [switch]$Preview,
    [switch]$Remap
    )
    $GetOptions = [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::None
    switch ($PSBoundParameters.keys)
    {
    "GetAll"
    {
    $GetOptions = $GetOptions -bor [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::GetAll
    }
    "Overwrite"
    {
    $GetOptions = $GetOptions -bor [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite
    }
    "Preview"
    {
    $GetOptions = $GetOptions -bor [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Preview
    }
    "Remap"
    {
    $GetOptions = $GetOptions -bor [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Remap
    }
    }
    Write-Output $GetOptions
    }

    by mjolinor at 2012-10-17 08:10:32

    Hmm. Does this work?

    Function Initialize-MyFunction
    {
    [CmdletBinding()]
    Param(
    [switch]$GetAll,
    [switch]$Overwrite,
    [switch]$Preview,
    [switch]$Remap
    )

    $Options = [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]
    $GetOptions = $Options::None

    @($PSBoundParameters.keys) -match 'GetAll|Overwrite|Preview|Remap' |
    foreach {$GetOptions = $GetOptions -bor $Options::$_}

    Write-Output $GetOptions
    }

    by jonhtyler at 2012-10-17 08:15:51

    That actually works great...more along what I was looking for! Nice, compact code! I will opt to use your code instead of mine. Thanks again!

    by mjolinor at 2012-10-17 08:20:23

    Thanks! That @($PSBoundParameters.keys) might look a little odd, but if you don't do that the -match will return $true if there's just one parameter, instead of the parameter name.

    by DexterPOSH at 2012-10-22 16:33:42

    This one is really cool...I was searching for something similar.
    Great solution...@Mjolinor

You must be logged in to reply to this topic.