T3RRYT3RR0R

Macro template with argument and switch handling

Apr 7th, 2021 (edited)
727
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ::: Author : T3RRY : Created 08/04/2021 : Version : 1.0.3
  2. ::: Version change: corrected placement of substring removal of doublequotes to after assignment of leading.args
  3. ::: Use search / replace to substitute $Macroname for your desired macro name.
  4. ::: All macro internal variables are prefixed with $Macroname to prevent conflict with other script variables
  5.  
  6. ::: PURPOSE: Template for handling multiple arguments and switches within batch macros.
  7. :::          Batch macros perform much more efficiently than functions ; and can accept parameters [args]
  8. :::          just as a function can using a for loop to catch the assignment of the parameter value.
  9. :::          Batch macros are a scripting technique, not an innate part of cmd's command syntax and therefor
  10. :::          lack metavriables such as "%*" or "%1" or commands such as "SHIFT" to assist in the processing of arguments
  11. :::          The macro template below provides a ready to use structure for splitting macro arguments into a usable form.
  12.  
  13. ::: For advanced usage example see: https://pastebin.com/GL87S8fF
  14.  
  15. ::: Defines an array for arguments recieved : 'arg[i]' where 'i' is a 1 indexed integer
  16. ::: Defines an array for switches  used     : 'switch[l]' where 'l' is a letter / string defined as a valid switch.
  17. ::: If switch is plain [ has no subvalue ], switch[l] returns true ; else switch[l] returns value provided.
  18.  
  19. ::: Notes on arg / switch structure. As with typical cmd commands SWITCHES MUST FOLLOW ARGS.
  20. ::: - Arg order can be rigid or flexible depending on the use case and how you script assessment of the arg array.
  21. ::: - Rigid arg order is recommended for values that cant easily be distinguished as belonging to a given arg type.
  22. ::: - Switch order is flexible
  23. :::
  24. ::: As with normal batch arguments, args containing poison characters or standard delimiters must be doublequoted.
  25. :::
  26. ::: Permits switch usage structures:
  27. :::  /Switch
  28. :::  /Switch value
  29. :::  /Switch "quoted values"
  30.  
  31. :::
  32. ::: Args or Switch values containing poison characters must be doublequoted
  33. ::: Args are delimited using whitespaces except when encased in doublequotes.
  34. :::
  35. ::: Escaping: arg or switch values containing '!' shoud escape '!' and enclose in doublequotes: "SomeString^!"
  36. :::  - IE: %$Macroname% arg1 "arg 2" /C "poison & | ^! < > %:~%" /D
  37.  
  38. @Echo off
  39.  
  40. (Set \n=^^^
  41.  
  42. %= \n macro newline variable. Do not modify =%)
  43.  
  44. :# define the valid switches for the macro
  45. :# by design switches must be seperate objects
  46. :# - IE: /A value /B value ; NOT: /AB value
  47.  
  48.  Set $MacroName_Switches="A" "B" "C" "D" "E"
  49.  
  50. :# usage: %$Macroname% args /switches
  51.  
  52.  Set $MacroName=For %%n in (1 2)Do if %%n==2 (%\n%
  53.   For %%G in ( %$MacroName_Switches% )Do Set "$MacroName_Switch[%%~G]="%\n%
  54.   Set "$MacroName_leading.args=!$MacroName_args:*/=!"%\n%
  55.   For /F "Delims=" %%G in ("!$MacroName_leading.args!")Do Set "$MacroName_leading.args=!$MacroName_args:/%%G=!"%\n%
  56.   Set ^"$MacroName_args=!$MacroName_args:"=!"%\n%
  57.   Set "$MacroName_i.arg=0"%\n%
  58.   For %%G in (!$MacroName_leading.args!)Do (%\n%
  59.    Set /A "$MacroName_i.arg+=1"%\n%
  60.    Set "$MacroName_arg[!$MacroName_i.arg!]=%%~G"%\n%
  61.   )%\n%
  62.   If "!$MacroName_Args:~-2,1!" == "/" (%\n%
  63.    Set "$MacroName_Switch[!$MacroName_Args:~-1!]=true"%\n%
  64.    Set "$MacroName_Args=!$MacroName_Args:~0,-2!"%\n%
  65.   )%\n%
  66.   For %%G in ( %$MacroName_Switches% )Do If not "!$MacroName_args:/%%~G =!" == "!$MacroName_args!" (%\n%
  67.    Set "$MacroName_Switch[%%~G]=!$MacroName_Args:*/%%~G =!"%\n%
  68.    If not "!$MacroName_Switch[%%~G]:*/=!" == "!$MacroName_Switch[%%~G]!" (%\n%
  69.     Set "$MacroName_Trail[%%~G]=!$MacroName_Switch[%%~G]:*/=!"%\n%
  70.     For %%v in ("!$MacroName_Trail[%%~G]!")Do (%\n%
  71.      Set "$MacroName_Switch[%%~G]=!$MacroName_Switch[%%~G]: /%%~v=!"%\n%
  72.      Set "$MacroName_Switch[%%~G]=!$MacroName_Switch[%%~G]:/%%~v=!"%\n%
  73.     )%\n%
  74.     Set "$MacroName_Trail[%%~G]="%\n%
  75.     If "!$MacroName_Switch[%%~G]:~-1!" == " " Set "$MacroName_Switch[%%~G]=!$MacroName_Switch[%%~G]:~0,-1!"%\n%
  76.     If "!$MacroName_Switch[%%~G]!" == "" Set "$MacroName_Switch[%%~G]=true"%\n%
  77.    )%\n%
  78.   )%\n: NOTE - APPEND COMMAND ACTIONS BEFORE assignment of $MacroName_args below \Do not Modify This line or Above=%
  79.   %= Example =% For %%G in ( %$MacroName_Switches% )Do if not "!$MacroName_switch[%%~G]!" == "" Set "$MacroName_switch[%%~G]"%\n%
  80.   %= Example =% For /L %%i in (1 1 !$MacroName_i.arg!)Do Set "$MacroName_arg[%%i]"%\n%
  81.  ) Else Set $MacroName_args=%= Only modify the above example lines in this macro =%
  82.  
  83. Setlocal EnableDelayedExpansion
  84.  
  85. :# facilitate testing of the macro using parameters from the command line
  86.  if not "%~1" == ""  (
  87.   %$MacroName% %*
  88.  )Else Goto :Eof
  89.  
  90.  pause
  91. Goto :eof
  92.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×