Sample code for parameter parsing
[Autolink] Menu
@ECHO OFF
@REM ***** CatchArg.cmd
@REM ***** This is excellent code to parse parameter in Batch files
@REM *****
@REM ***** Author: Ralf Ulrich (see EMail Addresses)
@REM *****
@setlocal
@echo off
REM ***** Change "echo my_Progi" to the name of the program to run
set PROGI=echo my_Progi&rem
REM ***** &rem prevents from space to be inserted at the end!
REM ***** This variable is used for the valid parameters checked so far
set PARAM=&rem
REM ***** This variable is used to save the order of the first 7 parameters
set ORDER=$&rem
REM ***** This variables used for --HELP message!
set H1=%0 execs "%PROGI%" only with parameters in special order!
set H2="/Arg1 /Arg3" or "/Arg2 /Arg3" or "/Arg3 /Arg1"
REM ***** This is the main loop for the parameter processing
:$Loop
REM ***** check if there is a parameter
IF '%1' == '' GOTO $End1
REM ***** Process the next parameter
SHIFT
REM ***** If Cmd.exe does not find the label, it shall not output
REM ***** the SysError but echo defined Help (H1 & H2).
goto X%0 2>NUL || (echo dismissed "%0" &echo %H1% &echo %h2%)
REM ***** Or insert a default execution: (%PROGI% --Help)
REM ***** Insert a label for every possible parameter
:X/Arg1
set ORDER=%ORDER%1&rem
set PARAM=%PARAM% %0&rem
echo accepted "%0"
goto $Loop
:X/Arg2
set ORDER=%ORDER%2&rem
set PARAM=%PARAM% %0&rem
echo accepted "%0"
goto $Loop
:X/Arg3
set ORDER=%ORDER%3&rem
set PARAM=%PARAM% %0&rem
echo accepted "%0"
goto $Loop
:$End1
goto %ORDER% 2>NUL || (echo Invalid parameter order, possible: &echo %H2%)
REM ***** Insert a label for every possible parameter order here
REM ***** For example you can call this sample with the parameter
REM ***** in the following order:
REM ***** CatchArg.cmd /arg1 /arg3
:$13
REM ***** CatchArg.cmd /arg2 /arg3
:$23
REM ***** CatchArg.cmd /arg3 /arg1
:$31
REM ***** all other possible parameter orders are invalid!
REM ***** if Cmd.exe does not find a fitting label it executes:
REM ***** "echo Invalid parameter order, possible: &echo ..."
REM ***** "PROGI" inserts the main code here:
%PROGI%%PARAM%
REM ***** Remember the first catch already inserted a space in front of PARAM
REM ***** This label marks the end of the program
:$End2
[Back: The same in REXX ...]
[Next: CID related information]