在先前接受的答复中,我找到了以下代码。 source:https://codereview.stackexchange.com/questions/41121/making-a-bat-batch-command-line-interface#new-answer?s=0857a55f8b4e43c1a1d4b13236169370
:: Define all valid commands: make sure there is a space between each command
:: and also one at beginning and end
set "commands= something echo exit "
:input.get
:: Clear the existing value in case user hits <Enter> without entering anything
set "input="
:: Get the next command
set /p "input=COMMAND\>"
:: Parse command into command and arguments.
for /f "tokens=1* delims= " %%A in ("!input!") do (
REM check if command is valid (not case sensitive) and act accordingly
if "!commands: %%A =!" equ "!commands!" (
echo Invalid command: %%A
) else if /i %%A equ exit (
exit /b
) else (
call :%%A %%B
)
)
echo(
goto input.get
:something
echo Doing something with Arg1=[%1] and Arg2=[%2]
exit /b
:echo
echo(%*
exit /b
如果我想再添加一个命令,该怎么做?例如,如果用户写“ pop”,那么他/她会像下面这样得到答复:
You wrote pop
现在如何编辑此内容。
您需要做两个简单的事情:
将pop
添加到有效命令列表中
set "commands= something echo exit pop "
添加代码以将命令作为新的可调用子例程处理
:pop
echo You wrote pop
exit /b