Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::: Author: T3RRY : Created 27/08/2021
- ::: Purpose: Example handling of switches and args in batch scripting
- ::: Args or Switch Subargs should always be doublequoted.
- @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
- Set Switch.1 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement