0

I have this for recognising a help parameter at the top of each of a set of batch files. Is there a briefer way, or is this as good as it gets?

@Echo Off
if "%1" == "" Goto Usage
if "%1" == "-?" Goto Usage
if "%1" == "/?" Goto Usage
if "%1" == "/help" Goto Usage
if "%1" == "-help" Goto Usage
if "%1" == "/Help" Goto Usage

2 Answers 2

2

You can't make it shorter:

if "%~1" == "" Goto Usage
if "%~1" == "-?" Goto Usage
if "%~1" == "/?" Goto Usage
if /i "%~1" == "/help" Goto Usage
if "%~1" == "-help" Goto Usage

if /i means ignore case. "%~1" to avoid double-double quotes.

You can ask for all useful answers and goto usage at the end:

if /i "%~1" == "a" Goto :doit_a
if /i "%~1" == "b" Goto :doit_b
if /i "%~1" == "c" Goto :doit_c
goto :Usage
Sign up to request clarification or add additional context in comments.

1 Comment

at least the /i is a step forward on what I had, thanks. And your ~%1 is more accurate
2
setlocal enableDelayedExpansion
set "help= -? /? -help /help "
if "%~1" equ "" goto Usage
if "!help: %~1 =!" neq "%help%" goto Usage

5 Comments

@JubjubBandersnatch I call this after its inventor "method Aacini" [please do not take too seriously:)] and you need delayed expansion.
@Endoro - I had seen this basic technique to check for a list of values posted on DosTips long before Aacini burst on the scene. I wouldn't be surprised if many people have come up with the idea independently. But it is an old concept.
Yes, of course. That's why I asked please to not take it seriously.
Please, explain the magic?! The colon on line 4 is new to me and I don't get the trailing = inside the !! quotes.
Ah, got it: I should read line 4 as : expand the help variable, substituting %~1 with blank, and compare the result to the original. If the substitution caused difference, then %~1 was a substring of %help%.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.