Page 1 of 1
Finding /? in arguments
Posted: 10 Dec 2021 10:47
by Szecska
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
Re: Finding /? in arguments
Posted: 10 Dec 2021 13:30
by jeb
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 ...
Re: Finding /? in arguments
Posted: 11 Dec 2021 16:39
by Szecska
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
But really thanks, you saved me from a huge headache
Re: Finding /? in arguments
Posted: 11 Dec 2021 18:52
by AR Coding
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.
Re: Finding /? in arguments
Posted: 14 Dec 2021 14:52
by Szecska
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.
Re: Finding /? in arguments
Posted: 14 Dec 2021 15:13
by Squashman
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
Re: Finding /? in arguments
Posted: 15 Dec 2021 12:45
by CJM
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 "/?"
Re: Finding /? in arguments
Posted: 15 Dec 2021 17:22
by Szecska
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
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