0

I need pass various parameters into a .cmd file with unix format

file.cmd -Configuration=value -Source=value -flag

But, i try this:

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%
  ) 

But i have a problems, because when i write a parameter in this mode

file.cmd Source=value

always take the value,i need validate this, some idea... thank you

davdomin
  • 1,219
  • 6
  • 18
  • 38
  • 1
    It is unclear what you are asking, but your algorithm will not work very well if you have a mixture of flags and name/value pairs. You really should try the solution at http://stackoverflow.com/a/8162578/1012053. I updated the code in that answer to show how to use the variable once it has been assigned. The CMD parser will treat `=` as a ``, so `-name=value` and `-name value` will give the same result. – dbenham Aug 23 '13 at 16:22
  • Thank you man... it's very good – davdomin Aug 24 '13 at 01:55

1 Answers1

3

try this:

file.cmd "Source=value" 
Endoro
  • 37,015
  • 8
  • 50
  • 63