在尝试执行该函数之前,只是尝试检查函数是否存在。 我担心我可能会尝试简单地使用它,我应该在执行Process循环之前检查字符串。
SET /P Selection=Please Select Options?
echo You chose: %Selection%
Call :Process
pause
goto :EOF
:Process
for %%A in (%Selection%) do (
if not exist :Opt%%A (Call :Redo) ELSE (Call :Opt%%A)
)
GOTO :EOF
:Redo
Echo %Selection%
SET /P Selection=Selection was Invalid, Please choose a Valid Option:
Call :Process
GOTO :EOF
:Opt1
ECHO Option 1's code
GOTO :EOF
所以我遇到的问题是,如果不存在语句,我会陷入困境,并且不允许函数正确运行。
提前致谢
答案 0 :(得分:2)
当使用不存在的标签执行CALL
命令时,会显示错误消息,但过程继续,并且在调用后ERRORLEVEL = 1。您可以充分利用这一点,以避免检查标签是否存在:
编辑:代码已修改为对评论的回复
SET /P Selection=Please Select Options?
echo You chose: %Selection%
:Process
for %%A in (%Selection%) do (
Call :Opt%%A 2>NUL
if errorlevel 1 (
SET /P Selection=Selection was Invalid, Please choose a Valid Option:
goto :Process
)
)
pause
goto :EOF
:Opt1
ECHO Option 1's code
exit /B 0