Advertisement
BrainWaveCC

CheckParameters.BAT (w/Debug Info)

Jun 23rd, 2024 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.18 KB | Source Code | 0 0
  1. @REM - CheckParameters.BAT (23 Jun 2024 // 30 Jun 2024 / 23 Jun 2024): How to Check Parameters -- Native vs 3rd Party
  2. @ECHO OFF
  3.  
  4.  ::: If you want to check for a parameter of /X or -X, the use the following format:
  5. :::   CALL :GetParams "X EXTENDED" & IF DEFINED #OK (execute whatever commands you want here)
  6. :::
  7. ::: You would call this from the command-line as:
  8. :::   CheckParameters.BAT /X
  9. :::   CheckParameters.BAT -X
  10. ::: -----------------------------------------------
  11. :::
  12. ::: If you want to check for a parameter of /X or -X that contains a value, then use the following format:
  13. :::   CALL :GetParams "X EXTENDED" #X_VARIABLE & IF DEFINED #X_VARIABLE (execute whatever commands you want here)
  14. :::
  15. ::: You would call this from the command-line as:
  16. :::   CheckParameters.BAT /X:1
  17. :::   CheckParameters.BAT /X:NoSpaces
  18. :::   CheckParameters.BAT /X:"Value with Spaces"
  19. :::
  20. ::: You can do this natively or with a 3rd party utility that really streamlines the process.
  21. ::: I use this for pretty much all of my major scripts.
  22. :::
  23. ::: The :FoundParam routine is just here for testing.  Not needed for production at all.
  24. ::: All you need for this to work is the variable block, and EITHER :GetParams OR :GetParams2
  25. :::
  26. ::: Full script can be found here .....  https://pastebin.com/wjs4Hz5k
  27. ::: Script without debug info .........  https://pastebin.com/5zAGAyrL
  28. :::
  29. ::: I started my testing with: CheckParameters.BAT /H /S /SHOW /L:C:\TEMP\TEST.TXT
  30. :::
  31.  
  32.  
  33. :Variables
  34.  SETLOCAL ENABLEDELAYEDEXPANSION
  35.  SET #ALL_PARAMS=%*
  36.  SET #DIVIDER=--------------------------------------------------------------------
  37.  
  38.  
  39.  rem ----------------------------------------------------------------------------------------------
  40. rem -- Process Special Command-line Parameters Natively
  41. rem ----------------------------------------------------------------------------------------------
  42.  
  43.  ::: Loop through all provided command-line parameters, and run the native
  44. ::: subroutine to see if that parameter is a valid one
  45.  FOR %%P IN (%#ALL_PARAMS%) DO CALL :GetParams %%P
  46.  
  47.  ::: Check through the %#SYNTAX% variable when all parameter parsing is done,
  48. ::: and if there are any "F" entries set, then you received a bad parameter.
  49. ::: You can decide if you actually want to do anything about that.
  50.  FOR %%S IN (%#SYNTAX%) DO IF "%%~S"=="F" (ECHO *** ERROR: Invalid parameters provided ***)
  51.  
  52.  ::: If the %#LOGFILE% variable wasn't set by specific parameter, set it to
  53. ::: the first parameter.  You can test its validity later.
  54.  IF NOT DEFINED #LOGFILE (SET #LOGFILE="%~1")
  55.  ECHO %#DIVIDER%
  56.  
  57.  
  58.  rem ----------------------------------------------------------------------------------------------
  59. rem -- Process Special Command-line Parameters via CHECKPARAMS.EXE
  60. rem ----------------------------------------------------------------------------------------------
  61.  
  62.  ::: The paradigm for using CHECKPARAMS.EXE is different.  Here, you're going
  63. ::: to call the subroutine once for each valid option that you want to search
  64. ::: for, and if it is found in the list, then %#OK% will be set to some value,
  65. ::: and you can check for that immediately after you make that call.  It's a
  66. ::: little easier to control precedence of selected parameters this way.
  67.  CALL :GetParams2 "S SHOW"   & IF DEFINED #OK (SET #SHOW=TRUE)
  68.  CALL :GetParams2 "H HELP ?" & IF DEFINED #OK (SET #HELP=TRUE)
  69.  
  70.  ::: The following syntax allows you to not only check for specific parameters,
  71. ::: but it captures the provided value into a specific variable, which you can
  72. ::: then test for immediately.
  73.  CALL :GetParams2 "L LOGFILE"       #LOGFILE   & IF NOT DEFINED #LOGFILE (SET #LOGFILE="%~1")
  74.  CALL :GetParams2 "D DIR DIRECTORY" #DIRECTORY & IF NOT DEFINED #DIRECTORY (ECHO No Directory was selected)
  75.  ECHO %#DIVIDER%
  76.  
  77.  
  78.  rem -- Test Your Parameters
  79. :Testing
  80.  ECHO:
  81.  ECHO Setup your parameters above, and then test them by calling this script
  82.  ECHO with your desired parameters.  Remember that "-" and "/" are both valid
  83.  ECHO option delimiters.  For compound options, use ":" as the separator
  84.  ECHO between the option and the value.
  85.  ECHO:
  86.  ECHO Valid parameters for this test are as follows:
  87.  ECHO   %~n0 /S
  88.  ECHO   %~n0 /SHOW
  89.  ECHO   %~n0 /H
  90.  ECHO   %~n0 /?
  91.  ECHO   %~n0 /HELP
  92.  ECHO   %~n0 /L:C:\Temp\LogFile.TXT
  93.  ECHO   %~n0 /LOGFILE:"%TEMP%\LogFile.TXT"
  94.  ECHO   %~n0 "%TEMP%\LogFile.TXT"
  95.  ECHO        {you can make the logfile take on the 1st param if not /L is found}
  96.  ECHO   %~n0 /D:%WINDIR%
  97.  ECHO   %~n0 /DIR:"%WINDIR%"
  98.  ECHO   %~n0 /DIRECTORY:"%~dp0"
  99.  ECHO:
  100.  FOR %%P IN (LOGFILE DIRECTORY) DO IF NOT DEFINED #%%P ECHO *** ERROR: #%%P was not defined
  101.  
  102.  
  103. :ExitBatch
  104.  ENDLOCAL
  105.  GOTO :EOF
  106.  
  107.  
  108.  rem -- SUBROUTINE: Use Native Batch Commands to Capture Selected Parameters
  109. :GetParams
  110. rem %1 = Parameter to Evaluate
  111.  
  112.  ::: If you decide to use this routine, be sure to remove every instance of the
  113. ::: following:   '& CALL :FoundParam GetParams "%#PREFIX%" "%#SUFFIX%"'   from
  114. ::: the lines below (after testing), or just look at the production (non-debug)
  115. ::: version of the script found here: https://pastebin.com/5zAGAyrL
  116.  
  117.  SET #OK=
  118.  SET #PREFIX=%~1
  119.  SET #SUFFIX=
  120.  IF NOT DEFINED #PREFIX GOTO :EOF
  121.  
  122.  ::: With each parameter you parse, start by stripping all doublequotes from
  123. ::: the parameter.  Next, attempt to determine if it is a compound parameter
  124. ::: by searching for a ":" character, and making the part before the ":" as
  125. ::: the prefix, and the portion after the ":" as the suffix.
  126.  SET #PREFIX=!#PREFIX:"=!
  127.  FOR /F "TOKENS=1* DELIMS=:" %%a IN ('ECHO !#PREFIX!') DO (
  128.      SET #PREFIX=%%~a
  129.      SET #SUFFIX=%%~b
  130.  )
  131.  
  132.  ::: Now, check for both a leading "/" or "-", compare the parameter you are
  133. ::: currently evaluating, with one of the valid parameters (case-insensitive)
  134. ::: and decide what you will do if it is the one you have found.
  135.  FOR %%d IN (/ -) DO (
  136.      FOR %%O IN (S SHOW) DO (
  137.          IF /I "%#PREFIX%"=="%%d%%O" (SET #OK=T& SET #SUFFIX=.& SET #SHOW=TRUE& CALL :FoundParam GetParams "%#PREFIX%" "%#SUFFIX%")
  138.      )
  139.  
  140.      FOR %%O IN (H HELP) DO (
  141.          IF /I "%#PREFIX%"=="%%d%%O" (SET #OK=T& SET #SUFFIX=.& SET #HELP=TRUE& CALL :FoundParam GetParams "%#PREFIX%" "%#SUFFIX%")
  142.      )
  143.  
  144.      FOR %%O IN (L LOGFILE) DO (
  145.          IF /I "%#PREFIX%"=="%%d%%O" (SET #OK=T& SET #LOGFILE=%#SUFFIX%& CALL :FoundParam GetParams "%#PREFIX%" "%#SUFFIX%")
  146.      )
  147.  
  148.      FOR %%O IN (D DIR DIRECTORY) DO (
  149.          IF /I "%#PREFIX%"=="%%d%%O" (SET #OK=T& SET #DIRECTORY=%#SUFFIX%& CALL :FoundParam GetParams "%#PREFIX%" "%#SUFFIX%")
  150.      )
  151.  )
  152.  
  153.  ::: If you get to the end of the valid parameter list, and %#OK% has not yet
  154. ::: been set to "T", then this means that the parameter you have just parsed
  155. ::: is an invalid one. You'll get to deal with this when all parameter
  156. ::: processing has been completed.
  157.  IF NOT DEFINED #SUFFIX SET #OK=F
  158.  SET #SYNTAX=%#OK%;%#SYNTAX%
  159.  GOTO :EOF
  160.  
  161.  
  162.  rem -- SUBROUTINE: Use CheckParams.exe (3rd party util) to Capture Selected Parameters
  163. rem -- https://www.majorgeeks.com/files/details/checkparams.html
  164. :GetParams2
  165. rem %1 = Parameters to Search For
  166. rem %2 = Variable to Set
  167.  
  168.  SET #OK=& IF "%~1"=="" GOTO :EOF
  169.  
  170.  ::: As mentioned earlier, this subroutine is called with one valid entry that
  171. ::: needs to be searched for from the entire list of parameters provided by
  172. ::: the user at the command-line.  For more explanation of CHECKPARAMS, type
  173. ::: the following at a command prompt:  CHECKPARAMS /??
  174.  FOR /F "TOKENS=2-3*" %%v IN ('CHECKPARAMS -q -a -c "%~1" -s %#ALL_PARAMS% 2^>NUL') DO IF /I "%%~v"=="TRUE" (
  175.      SET #OK=T
  176.      IF NOT "%~2"=="" SET %~2=%%~x
  177.  
  178.      rem ::: This following call to :FoundParam is just here for testing, and
  179.      rem ::: should be removed for production purposes, or the production
  180.      rem ::: version of the script be used instead...
  181.      CALL :FoundParam GetParams2 "%%~w" "%%~x"
  182.  )
  183.  GOTO :EOF
  184.  
  185.  
  186.  rem -- SUBROUTINE: Display What Parameter You Found
  187. :FoundParam
  188. rem %1 = Subroutine called from
  189. rem %2 = Parameter Found
  190. rem %3 = Parameter Value Found
  191.  
  192.  SET #ZZ=%~2
  193.  SET #ZZ=!#ZZ:[=!
  194.  SET #ZZ=!#ZZ:]=!
  195.  
  196.  ECHO:
  197.  ECHO Subroutine being tested ....... %~1
  198.  ECHO Parameters you provided ....... %#ALL_PARAMS%
  199.  ECHO Valid parameter found ......... !#ZZ!
  200.  ECHO Valid parameter value found ... %3
  201.  GOTO :EOF
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement