使用 CMD 制作我自己的 CLI(命令行界面)

Making My Own CLI(Command Line Interface) Using CMD

在之前接受的回复中我找到了下面的代码。 source: https://codereview.stackexchange.com/questions/41121/making-a-bat-batch-command-line-interface#new-answer?s=0857a55f8b4e43c1a1d4b13236169370

题目:制作.bat批处理命令行界面

:: 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' 然后 he/she 会得到如下回复:

You wrote pop

现在我如何通过编辑这个来做到这一点。

您需要做两件简单的事情:

  1. pop 添加到您的有效命令列表中

    set "commands= something echo exit pop "
    
  2. 添加代码以将命令作为新的可调用子例程处理

    :pop
    echo You wrote pop
    exit /b