user3081278
user3081278

Reputation: 9

How to choose only the options available Batch file

I found this tutorial to make a game on Notepad++ and make a batch file to run on CMD. Well here's what I have so far:

:ChooseWeapon 

cls 

echo "I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest." 

echo. 

set /p weapon=What is your weapon? (Sword, Double-Bladed Axe, Dagger): 

The point to this is to choose a weapon you wish to use by typing what you want. Now, the thing is I'm able to type "ghregff" and it would say that is my weapon. How do I make it so you have to choose either: Sword, Double-Bladed Axe, or Dagger?

Upvotes: 0

Views: 104

Answers (4)

Wouter
Wouter

Reputation: 322

The easiest way seems to be doing it through the findstr command.

:Choice
set /p weapon=What is your weapon? (Sword, Double-Bladed Axe, Dagger):
echo %weapon%| findstr /r "^Sword$ ^Double-Bladed Axe$ ^Dagger$">nul
if errorlevel 1 (
    echo %weapon% was sadly not an option.
    goto :Choice
)
echo you chose %weapon%.

(you need ^ and $. ^ means start of line, $ means end of line)

OR

if you want to use that system several times you could use findstr with parameters and a return value

:MakeChoice
set /p answer= : 
echo %answer%| findstr /r "^%~2$ ^%~3$ ^%~4$ ^%~5$ ">nul
if errorlevel 1 (
echo "%answer%" is sadly not a valid choice, choose again.
goto :MakeChoice
)
set %~1=%answer%
goto :eof

You can than call the function this way:

echo choose a weapon(Sword, Axe, Dagger)
call :MakeChoice chosenOption "Sword" "Double-Bladed Axe" "Dagger"
echo you chose %chosenOption%

This works with 1-4 variables, but if you want more, you can always add ^%~6$ ^%~7$... after ^%~5$.

Upvotes: 0

Magoo
Magoo

Reputation: 80203

@ECHO OFF
SETLOCAL
SET setprompt=What is your weapon?
CALL :chooselist  Sword, "Double-Bladed Axe", Dagger
ECHO(Choice made was %response%
CALL :chooselist2  Sword, "Double-Bladed Axe", Dagger
ECHO(Choice made was %response%
CALL :choosemenu  Sword, "Double-Bladed Axe", Dagger
ECHO(Choice made was %response%
GOTO :EOF

:chooselist
SET valid=%*
SET "nchoices="
CALL :choose "%setprompt% (%valid:"=%):"
GOTO :eof

:chooselist2
SET valid=%*
SET "nchoices="
FOR %%Z IN (%*) DO ECHO %%~Z
CALL :choose "%setprompt%:"
GOTO :eof

:choosemenu
SET valid=%*
SET /a nchoices=0
FOR %%Z IN (%*) DO SET /a nchoices+=1&CALL ECHO %%nchoices%% : %%~Z
CALL :choose "%setprompt%:"
GOTO :eof

:choose
SET /p "response=%~1 "
IF NOT DEFINED response GOTO choose
IF DEFINED nchoices FOR /l %%Z IN (1,1,%nchoices%) DO IF "%%Z"=="%response%" CALL :setresp %valid%&GOTO :eof
SET /a cmatch=0
CALL :countresp %valid%
IF NOT %cmatch%==1 GOTO choose
GOTO :eof

:setresp
IF %response% neq 1 SET /a response-=1&shift&GOTO setresp
SET response=%~1
GOTO :eof

:countresp
SET $1=%~1
IF NOT DEFINED $1 (
 IF %cmatch%==1 (SET response=%$3%)
 GOTO :eof
)
CALL SET $2=%%$1:*%response%=%%
IF /i "%response%%$2%"=="%$1%" SET /a cmatch+=1&SET "$3=%~1"
shift&GOTO countresp

GOTO :eof

This is a flexible piece of code that allows you to make your choice in ne of three manners:

Using chooselist, the parameter-list is appended to setprompt and the user may choose to enter any response in full, or simply sufficient to unambiguously define the requirement. "s" would define Sword, for instance, but "do" would be required for "Double-Bladed Axe" and "da" for "Dagger" since "d" is ambiguous.

Using chooselist2 is similar, it simply lists the choices each on its separate line and asks for input.

Finally, choosemenu does the same, but numbers the line and the choice can be made using either the number or an unambiguous start-string.

The response will always be in response

Upvotes: 0

RGuggisberg
RGuggisberg

Reputation: 4750

@echo off
set "NumberWeapons=3"

:ChooseWeapon
color 07
cls 
echo."I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest." 
echo.
echo.1 Sword
echo.2 Double-Bladed Axe
echo.3 Dagger
set /p Weapon=What is your weapon? (1-%NumberWeapons%): 
echo.
if %Weapon% lss 1 goto :ChoiceError
IF %Weapon% gtr %NumberWeapons% goto :ChoiceError
GOTO :Start

:ChoiceError
COLOR CF
ECHO.Error! Choose a valid choice.
pause
goto :ChooseWeapon

:Start
echo.Replace this line with the rest of your stuff & pause

Upvotes: 0

malexander
malexander

Reputation: 4700

You could make it a choice menu like below, and if they choose a number not 1 2 or 3, it will kick them back to enter a selection again.

@echo off

:chooseweapon
cls
echo "I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest."
echo.
echo  What is your weapon? (Sword, Double-Bladed Axe, Dagger): 
echo   1 - Sword
echo   2 - Double-Bladed Axe
echo   3 - Dagger
echo.

set /P Weapon="Enter a choice: "
echo --------------------------------------------------------------------
for %%I in (1 2 3) do if #%Weapon%==#%%I goto wp%%I
goto chooseweapon

:wp1
Set weapon=Sword
goto end

:wp2
Set weapon=Double-Bladed Axe
goto end

:wp3
Set weapon=Dagger
goto end

:end
echo %weapon%
pause

Upvotes: 1

Related Questions