You can use a FOR /F to parse out the command from the arguments.
I would define a variable containing a delimited list of all valid commands. Then you can use simple search and replace to validate whether the user entered command is valid.
Addition of a new command is as easy as adding the command to the list, and creating a labeled subroutine for the new command.
I would use CALL instead of GOTO so that each routine can easily parse the arguments. The only disadvantage is CALL will double up quoted carets ("^"
becomes "^^"
)
Here is a basic framework that can easily be extended. Note that user entered !
will be corrupted (or expanded) because of delayed expansion. There are simple ways to get around this limitation with additional code.
@echo off
setlocal enableDelayedExpansion
echo Simple command-line interface
echo(
:: 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
::
toREM
.::
was used for ending labels a while ago, but are no longer used for that. UsingREM
is much more acceptible. Hope I helped. \$\endgroup\$