Finding /? in arguments

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Szecska
Posts: 17
Joined: 15 Aug 2019 15:29
Location: Hungary

Finding /? in arguments

#1 Post by Szecska » 10 Dec 2021 10:47

I'm 100% certain this question has been asked and answered already, but its literally impossibble to search for.

I want to tell if the batch file's arguments contains `/?`.
I need to check every possible postion, then if it's present show a help message just like a com application do.

I tried

Code: Select all

(echo %*|findstr /r "\/?")&&goto help
but echo interprets /? and shows its own help message, thus rendering the trial useless.
This happens when I set %* as a variable, and expand it as %var% and !var! as well.

Can you redirect me to the answered post or copy the answer here?
Thanks in advance: Szecska

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Finding /? in arguments

#2 Post by jeb » 10 Dec 2021 13:30

Hi Szecska,

you can use echo(

Code: Select all

(echo(%*|findstr /r "\/?")&&goto help
Looks odd and you could think, there is a missing closing parenthesis, but it's not.
echo( is the only construct, that seems to work in any situation and can output any string without interpreting it.

Take a look at ECHO. FAILS to give text or blank line - Instead use ...

Szecska
Posts: 17
Joined: 15 Aug 2019 15:29
Location: Hungary

Re: Finding /? in arguments

#3 Post by Szecska » 11 Dec 2021 16:39

Thanks Jeb, i have absolutely no idea why I didn't think about that, but it even works with echo: which I actually use pretty often :P
But really thanks, you saved me from a huge headache :)

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Finding /? in arguments

#4 Post by AR Coding » 11 Dec 2021 18:52

Szecska wrote:
10 Dec 2021 10:47
I'm 100% certain this question has been asked and answered already, but its literally impossibble to search for.
You can use also Google as a search engine. just add site:dostips.com in the search bar after whatever you want to search for.

Szecska
Posts: 17
Joined: 15 Aug 2019 15:29
Location: Hungary

Re: Finding /? in arguments

#5 Post by Szecska » 14 Dec 2021 14:52

AR Coding wrote:
11 Dec 2021 18:52
You can use also Google as a search engine. just add site:dostips.com in the search bar after whatever you want to search for.
Yeah, it does not work like this. Google cannot search punctuation marks and non alphanumeric symbols except for . and , ... And this is true to all major engines iikr.
That's why I wrote literally impossible.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Finding /? in arguments

#6 Post by Squashman » 14 Dec 2021 15:13

Szecska wrote:
14 Dec 2021 14:52
AR Coding wrote:
11 Dec 2021 18:52
You can use also Google as a search engine. just add site:dostips.com in the search bar after whatever you want to search for.
Yeah, it does not work like this. Google cannot search punctuation marks and non alphanumeric symbols except for . and , ... And this is true to all major engines iikr.
That's why I wrote literally impossible.
"/?" site:dostips.com

CJM
Posts: 24
Joined: 25 Oct 2019 20:34
Location: Baltimore, MD USA

Re: Finding /? in arguments

#7 Post by CJM » 15 Dec 2021 12:45

I wrote this one-liner to handle (only in the first argument) the help command option in batch files:

Code: Select all

@For %%? in ("" : - /)do @IF %%~??==%1 GOTO:Help
...
:Help
...
It supports whatever leading switch characters you want to allow (in the FOR command set, except "*" which triggers a wildcard replacement for files in the current directory), but as shown will, as many .EXE programs do, accept these formats:
  • ?
  • :?
  • -?
  • /?
Sadly, this falls short of your requirement that it check all parameters (it does not, only the first argument), but perhaps you may find a use since it's more forgiving in it's syntax handling your choice of leading characters.

Also, as coded, when added at the beginning of a batch file, this one-liner permits the caller to bypass it's checking by quoting the argument, enabling a method to use any existing functionality that may also check for "/?" later in the batch file at run time, e.g.: cmdhelp.bat "/?"

Szecska
Posts: 17
Joined: 15 Aug 2019 15:29
Location: Hungary

Re: Finding /? in arguments

#8 Post by Szecska » 15 Dec 2021 17:22

Squashman wrote:
14 Dec 2021 15:13
Szecska wrote:
14 Dec 2021 14:52
AR Coding wrote:
11 Dec 2021 18:52
You can use also Google as a search engine. just add site:dostips.com in the search bar after whatever you want to search for.
Yeah, it does not work like this. Google cannot search punctuation marks and non alphanumeric symbols except for . and , ... And this is true to all major engines iikr.
That's why I wrote literally impossible.
"/?" site:dostips.com
Oh well, i must've been doing it wrong then :P
CJM wrote:
15 Dec 2021 12:45
I wrote this one-liner to handle (only in the first argument) the help command option in batch files:

Code: Select all

@For %%? in ("" : - /)do @IF %%~??==%1 GOTO:Help
...
:Help
...
It supports whatever leading switch characters you want to allow (in the FOR command set, except "*" which triggers a wildcard replacement for files in the current directory), but as shown will, as many .EXE programs do, accept these formats:
  • ?
  • :?
  • -?
  • /?
Sadly, this falls short of your requirement that it check all parameters (it does not, only the first argument), but perhaps you may find a use since it's more forgiving in it's syntax handling your choice of leading characters.

Also, as coded, when added at the beginning of a batch file, this one-liner permits the caller to bypass it's checking by quoting the argument, enabling a method to use any existing functionality that may also check for "/?" later in the batch file at run time, e.g.: cmdhelp.bat "/?"
Thank you CJM, but I've already found the solution with Jeb's help.

It's

Code: Select all

(echo(%*|findstr /r "\/?")&& goto help
or

Code: Select all

(echo:%*|findstr /r "\/?" >nul) && goto help
These can find the switch anywhere in the arguments, but if I want to include all the switch variants (except for the single ? beacuse that could be in a string and findstr cannot process regex that complicated The regex is (?=(?:[^"]*"[^"]*")*[^"]*$) ) it would become

Code: Select all

(echo:%*|findstr /r "[\/:-]?" >nul) && goto help

Post Reply