Function arg validation and return structure

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
T3RRY
Posts: 181
Joined: 06 May 2020 10:14

Function arg validation and return structure

#1 Post by T3RRY » 07 Sep 2021 15:12

Just a template approach I've formulated for validating mandatory function values where return variable names are fixed.

The validation approach makes use of powershell -match syntax to test values comply with required regex

The variable return approach uses findstr to read the label name %0 of the function from the file %~f0 and assigns values from
mandatory <ArgNames> or optional [Argnames] listed after the Arg ID phrase ":labelname Args:"

Code: Select all

@Echo off

 Call:Demo 123 45 "any string"
 Set $
 Call:Demo 12 ab
 Set $
goto :Eof

:Demo Args: <$Columns> <$Lines> [$Option.1] [$Option.2]
Set "Err.out= Invalid. Required: %%# matching regex"
 For /f "tokens=3-6 Delims=<>[] " %%1 in ('%__APPDIR__%findstr.exe /lic:"%0 Args:" "%~f0"')Do (
  Set "%%1=" & Set "%%2=" & Set "%%3=%~3" & Set "%%4=%~4"
  (Call:regex "%~1" "\d{1,3}$" && Set "%%1=%~1") || (Echo(Function%0 Arg 1:"%~1" %Err.Out:#=1% \d{1,3} &Pause &Exit /b 1)
  (Call:regex "%~2" "\d{2}$"   && Set "%%2=%~2") || (Echo(Function%0 Arg 2:"%~2" %Err.Out:#=2% \d{2}   &Pause &Exit /b 2)
 )
Exit /b 0
 
:Regex <"String"> <"Regex Pattern">
:# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions?view=powershell-7.1
 For /f "usebackq delims=" %%v in (`powershell.exe -nologo -noprofile -c "'%~1' -match '%~2'"`)Do If "%%v"=="True" Exit /b 0
Exit /b 1

Post Reply