Searching for PowerShell syntax for multiple enum values in the case block of a switch statement?

huangapple go评论67阅读模式
英文:

Searching for PowerShell syntax for multiple enum values in the case block of a switch statement?

问题

这是您要翻译的内容:

enum Fruit {
    Apple
    Banana
    Orange
}

$myFruit = [Fruit]::$UsrString

switch ($myFruit) {
    Apple, Orange {
        Write-Host "这是苹果或橙子"
        break
    }
    Banana {
        Write-Host "这是香蕉"
        break
    }
    default {
        Write-Host "未知水果: $myFruit"
        break
    }
}

您可以将这段 PowerShell 代码用于枚举类型的多项情况。

英文:

An example of what I want to do:

	enum Fruit {
	    Apple
	    Banana
	    Orange
	}

	$myFruit = [Fruit]::$UsrString

	switch ($myFruit) {
	    Apple, Orange {
		Write-Host "This is an apple or an orange"
		break
	    }
	    Banana {
		Write-Host "This is a banana"
		break
	    }
	    default {
		Write-Host "Unknown fruit: $myFruit"
		break
	    }
	}

But each attempt I have made has yet to work as intended, if at all.

I have searched for anything to do with Powershell, switch-statements, and enums but have yet to find the answer.

I have reviewed this link on StackOverflow.

I have successfully used the "switch -regEx (someEnum)" as long as I use the integer values of the enum items. However, that essentially defeats the use of the enum (ie. labeled ints).

My intent was to keep the switch as efficient as possible (ie. switching on simple int values; staying away from the more common string-compare style PS switch usage, just to keep performance up).

Does the multi-item case block syntax exist for enum usage in powershell?

Thanks in advance for any information.

答案1

得分: 2

switch语句:

  • 不支持 多个分支条件,但是...

  • 支持 使用 脚本块 ({ ... }) 作为分支条件,在其中您可以自由执行任何测试,包括多个值的测试,使用 自动 $_ 变量 来引用手头的输入值。

因此:

switch ($myFruit) {
  { $_ -in 'Apple', 'Orange' } {
    "这是苹果或橙子"
    break
  }
  Banana {
    "这是香蕉"
    break
  }
  default {
    "未知水果: $_"
  }
}

注意:

  • 如您在上面的 default 分支的 动作 脚本块中所看到的,$_ 也在那里引用输入值。

    • 通常情况下,使用 $_ 更可取,因为 switch 的输入值可以是 数组(类似列表的集合),在这种情况下,分支会对每个元素进行评估(然后 $_ 引用该元素)。

    • 对于作为输入的数组,请在动作脚本块中使用 continue 代替 break 以继续处理 下一个元素break 将阻止进一步处理其他元素)。

  • 请注意,即使 [Fruit] 枚举值与 字符串 进行比较,比较也能正常工作,因为 PowerShell 方便地自动将枚举值转换为其字符串表示形式,反之亦然。

英文:

The switch statement:

  • does not support multiple branch conditions, but ...

  • does support using a script block ({ ... }) as a branch condition, inside of which you're free to perform any test, including for multiple values, using the automatic $_ variable to refer to the input value at hand.

Therefore:

switch ($myFruit) {
  { $_ -in 'Apple', 'Orange' } {
    "This is an apple or an orange"
    break
  }
  Banana {
    "This is a banana"
    break
  }
  default {
    "Unknown fruit: $_"
  }
}

Note:

  • As you can see in the default branch's action script block above, $_ also refers to the input value there.

    • Using $_ is generally preferable, as the switch input value can be an array (list-like collection), in which case the branches are evaluated for each element (and $_ then refers to the element at hand.

    • With an array as input, use continue in lieu of break in an action script block in order to continue processing with the next element (break will prevent processing of further elements).

  • Note that even though the [Fruit] enumeration values are compared against strings, the comparison works, because PowerShell conveniently automatically converts enumeration values to and from their string representations.

huangapple
  • 本文由 发表于 2023年5月11日 03:10:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定