Reputation: 6830
I wish to write a Windows Batch script that first tests to see if any of the command line arguments are equal to /?
. If so, it displays the help message and terminates, otherwise it executes the rest of the script code. I have tried the following:
@echo off
FOR %%A IN (%*) DO (
IF "%%A" == "/?" (
ECHO This is the help message
GOTO:EOF
)
)
ECHO This is the rest of the script
This doesn't seem to work. If I change the script to:
@echo off
FOR %%A IN (%*) DO (
ECHO %%A
)
ECHO This is the rest of the script
and call it as testif.bat arg1 /? arg2
I get the following output:
arg1
arg2
This is the rest of the script
The FOR
loop appears be ignoring the /?
argument. Can anyone suggest an approach to this problem that works?
Upvotes: 4
Views: 7558
Reputation: 881
Something like this should do the trick:
@echo off
IF [%1]==[/?] GOTO :help
echo %* |find "/?" > nul
IF errorlevel 1 GOTO :main
:help
ECHO You need help my friend
GOTO :end
:main
ECHO Lets do some work
:end
Thanks to @jeb for pointing out the error if only /? arg provided
Upvotes: 12
Reputation: 3830
Do not use a FOR loop, but use the following instead:
@ECHO OFF
:Loop
IF "%1"=="" GOTO Continue
IF "%1" == "/?" (
ECHO This is the help message
GOTO:EOF
)
SHIFT
GOTO Loop
:Continue
ECHO This is the rest of the script
:EOF
Upvotes: 0