Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
sorry for my poor English,

My batch file is xmake.bat, which only accepts the formatted command line:
xmake codename=g26 action=new lang=en:chs

My question is: how to parse command line in batch file?

BAT
// xmake.bat content
:: %* stands for 'codename=g26 action=new lang=en:chs'
::
:: keywords = value
::
:: (keywords,value) pair
::
:: for each 'KEY'(codename, action, lang, the word before equal sign(=)) and it's 'VALUE', set 'VARIABLE' to 'VALUE'
:: eg. SET CODENAME = G26 & SET ACTION = NEW & SET LANG = EN:CHS
::
::
FOR %%A IN (%*) DO (
     IF %%A:~0,?(here use substring) == codename (or CODENAME) SET CODENAME = g26
     IF %%A:~0,?(here use substring) == action (or ACTION) set ACTION = new
     IF %%A:~0,?(here use substring) == lang (or LANG) set LANG = en:chs
)

echo %CODENAME%
echo %ACTION%
echo %LANG%

:: should output g26 new en:chs

:: some code pieces
perl make.pl %CODENAME% %ACTION% %LANG%


:: my xmake.bat file content goes end here
:: Thanks for your help
Posted
Updated 4-Apr-11 7:28am
v7
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 1:54am    
This is not *DOS* batch file. The modern version of the language is very different. How is it related to DOS?
--SA
Symfund 5-Apr-11 1:58am    
I mean, the DOS here is the command prompt on XP or above Windows 7, cmd.exe

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 . . .
 
Share this answer
 
Comments
Dalek Dave 4-Apr-11 18:46pm    
Good Answer.
Michel [mjbohn] 5-Apr-11 2:24am    
Once upon a time, in a world with out Java, where real man did some real batch jobs on their i386 steam engines.... ;-)
my 5
I think you trying to do build using batch. Worst idea! You have to use MSBuild and its XML-based declarative language. You can find comprehensive information on MSDN. Unlike batch, you can program custom tasks (and, in particular, embed any 3rd-party compiler or any other build tools), you can optionally do incremental build or build from scratch and all using the powerful build engine.

—SA
 
Share this answer
 
Depends on which programming language you are using for parsing.
Found this useful link[^] for C#.

Seach on the internet for similar solutions in other languages.
 
Share this answer
 
Comments
Symfund 4-Apr-11 2:47am    
Thanks, but how in a DOS batch file?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900