I'm always up for a batch challenge and here is my attempt. Testing was minimal as I didn't want to break it!
Alan.
@echo off
REM parse arguments in the format key=value
REM Example codename=g26 action=new lang=en:chs
SETLOCAL
SET CMDLINE=%*
SET CODENAME=
SET ACTION=
SET LANG=
SET BADARGS=
SET VALIDATION=
GOTO main
:SplitArgs
REM recursive procedure to split off the first two tokens from the input
echo SplitArgs(%*)
if "%*" NEQ "" (
REM %%i = KEY, %%j = VALUE, %%k = remainder of input
REM delimiters are space character and equals character
for /F "tokens=1,2,* delims== " %%i in ("%*") do call :AssignKeyValue %%i %%j & call :SplitArgs %%k
)
goto :eof
:AssignKeyValue
REM KEY %1, VALUE %2
echo AssignKeyValue(%1, %2)
if /i %1 EQU codename (
SET CODENAME=%2
) else if /i %1 EQU action (
SET ACTION=%2
) else if /i %1 EQU lang (
SET LANG=%2
) else (
REM Append unrecognised [key,value] to BADARGS
echo Unknown KEY %1
SET BADARGS=%BADARGS%[%1, %2]
)
goto :eof
:Validate
REM VALIDATION == SUCCESS|FAIL
echo Validating
SET VALIDATION=FAIL
if defined CODENAME (
echo codename ok
if defined ACTION (
echo action ok
if defined LANG (
echo lang ok
if defined BADARGS (
echo badargs found
) ELSE (
SET VALIDATION=SUCCESS
)
)
)
)
goto :eof
:main
cls
echo command line is %CMDLINE%
call :SplitArgs %CMDLINE%
call :Validate
if "%VALIDATION%" EQU "SUCCESS" (
echo **************
echo Et voila
echo perl make.pl %CODENAME% %ACTION% %LANG%
echo **************
) else (
echo Missing or unrecognised tokens in the command line
echo %BADARGS%
)
pause
OUTPUT
command line is codename=g26 action=new lang=en:chs
SplitArgs(codename=g26 action=new lang=en:chs)
AssignKeyValue(codename, g26)
SplitArgs(action=new lang=en:chs)
AssignKeyValue(action, new)
SplitArgs(lang=en:chs)
AssignKeyValue(lang, en:chs)
SplitArgs()
Validating
codename ok
action ok
lang ok
**************
et voila
perl make.pl g26 new en:chs
**************
Press any key to continue . . .