Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show help message if any command line argument equals /?

Tags:

batch-file

cmd

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?

like image 478
Dan Stevens Avatar asked Dec 20 '22 12:12

Dan Stevens


1 Answers

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

like image 167
Simon Bull Avatar answered Feb 11 '23 23:02

Simon Bull