Check input arguments for a particular keyword

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Check input arguments for a particular keyword

#1 Post by thefeduke » 11 Nov 2016 12:40

In Topic, Re: Passing batch argument to Choice command, Sponge Belly wrote:Aacini wrote:

Code: Select all

set "vars=apl1=2,apl2=,apl3=kup,aplN=etc"
set "%vars:,=" & set "%"


Brilliant! 8)
I thought so, too. I recall the aggravation of interpreting arguments while using SHIFT, which intrudes on the order of things, by definition. Here is a non-intrusive quick check for a keyword in up to the first nine arguments:

Code: Select all

:: KeywordArg.bat version 0.0 posted by TheFeDuke 
::
:KeywordArg arguments[Opt] - only the first nine are checked
@Echo Off&SetLocal EnableDelayedExpansion

::  This technique is based on:   
::
::  Post subject: Re: Passing batch argument to Choice command
::  Posted: Sun Apr 24, 2016 11:39 am by Aacini
::  http://www.dostips.com/forum/viewtopic.php?p=46412#p46412
Set "vars=arg1=%~1,arg2=%~2,arg3=%~3,arg4=%~4,arg5=%~5,arg6=%~6,arg7=%~7,arg8=%~8,arg9=%~9"
Set "%vars:,=" & set "%"

For /L %%I In (1 1 9) do (
    set "arg=!arg%%I!"
Rem.In this particular example the rightmost argument takes effect.
    If /I "!arg!" EQU "/?"  (Set "HelpSw=1"&Echo()
    If /I "!arg!" EQU "/??" (Set "HelpSw=2"&Echo()
)

If /I "%HelpSw%" EQU "1" Echo(Arguments include '/?' to indicate a desire for syntax Help.
If /I "%HelpSw%" EQU "2" Echo(Arguments include '/??' to indicate a desire for detailed Help.

Exit /B
Or more briefly,

Code: Select all

@Echo Off&SetLocal EnableDelayedExpansion

Set "KeyWordSw=0"&Set "KeyWord=/?"
Set "vars=arg1=%~1,arg2=%~2,arg3=%~3,arg4=%~4,arg5=%~5,arg6=%~6,arg7=%~7,arg8=%~8,arg9=%~9"
Set "%vars:,=" & set "%"

For /L %%I In (1 1 9) do (Set "arg=!arg%%I!"
    If /I "!arg!" EQU "%KeyWord%" Set "KeyWordSw=1")
If /I "%KeyWordSw%" EQU "1" Call :aKeyWordRoutine
Where the coder supplies :aKeyWordRoutine

John A.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Check input arguments for a particular keyword

#2 Post by penpen » 11 Nov 2016 19:08

This might help you to initialize all arguments in a row; all is needed is just a special argument format:
http://www.dostips.com/forum/viewtopic.php?t=4692

penpen

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Check input arguments for a particular keyword

#3 Post by thefeduke » 12 Nov 2016 13:24

@penpen, thanks for the pointer. It aimed at the topic itself, so I saw Antonio's post first. I adapted that to suit me, but "IN (%*)" ignores strings with "?"s so it needed a little poison fiddling. The code follows later in this post and extends the limitation of nine arguments in my original.

I did see a post by penpen a little later and became fascinated with:

Code: Select all

%parms: /="&set "%
I was not interested in setting a switch or an option to a value but merely checking if a keyword was specified. I was focused on "/?" for a quick HELP and EXIT fork while generalizing for any keyword. The adaptation was a somewhat painful learning exercise and here is what I can up with:

Code: Select all

@Echo Off&SetLocal

Rem.Erase all existent variables to show the created ones at end with SET command
for /F "delims==" %%a in ('set') do set %%a=

::  Show all defined variables
set

set "parms=%*"
::  eliminate null arguments
set parms=%parms:"" =%"
::  use double spaces for substitution
set parms=%parms: =  %"
::  use single space to avoid substitution within the actual SET command
set parms=set "SubstitutionDone  %parms:"=%=1"
%parms:  ==1" /&set "%

Set "KeyWord=/?"

If Defined %KeyWord% Echo(Call :aKeyWordRoutine using parm

::  Show all defined variables
set

Exit /B
and here using Antonio's example:

Code: Select all

@Echo Off&SetLocal EnableDelayedExpansion

Rem.Erase all existent variables to show the created ones at end with SET command
for /F "delims==" %%a in ('set') do set %%a=

::  Show all defined variables
set

set "ArgStr=%*"
set "ArgStrq=%ArgStr:?=#@Question@#%"
For %%a in (%ArgStrq%) do (
    Set /A "rguCtr+=1"
    Set "rgu=%%a"
    set "rgu!rguCtr!=!rgu:#@Question@#=?!"
)

Set "KeyWord=/?"

Set "KeyWordSw=0"
For /L %%I In (1 1 %rguCtr%) do (Set "rgu=!rgu%%I!"
    If /I "!rgu!" EQU "%KeyWord%" Set "KeyWordSw=1")
If /I "%KeyWordSw%" EQU "1" Echo(Call :aKeyWordRoutine using rgu

::  Show all defined variables
set

Exit /B

Thanks, again for letting me hack away at your examples.

John A.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Check input arguments for a particular keyword

#4 Post by thefeduke » 12 Nov 2016 18:31

The meat is really in my previous post, but I noticed an interesting possibility in re-using the "parm" string above. A side effect of this "special argument format" approach is that arguments can clutter the environment with any kind of unpredicted variables. The clearing of all variables at the beginning in the examples would normally not be done, but is useful to see what is going on. If the subject keyword is not detected and the script did not transfer to an EXIT point, it makes sense to execute an ENDLOCAL to clean up and continue, but I could not resist trying the following to exploit that odd variable with the double-space:

Code: Select all

set DelParms=set "Parms  SetParms  SubstitutionDone  Keyword  DelParms  %parms:"=%="
%DelParms:  ==" /&set "%
What I found most interesting is that it successfully deletes the "DelParms" variable as it executes a modification of the same variable.

This recreational mind-twisting makes my hair hurt. :?

John A.

Post Reply