atfon wrote: ↑27 Aug 2021 07:07
I'm sure the following is self explanatory with regards to how to maintain or modify what switches you wish to accept
I apologize. I misspoke. I meant I may limit how many switches I wish to accept, not
which switches I wish to accept. I'm reviewing your script and some other script I have found on this forum which utilize argument switches.
If you want to Exclude subsequent switches after one is defined, I'm sure you'll see where you need to add an "exit /b 1"
Alternately, you could modify the definition of the Switch[index] array to use the ?Switch{I} count instead of the Switch letter and just use the value of Switch[1]
I can't guess the order of priority you wish to place on switch assessment [ If you have multiple permitted switches but only want to accept one, how do you decide which to keep? ]
The list form of valid_Switches makes it easy for you to implement priority by defining the Valid_Switches variable in the order of importance, regardless of which of the two exclusion approaches described above you choose.
Theres also the choice of modifying the switch array definition to support both count and letter indexing:
Code: Select all
@Echo off
%= Example. define valid_Switches variable for your script as doublequoted list below =%
Set valid_Switches="/a" "/b" "/col" "/f" "-a" "-b" "-col" "-f"
Setlocal EnableExtensions
Call :GetArgs %*
If %Errorlevel% EQU 0 (
%= To notify usage in event of no args or switches remove rem below =%
Rem For %%G in (%Valid_Switches:" "=|%)Do Echo(No paramaters. %~n0 [Args] [%%~G]
)
Set Switch[ 2> nul
Set Arg[ 2> nul
Pause
Endlocal & Goto :eof
======================================
:GetArgs <%*>
:: Return values:
:: Errorlevel 0 No Args, No Switches
:: ?Arg{I} Count of Arguments
:: ?Switch{I} Count of Switches used
:: Arg[i] Index Argument value at position i
:: Switch.i Index Switch value at position i
:: Switch[string] Index value of /String or -String defined in valid_Switches [true if used without subarg; else subargs value]
If not defined Valid_Switches (
Echo(Define Valid_Switches Variable prior to calling %~n0
Pause
Exit
)
If "%~1"=="" Exit /B 0
For /f "tokens=1 Delims==" %%G in ('Set "Switch" 2^> nul')Do Set "%%~G="
For /f "tokens=1 Delims==" %%G in ('Set "Arg[" 2^> nul')Do Set "%%~G="
Set "?Switch{I}=0"
Set "?Arg{I}=0"
Set "?Switch_Encountered="
:processParams
If "%~1"=="" Exit /B 1
Set "?Switch_Name=%~1"
Set "?Switch_Valid="
Set "?Switch_NoSubArg="
For %%G in (%valid_Switches%)Do If "%%~G"=="%~1" (
Set "?Switch_Encountered=true"
Call :Verify "%~2"
If not errorlevel 1 (
Set "?Switch_Valid=t"
Set /A "?Switch{I}+=1"
)Else (
Set "?Switch_NoSubarg=t"
Set /A "?Switch{I}+=1"
)
)
%= Define Switch[letter] with subarg value =%
If Defined ?Switch_Valid (
Set "Switch[%?Switch_Name:~1%]=%~2"
For /f "Tokens=2 delims==" %%v in ('Set "?Switch{I}"')Do Set "Switch.%%v=%~2"
)
%= Define Switch[letter] as true - used with no subarg =%
If Defined ?Switch_NoSubarg (
Set "Switch[%?Switch_Name:~1%]=true"
For /f "Tokens=2 delims==" %%v in ('Set "?Switch{I}"')Do Set "Switch.%%v=true"
)
%= Capture args preceeding switches as array =%
If not defined ?Switch_Encountered (
%= Exclude invalid switch pattern =%
Call :Verify "%~1"
If not Errorlevel 1 (
Set /A "?Arg{I}+=1"
For /f "Tokens=2 delims==" %%v in ('Set "?Arg{I}"')Do Set "Arg[%%v]=%~1"
)
)
Shift
Goto :processParams
:Verify
%= Handle last switch with no Subarg =%
If "%~1"=="" Exit /b 1
%= Handle Switch with no Subarg followed by another Switch =%
For %%V in (%valid_Switches%)Do If "%%~V"=="%~1" Exit /b 1
%= Flag Subvalue as invalid Switch if SINGLE LETTER =%
<nul Set /P "=%~1"|%__APPDIR__%Findstr.exe /ri "^[/-][abcdefghijklmnopqrstuvwxyz]$" > nul 2> nul && (
%= Terminate Arg array definition on encountering invalid switch =%
Set "?Switch_Encountered=true"
Exit /b 1
)
%= Flag Switch as trailed by Subarg =%
Exit /b 0