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

问题描述 投票:1回答:1

在先前接受的答复中,我找到了以下代码。 sourcehttps://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”,那么他/她会像下面这样得到答复:

You wrote pop

现在如何编辑此内容。

batch-file cmd command command-line-interface
1个回答
0
投票

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

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

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

    :pop
    echo You wrote pop
    exit /b
    
© www.soinside.com 2019 - 2024. All rights reserved.