DOS批处理:如何在DOS批处理文件中解析命令行? [英] DOS Batch: how to parse command line in DOS batch file?

查看:136
本文介绍了DOS批处理:如何在DOS批处理文件中解析命令行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我的英语不好,

我的批处理文件是xmake.bat,它仅接受格式化的命令行:
xmake codename = g26 action = new lang = zh-CN:chs

我的问题是:如何解析批处理文件中的命令行?

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?

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



::我的xmake.bat文件内容在这里结束
::谢谢您的帮助.



:: my xmake.bat file content goes end here
:: Thanks for your help

推荐答案

我总是很想进行批量挑战,这是我的尝试.测试是最小的,因为我不想破坏它!

艾伦.

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 . . .


我认为您尝试使用批处理进行构建.最糟糕的主意!您必须使用MSBuild及其基于XML的声明性语言.您可以在MSDN上找到全面的信息.与批处理不同,您可以对自定义任务进行编程(尤其是嵌入任何第3方编译器或任何其他构建工具),还可以选择进行增量构建或从头开始构建,并且都可以使用功能强大的构建引擎进行构建.

—SA
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


取决于您用于解析的编程语言.
找到了此有用的链接 [
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.


这篇关于DOS批处理:如何在DOS批处理文件中解析命令行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆