Recently I am working on batch to make a very simple command line interface.
The code looks like this:
@echo off
echo Simple command-line interface
echo.
:input.get
set /p input="COMMAND\>"
set input5=%input:~0,5%
if "%input%"=="something" goto something
if "%input5%"=="echo " goto echo
:echo
set content=%input:~5%
echo.%content%
goto input.get
:something
::do something
goto input.get
Is this really a good way to do it? I have posted a question about "How to check whether labels exist" in stack overflow, but if i input "echo hi" then it will be nearly impossible to check the tag.
Is it good to use both ways? (check whether label exist -> if not exist -> do "if" check)
1 Answer 1
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
-
\$\begingroup\$ I think it is good, however, i don't know NEARLY EVERYTHING about
for /f
. I knowfor /l
,for
. \$\endgroup\$Jamie– Jamie2014年02月13日 11:10:29 +00:00Commented Feb 13, 2014 at 11:10
::
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\$