我需要将各种参数传递到具有 unix 格式的.cmd 文件中
file.cmd -Configuration=value -Source=value -flag
但是,我试试这个:
StartLocal
@ECHO off
CLS
SETLOCAL
SET CMDLINE=%*
SET Configuration=
SET Source=
SET BADARGS=
SET VALIDATION=
GOTO main
:SplitArgs
echo SplitArgs(%*)
if "%*" NEQ "" (
for /F "tokens=1,2,* delims== " %%i in ("%*") do call :AssignKeyValue %%i %%j & call :SplitArgs %%k
)
goto :eof
:AssignKeyValue
echo AssignKeyValue(%1, %2)
if /i %1==-Configuration (
SET Configuration=%2
) else if /i %1==-Source (
SET Source=%2
) else (
REM Append unrecognised [key,value] to BADARGS
echo Unknown KEY %1
SET BADARGS=%BADARGS%[%1, %2]
)
goto :eof
:Validate
echo Validating
SET VALIDATION=FAIL
if defined Configuration (
echo -Configuration ok
if defined Source (
echo -Source ok
if NOT defined BADARGS (
SET VALIDATION=SUCCESS
)
)
)
goto :eof
:main
cls
call :SplitArgs %CMDLINE%
call :Validate
if "%VALIDATION%" EQU "SUCCESS" (
ECHO -Configuration = %Configuration%
ECHO -Source = %Source%
)
但是我有一个问题,因为当我在这种模式下写一个参数时
file.cmd 源=值
总是取值,我需要验证这个,一些想法......谢谢