Preface: job / function is used interchangeably below.
It is possible to call functions in this way, however you need to script it into your library.bat program. That means assessing provided arguments, determining if they correspond to a valid job, and determining the action to take such as calling a job with %*
arguments or providing help output.
Note: using Call %*
to execute a label supplied as %1
when calling library.bat will:
- desirable behaviour:
- Allow arguments provided to a called job to reside in their intended positions as
:jobname
becomes %0
when called.
- Potentially undesirable behaviours:
- Result in the doubling of any carets present in the arguments.
If relevent to any jobs, you'll need to account for that in the relevant job.
- Expand any
%variables%
present in the argument string supplied to library.bat (defined or undefined)
An example of executing the desired functionality:
@Echo off
Echo(
Set "ExitCode=0"
2> nul (
Rem // search this file for labels matching Argument 1 at line beginning.
%SystemRoot%\System32\Findstr.exe /BLI "%~1" "%~f0" > nul && (
Call %* || (
Call Set "ExitCode=%%Errorlevel%%"
(Call )
)
) || (
Echo("%~1"|%SystemRoot%\System32\Findstr.exe /R "[:][.]*" && (
Rem // No labels matching Argument 1 at line beginning. Argument 1 begins with a ":"
Echo(Invalid job. Valid jobs:
For /f "tokens=1" %%J in ('%SystemRoot%\System32\Findstr.exe /R "^[:][.]*" "%~f0"')Do Echo(%%J
Exit /b 1
) || If not "%~1" == "" (
Rem // Argument 1 does not begin with a ":"
Echo(No job call attempt. Args: %*
Echo(
Echo(To call a job:
Echo(Call "%~f0" :jobname %%*
)
)
)
Exit /b %ExitCode%
:demo
Echo(Jobname %0 JobArgs: [%*]
Exit /b 0
:job
Echo(Jobname %0 JobArgs: [%*]
REM // demostrate return of errorlevel in ExitCode variable facilitate && || logic
set /A ExitCode=%random% %% 2
Exit /b %ExitCode%