无法在 Powershell 中递归调用函数
Not able to call a function recursively in Powershell
我正在创建一个菜单驱动的脚本,供用户根据他们选择的选项执行某些操作。以防万一他们 select 一个错误的选项,我试图在显示适当的消息后再次显示选项。但是,只要 selected 有任何错误的选择,并且函数是从其内部调用的,我就会得到一个 ParseException。你能看看这里有什么问题吗?
Class O_Manager {
[int]$val
ShowMenu() {
Write-Host " Please choose from below Options : "
Write-Host " "
Write-Host " 1. Option1 "
Write-Host " 2. Option2 "
Write-Host " 3. Option3 "
$this.val = Read-Host " Enter your Choice here "
switch($this.val)
{
1 {"ONE"}
2 {"TWO"}
3 {"THREE"}
default {"Incorrect choice selected"
ShowMenu()}
}
}
}
$obj = New-Object O_Manager
$obj.ShowMenu()
PowerShell 将裸词标记 ShowMenu
解释为 命令名称 (例如 cmdlet、函数、脚本或可执行文件的名称),但是 ShowMenu
不是命令 - 它是附加到 [O_Manager]
class.
实例的 方法
要递归调用 ShowMenu
,请通过 $this
变量引用当前实例:
default {
"Incorrect choice selected"
$this.ShowMenu()
}
与其从自身内部递归调用 ShowMenu
,我建议重构您的 class 以具有一个共同的入口点,该入口点连续而不是递归地重复调用 ShowMenu
- 在根据我的经验,这通常会使代码更容易推理和排除故障。
在下面的示例中,入口点称为 REPL
(因为它实现了 a Read-Eval-Print-loop),它只调用了 ShowMenu
、Eval
和 Print
函数直到菜单或 eval 函数告诉它停止:
enum Outcome
{
Continue
Quit
}
Class O_Manager {
[int]$val
[string]$result
REPL() {
while($this.ShowMenu() -eq 'Continue' -and $this.Eval() -eq 'Continue'){
$this.Print()
}
}
Print() {
Write-Host $this.result -ForegroundColor Cyan
}
[Outcome]
Eval() {
# do something with $this.result here
return [Outcome]::Continue
}
[Outcome]
ShowMenu() {
Write-Host " Please choose from below Options : "
Write-Host " "
Write-Host " 1. Option1 "
Write-Host " 2. Option2 "
Write-Host " 3. Option3 "
try {
$this.val = Read-Host " Enter your Choice here "
}
catch {
Write-Host "Error encountered: $_" -ForegroundColor Yellow
return [Outcome]::Quit
}
$this.result = switch ($this.val) {
1 { "ONE" }
2 { "TWO" }
3 { "THREE" }
default {
$this.result = "Incorrect choice selected"
return [Outcome]::Quit
}
}
return [Outcome]::Continue
}
}
我正在创建一个菜单驱动的脚本,供用户根据他们选择的选项执行某些操作。以防万一他们 select 一个错误的选项,我试图在显示适当的消息后再次显示选项。但是,只要 selected 有任何错误的选择,并且函数是从其内部调用的,我就会得到一个 ParseException。你能看看这里有什么问题吗?
Class O_Manager {
[int]$val
ShowMenu() {
Write-Host " Please choose from below Options : "
Write-Host " "
Write-Host " 1. Option1 "
Write-Host " 2. Option2 "
Write-Host " 3. Option3 "
$this.val = Read-Host " Enter your Choice here "
switch($this.val)
{
1 {"ONE"}
2 {"TWO"}
3 {"THREE"}
default {"Incorrect choice selected"
ShowMenu()}
}
}
}
$obj = New-Object O_Manager
$obj.ShowMenu()
PowerShell 将裸词标记 ShowMenu
解释为 命令名称 (例如 cmdlet、函数、脚本或可执行文件的名称),但是 ShowMenu
不是命令 - 它是附加到 [O_Manager]
class.
要递归调用 ShowMenu
,请通过 $this
变量引用当前实例:
default {
"Incorrect choice selected"
$this.ShowMenu()
}
与其从自身内部递归调用 ShowMenu
,我建议重构您的 class 以具有一个共同的入口点,该入口点连续而不是递归地重复调用 ShowMenu
- 在根据我的经验,这通常会使代码更容易推理和排除故障。
在下面的示例中,入口点称为 REPL
(因为它实现了 a Read-Eval-Print-loop),它只调用了 ShowMenu
、Eval
和 Print
函数直到菜单或 eval 函数告诉它停止:
enum Outcome
{
Continue
Quit
}
Class O_Manager {
[int]$val
[string]$result
REPL() {
while($this.ShowMenu() -eq 'Continue' -and $this.Eval() -eq 'Continue'){
$this.Print()
}
}
Print() {
Write-Host $this.result -ForegroundColor Cyan
}
[Outcome]
Eval() {
# do something with $this.result here
return [Outcome]::Continue
}
[Outcome]
ShowMenu() {
Write-Host " Please choose from below Options : "
Write-Host " "
Write-Host " 1. Option1 "
Write-Host " 2. Option2 "
Write-Host " 3. Option3 "
try {
$this.val = Read-Host " Enter your Choice here "
}
catch {
Write-Host "Error encountered: $_" -ForegroundColor Yellow
return [Outcome]::Quit
}
$this.result = switch ($this.val) {
1 { "ONE" }
2 { "TWO" }
3 { "THREE" }
default {
$this.result = "Incorrect choice selected"
return [Outcome]::Quit
}
}
return [Outcome]::Continue
}
}