Advertisement
T3RRYT3RR0R

Batch Arg and Switch handler

Aug 27th, 2021 (edited)
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.01 KB | None | 0 0
  1. ::: Author: T3RRY : Created 27/08/2021
  2. ::: Purpose: Example handling of switches and args in batch scripting
  3. ::: Args or Switch Subargs should always be doublequoted.
  4.  
  5. @Echo off
  6.  
  7. %= Example. define valid_Switches variable for your script as doublequoted list below =%
  8.  Set valid_Switches="/a" "/b" "/col" "/f" "-a" "-b" "-col" "-f"
  9.  
  10. Setlocal EnableExtensions
  11.  Call :GetArgs %*
  12.  If %Errorlevel% EQU 0 (
  13. %= To notify usage in event of no args or switches remove rem below =%
  14.   Rem For %%G in (%Valid_Switches:" "=|%)Do Echo(No paramaters. %~n0 [Args] [%%~G]
  15.  )
  16.  
  17.  Set Switch[ 2> nul
  18.  Set Arg[ 2> nul
  19.  Set Switch.1 2> nul
  20.  
  21. Pause
  22. Endlocal & Goto :eof
  23.  
  24. ======================================
  25. :GetArgs <%*>
  26. :: Return values:
  27. :: Errorlevel 0 No Args, No Switches
  28. :: ?Arg{I} Count of Arguments
  29. :: ?Switch{I} Count of Switches used
  30. :: Arg[i] Index Argument value at position i
  31. :: Switch.i Index Switch value at position i
  32. :: Switch[string] Index value of /String or -String defined in valid_Switches [true if used without subarg; else subargs value]
  33.  
  34.  If not defined Valid_Switches (
  35.   Echo(Define Valid_Switches Variable prior to calling %~n0
  36.   Pause
  37.   Exit
  38.  )
  39.  If "%~1"=="" Exit /B 0
  40.  For /f "tokens=1 Delims==" %%G in ('Set "Switch" 2^> nul')Do Set "%%~G="
  41.  For /f "tokens=1 Delims==" %%G in ('Set "Arg[" 2^> nul')Do Set "%%~G="
  42.  Set "?Switch{I}=0"
  43.  Set "?Arg{I}=0"
  44.  Set "?Switch_Encountered="
  45.  
  46. :processParams
  47.  If "%~1"=="" Exit /B 1
  48.  Set "?Switch_Name=%~1"
  49.  Set "?Switch_Valid="
  50.  Set "?Switch_NoSubArg="
  51.  For %%G in (%valid_Switches%)Do If "%%~G"=="%~1" (
  52.   Set "?Switch_Encountered=true"
  53.   Call :Verify "%~2"
  54.   If not errorlevel 1 (
  55.    Set "?Switch_Valid=t"
  56.    Set /A "?Switch{I}+=1"
  57.   )Else (
  58.    Set "?Switch_NoSubarg=t"
  59.    Set /A "?Switch{I}+=1"
  60.   )
  61.  )
  62.  
  63. %= Define Switch[letter] with subarg value =%
  64.  If Defined ?Switch_Valid (
  65.   Set "Switch[%?Switch_Name:~1%]=%~2"
  66.   For /f "Tokens=2 delims==" %%v in ('Set "?Switch{I}"')Do Set "Switch.%%v=%~2"
  67.  )
  68. %= Define Switch[letter] as true - used with no subarg =%
  69.  If Defined ?Switch_NoSubarg (
  70.   Set "Switch[%?Switch_Name:~1%]=true"
  71.   For /f "Tokens=2 delims==" %%v in ('Set "?Switch{I}"')Do Set "Switch.%%v=true"
  72.  )
  73.  
  74. %= Capture args preceeding switches as array =%
  75.  If not defined ?Switch_Encountered (
  76.   %= Exclude invalid switch pattern =%
  77.   Call :Verify "%~1"
  78.   If not Errorlevel 1 (
  79.    Set /A "?Arg{I}+=1"
  80.    For /f "Tokens=2 delims==" %%v in ('Set "?Arg{I}"')Do Set "Arg[%%v]=%~1"
  81.   )
  82.  )
  83.  
  84.  Shift
  85. Goto :processParams
  86.  
  87. :Verify
  88. %= Handle last switch with no Subarg =%
  89.  If "%~1"=="" Exit /b 1
  90. %= Handle Switch with no Subarg followed by another Switch =%
  91.  For %%V in (%valid_Switches%)Do If "%%~V"=="%~1" Exit /b 1
  92. %= Flag Subvalue as invalid Switch if SINGLE LETTER =%
  93.   <nul Set /P "=%~1"|%__APPDIR__%Findstr.exe /ri "^[/-][abcdefghijklmnopqrstuvwxyz]$" > nul 2> nul && (
  94.   %= Terminate Arg array definition on encountering invalid switch =%
  95.   Set "?Switch_Encountered=true"
  96.   Exit /b 1
  97.  )
  98. %= Flag Switch as trailed by Subarg =%
  99.  Exit /b 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement