Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::: Author : T3RRY : Created 08/04/2021 : Version : 1.0.5
- ::: Version changes:
- ::: ADDED inbuilt substitution of {EQ} = ; {AS} * ; {FS} / and {DQ} "" for all args and switches
- ::: EXPANDED assessment of switch == true to support switches up to 3 characters long when switch is last switch used.
- ::: CORRECTED placement of substring removal of doublequotes to after assignment of leading.args
- :::
- ::: Use search / replace to substitute $Macroname for your desired macro name.
- ::: All macro internal variables are prefixed with $Macroname to prevent conflict with other script variables
- ::: PURPOSE: Template for handling multiple arguments and switches within batch macros.
- ::: Batch macros perform much more efficiently than functions ; and can accept parameters [args]
- ::: just as a function can using a for loop to catch the assignment of the parameter value.
- ::: Batch macros are a scripting technique, not an innate part of cmd's command syntax and therefor
- ::: lack metavriables such as "%*" or "%1" or commands such as "SHIFT" to assist in the processing of arguments
- ::: The macro template below provides a ready to use structure for splitting macro arguments into a usable form.
- ::: For advanced usage example see: https://pastebin.com/GL87S8fF
- ::: Defines an array for arguments recieved : 'arg[i]' where 'i' is a 1 indexed integer
- ::: Defines an array for switches used : 'switch[l]' where 'l' is a letter / string defined as a valid switch.
- ::: If switch is plain [ has no subvalue ], switch[l] returns true ; else switch[l] returns value provided.
- ::: Notes on arg / switch structure. As with typical cmd commands SWITCHES MUST FOLLOW ARGS.
- ::: - Arg order can be rigid or flexible depending on the use case and how you script assessment of the arg array.
- ::: - Rigid arg order is recommended for values that cant easily be distinguished as belonging to a given arg type.
- ::: - Switch order is flexible
- :::
- ::: As with normal batch arguments, args containing poison characters or standard delimiters must be doublequoted.
- :::
- ::: Permits switch usage structures:
- ::: /Switch
- ::: /Switch value
- ::: /Switch "quoted values"
- :::
- ::: Args or Switch values containing poison characters must be doublequoted
- ::: Args are delimited using whitespaces except when encased in doublequotes.
- :::
- ::: Escaping: arg or switch values containing '!' shoud escape '!' and enclose in doublequotes: "SomeString^!"
- ::: - IE: %$Macroname% arg1 "arg 2" /C "poison & | ^! < > %:~%" /D
- @Echo off
- (Set \n=^^^
- %= \n macro newline variable. Do not modify =%)
- (Set LF=^
- %= LF newline variable. Do not modify =%)
- :# Example Switches for demonstrating concept. List used twice within macro switch handling. Define outside macro to
- :# simplify maintainance.
- Set $MacroName_Switches="A" "B" "C" "D" "E" "Def"
- Set $MacroName=For %%n in (1 2)Do if %%n==2 (%\n%
- For /F "Tokens=1,2 Delims==" %%G in ('Set "$Macroname_Arg[" 2^^^> nul')Do Set "%%~G="%\n%
- For %%G in ( %$MacroName_Switches% )Do Set "$MacroName_Switch[%%~G]="%\n%
- If not "!$MacroName_args:* /=!" == "!$MacroName_args!" (%\n: build $MacroName.Args[!$MacroName_arg[i]!] array if args present =%
- Set "$MacroName_leading.args=!$MacroName_args:*/=!"%\n%
- For /F "Delims=" %%G in ("!$MacroName_leading.args!")Do Set "$MacroName_leading.args=!$MacroName_args:/%%G=!"%\n%
- Set ^"$MacroName_args=!$MacroName_args:"=!"%\n%
- Set "$MacroName_arg[i]=0"%\n%
- For %%G in (!$MacroName_leading.args!)Do (%\n%
- Set /A "$MacroName_arg[i]+=1"%\n%
- Set "$MacroName_arg[!$MacroName_arg[i]!]=%%~G"%\n%
- For %%i in ("!$MacroName_arg[i]!")Do (%\n%
- Set "$MacroName_arg[%%~i]=!$MacroName_arg[%%~i]:{FS}=/!"%\n%
- Set "$MacroName_arg[%%~i]=!$MacroName_arg[%%~i]:{AS}=*!"%\n%
- Set "$MacroName_arg[%%~i]=!$MacroName_arg[%%~i]:{EQ}==!"%\n%
- Set ^"$MacroName_arg[%%~i]=!$MacroName_arg[%%~i]:{DQ}="!"%\n%
- )%\n%
- )%\n%
- ) Else (%\n: remove doublequotes from args before switch processing switches =%
- Set ^"$MacroName_args=!$MacroName_args:"=!"%\n%
- )%\n%
- Set "$MacroName_LastSwitch="%\n%
- For /L %%L in (2 1 4)Do If "!$MacroName_LastSwitch!" == "" (%\n%
- If "!$MacroName_Args:~-%%L,1!" == " " Set "$MacroName_LastSwitch=_"%\n%
- If "!$MacroName_Args:~-%%L,1!" == "/" (%\n: Flag last switch true if no subargs; Works for character switches of up to 3 characters =%
- For /F "Delims=" %%v in ('Set /A "%%L-1"')Do Set "$MacroName_Switch[!$MacroName_Args:~-%%v!]=true"%\n%
- If not "!$MacroName_Args:/?=!." == "!$MacroName_Args!." Set "$MacroName_Switch[help]=true"%\n%
- Set "$MacroName_Args=!$MacroName_Args:~0,-%%L!"%\n%
- Set "$MacroName_LastSwitch=_"%\n%
- )%\n%
- )%\n%
- For %%G in ( %$MacroName_Switches% )Do If not "!$MacroName_args:/%%~G =!" == "!$MacroName_args!" (%\n%
- Set "$MacroName_Switch[%%~G]=!$MacroName_Args:*/%%~G =!"%\n%
- If not "!$MacroName_Switch[%%~G]:*/=!" == "!$MacroName_Switch[%%~G]!" (%\n%
- Set "$MacroName_Trail[%%~G]=!$MacroName_Switch[%%~G]:*/=!"%\n%
- For %%v in ("!$MacroName_Trail[%%~G]!")Do (%\n%
- Set "$MacroName_Switch[%%~G]=!$MacroName_Switch[%%~G]: /%%~v=!"%\n%
- Set "$MacroName_Switch[%%~G]=!$MacroName_Switch[%%~G]:/%%~v=!"%\n%
- )%\n%
- Set "$MacroName_Trail[%%~G]="%\n%
- If "!$MacroName_Switch[%%~G]:~-1!" == " " Set "$MacroName_Switch[%%~G]=!$MacroName_Switch[%%~G]:~0,-1!"%\n%
- If "!$MacroName_Switch[%%~G]!" == "" Set "$MacroName_Switch[%%~G]=true"%\n%
- If not "!$MacroName_Switch[%%~G]!" == "" If not "!$MacroName_Switch[%%~G]!" == "true" (%\n%
- Set "$MacroName_Switch[%%~G]=!$MacroName_Switch[%%~G]:{FS}=/!"%\n%
- Set "$MacroName_Switch[%%~G]=!$MacroName_Switch[%%~G]:{AS}=*!"%\n%
- Set "$MacroName_Switch[%%~G]=!$MacroName_Switch[%%~G]:{EQ}==!"%\n%
- Set ^"$MacroName_Switch[%%~G]=!$MacroName_Switch[%%~G]:{DQ}="!"%\n%
- )%\n%
- )%\n%
- )%\n: Insert Switch Assessment Below. Use conditional testing of valid switches to enact macro functions before $MacroName_args assignment =%
- REM INSERT COMMANDS BETWEEN THE ABOVE AND BELOW LINES TERMINATING EACH WITH \n VARIABLE %\n%
- %= EXAMPLE. Remove this line. =% Echo(!LF!Example: %%$Macroname%%!$Macroname_args!%\n%
- %= EXAMPLE. Remove this line. =% Set $Macroname_Arg[%\n%
- %= EXAMPLE. Remove this line. =% Set $Macroname_Switch[%\n%
- %= ESCAPE AMPERSANDS AND REDIRECTION CHARACTERS. =%) Else Set $MacroName_args=
- Setlocal enableDelayedExpansion
- :# For Testing from command line
- if not "%~1" == "" (
- %$MacroName% %*
- Goto :Eof
- )
- :# Proof of concept
- %$Macroname% "{AS} param 1 {EQ} {DQ}https:{FS}{FS}stackoverflow.com{FS}posts{FS}60793679{DQ}" "param 2" /C one /D 12 /Def
- %$Macroname% /B a b /E /Def "hello world & ^! > < | %%" /A 1 + 2 {EQ} 3 /Undefined switch
- pause
- Goto :eof
Add Comment
Please, Sign In to add comment