Show help message if any command line argument equals /?

batch-file, cmd

Solution

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

Problem

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?

Original source