对任何集合 /p 值使用全局变量?



我目前正在创建一个完全用 Batch 编写的游戏。我很好奇是否有一种方法可以将任何set /p函数的选项全局设置为有效响应。

例如,假设这是我的脚本。

@echo off
title Test Script
:HELLO1
set /p hello1=ENTER "HELLO1"! 
if %hello1% == HELLO1 GOTO HELLO1_CONTINUE
goto HELLO1_ERROR
:HELLO2
set /p hello2=ENTER "HELLO2"! 
if %hello2% == HELLO2 goto HELLO2_CONTINUE
goto HELLO2_ERROR

我希望这两个选项都能够使用exit作为关闭整个脚本的选项,但是我不想重复if %hello[#] == EXIT EXIT两次、三次或脚本中的播放器选项一样多,特别是因为每个选项都必须用每个特定的 # 编写, 根据我正在使用的标签set /p

有什么办法可以解决这个问题吗?也不反对使用外部库。

例如,我在想设置一个等于 EXIT 的变量,但是我对如何实际将其实现到脚本中的想法为 0。

如果你使所有变量都有一个足够唯一的前缀,你也许可以像这样工作:

set hello1=test
set hello2=bob
set hello3=exit
set "hello4=exit=bot"
set "hello5=bob=exit"
for /f "delims== tokens=1*" %%a in ('set hello') do (
if /i "%%~b"=="exit" exit
)
echo If you get here, exit not found.

我不确定我是否理解了这个问题,所以...

  • 这基本上遵循您的代码布局
@echo off && setlocal EnableDelayedExpansion 
set hello=<nul && title nul & title Test Script&& set/p "hello=ENTER "HELLO1" or "HELLO2"!! "
for %%S in (!hello!)do for /f %%n in ('cmd.exe /u /c echo="%%~S"^|find /v " " ^|findstr [0-9]
')do if %%~n geq 1 echo/!hello!|find/i "hello"|findstr "1 2" >nul && goto :hello%%~n_CONTINUE
echo/The "!hello!" is not a valid input option, only Hello1 or Hello2^!! && goto :HELLO_ERROR
:HELLO1_CONTINUE
echo/I'm in HELLO1_CONTINUE & goto :END
:HELLO2_CONTINUE
echo/I'm in HELLO2_CONTINUE & goto :END
:HELLO_ERROR
echo/Well, something is really wrong!! & goto :END
:END
endlocal && exit /b

或者,在另一种布局中:

@echo off && @(setlocal EnableDelayedExpansion)
set _hello=<nul & title nul & title Test Script
set /p "_hello=Enter: "HELLO1" or "HELLO2"!!! "
for /f %%n in ('cmd/u/cecho="!_hello!"^|findstr [0-9]')do (
echo/"!_hello!" |find /i "hello" |findstr "1 2" >nul && (
goto:!_hello!_CONTINUE) || (set "_msg=Input Error^!!"&& (
cmd /v/c echo/!_msg!&& timeout -1 && goto :HELLO_ERROR)))
:HELLO1_CONTINUE
echo/I'm in HELLO1_CONTINUE & goto:END
:HELLO2_CONTINUE
echo/I'm in HELLO2_CONTINUE & goto:END
:HELLO_ERROR
echo/Well, something is really wrong!!
goto :END
:END
endlocal && exit /b

对于全局选项:


@echo off && @(setlocal EnableDelayedExpansion)
set _hello=<nul & title nul & title Test Script
set /p "_hello=Enter: "HELLO1" or "HELLO2"!!! "
for /f %%n in ('cmd/u/cecho="!_hello!"^|findstr [0-9]')do (
echo/ "!_hello!" | find /i "hello" |findstr "1 2" >nul && (
goto:%_hello:~,-1%_CONTINUE)||(set "_msg=Input Error^!!") )
cmd /v /c echo/!_msg! && timeout /t -1 && goto :HELLO_ERROR
:HELLO_CONTINUE
echo/I'm in !_hello!_CONTINUE & goto:END
:HELLO_ERROR
echo/Well, something is really wrong!!
goto :END
:END
endlocal && exit /b

我认为这种方法更接近您的要求,我之前的答案并没有真正涵盖您正在寻找的方法。

With the use of a function and Call arguments, you can define what you want to 
happen with input.
As an alternative to writing set /p Varname= If-Do/Goto IfNot Do/Goto whenever
you want input, Use:
CALL :Get_Input example hello1 Hello1_Continue Hello1_Wrong compare

这定义了要分配的变量:"示例" 变量的所需值:"hello1" 如果值正确,则转到的标签:"Hello1_Continue" 如果值错误,则转到的标签:"Hello1_Wrong" 指示函数执行什么操作:"比较">

第五个参数使您可以自由地合并要执行的其他操作,具体取决于您在输入后执行的操作。

@ECHO OFF
::: Required:
SETLOCAL EnableDelayedExpansion
::: Whenever you need input:
::: CALL :Get_Input VarName DefaultValue RightLabel WrongLabel InputAction
CALL :Get_Input example hello1 Hello1_Continue Hello1_Wrong compare

*输入评估功能 *

:Get_Input
Set VarName=%~1
Set %VarName%=
Set DefValue=%~2
Set Right_Label=%~3
Set Wrong_Label=%~4
Set InputAction=%~5
Set /p %VarName%=[option:]
IF NOT DEFINED !VarName! GOTO Get_Input
::: Other Validation Options Here
IF /I "%InputAction%" EQU "validate" Goto :EOF
IF /I "!%VarName%!" EQU "exit" Goto end
IF /I "!%VarName%!" EQU "!DefValue!" Goto %Right_Label%
::: Other comparison / Do Options Here
::: Options for other action types Here.
::: Other options need to occur before being forwarded to next label.
::: If Variable is something such as a name etc, use 'validate' as fifth 
::: parameter to validate it, and continue your program from there.
IF /I "%InputAction%" EQU "compare" Goto %Wrong_Label%
::: Other actions using the input.
GOTO :EOF

*结束功能 *

:Hello1_Continue
ECHO %example% is correct
pause >nul
GOTO docall
:Hello1_Wrong
ECHO %example% should be %DefValue%
pause >nul
GOTO docall
:next
ECHO Your Program Continues...
pause
GOTO docall
:end
exit

相关内容

最新更新