views:

24

answers:

2

I have a nested switch statement inside a switch statement. I am wondering if it is possible to grab the un-nested current item within the scope of the nested switch statement. I know I can accomplish this with a for loop with moderate difficulty but I am hoping there is some elegance I am missing to accomplishing this with switch statements. Below I am try to build some argument processing for one of my scripts.

# Process Args
$currentArg
$recurseOn = $false

:ListArgs switch -regex ($args)
{
    "-ou?" { $currentArg = "Organizational Unit"; write-host "Noticed ou switch"}
    "-li?k?e?" { $currentArg = "Compare To"; write-host "Noticed like switch"}
    "^-re?c?u?r?s?e?$" { $recurseOn = $true; write-host "Recurse switched on..."}
    default {
        :CurrentSwitch switch ($currentArg)
        {
            "Organizational Unit" { $OU = $_.$_ ; write-host "OU changed to '$($OU)'..."}
            "Compare To" { $Match = $_.$_ ; write-host "Lookup changed to '$($Match)'..."}
        }
    }
}

I know the present model of $.$ does not work but I am looking for something that could refer to the current item. I am thinking I could have a filler variable that I just fill with the current item regardless of condition and use that within the scope, but maybe there is something more elegant. I have also tried labeling my switch statements and attempted 'ListArgs.$_' to no avail.

Any input would be much appreciated. Thanks in advance!

I did this after posting:

    # Process Args
$currentArg
$recurseOn = $false

:ListArgs switch -regex ($args)
{
    "-ou?" { $currentArg = "Organizational Unit"; write-host "Noticed ou switch"}
    "-li?k?e?" { $currentArg = "Compare To"; write-host "Noticed like switch"}
    "^-re?c?u?r?s?e?$" { $recurseOn = $true; write-host "Recurse switched on..."}
    default {
        $item = $_
        :CurrentSwitch switch ($currentArg)
        {
            "Organizational Unit" { $OU = $item; write-host "OU changed to '$($OU)'..."}
            "Compare To" { $Match = $item; write-host "Lookup changed to '$($Match)'..."}
        }
    }
}

And was able to get it working, but any input on $_ or loop labels would be appreciated. Or is there a better way to do arg processing in PowerShell?

A: 

I am thinking I could have a filler variable that I just fill with the current item regardless of condition and use that within the scope

Usually I do this and I do not know a better way. Thus, your code would be:

...
default {
    $filler = $_
    :CurrentSwitch switch ($currentArg)
    {
        "Organizational Unit" { use $filler in here }
        "Compare To" { use $filler in here }
    }
}
Roman Kuzmin
Ah, you have done it already after posting. The code is just fine, IMHO.
Roman Kuzmin
And indeed, the same trick will be needed in similar situations, not just `switch`, everything dealing with `$_`: `switch`, For-EachObject, $_ as event args in event handlers, and quite a few of others. `$_` is slightly overloaded too much sometimes.
Roman Kuzmin
+1  A: 

I guess since there are no better suggestions out there for argument processing, I answered my own question.

    $item = $_
Jered Odegard