Search code examples
batch-filecmddos

How to check if a CMD/BAT command line parameter matchs a specific word?


H Hello, everyone. I intent to write a batch(umake.bat) file that does the following:

Check whether -f appears in one of bat parameters, so that I can take different actions in the .bat.

CASE 1:

User call

umake 

Inside umake.bat I will execute

make -f Makefile.umk

CASE 2:

User call

umake debug=1 var="big cat"

Inside umake.bat I will execute

make debug=1 var="big cat" -f Makefile.umk

CASE 3:

User call

umake -f special.mk debug=1

Inside umake.bat I will execute

make -f special.mk debug=1

CASE 4:

User call

umake debug=1 -f

Inside umake.bat I will execute

make debug=1 -f

In case 4, make(GNU make) will not succeed because missing file name after -f . For simplicity, I don't have to care for this since it is user's fault and the problem will be reported by GNU make .

Summary:

  • If user provide -f xxx in command parameter, I'll pass those parameter to make .
  • If user does not provide -f xxx, I'll call make with all user's parameter as well as appending -f Makefile.umk as make's extra parameters.

Thank you.


Solution

  • I seems to have found the solution, although the code is something verbose.

    @echo off
    
    set _F_MAKEFILE=-f Makefile.umk
    
    :CHECK_PARAM_AGAIN
    if "%~1" == "" (
      goto CHECK_PARAM_DONE
    ) else (
      if "%~1" == "-f" (
        set _F_MAKEFILE=
        goto CHECK_PARAM_DONE
      )
      rem echo @@@%1
      shift
    )
    goto CHECK_PARAM_AGAIN
    
    :CHECK_PARAM_DONE
    
    @echo on
    make %* %_F_MAKEFILE%
    

    NOTES:

    • Using %~1 is a must. Because: If %1 has value "big cat", %~1 makes it big cat , so that we can surrond %~1 with quotes.