如何检查CMD / BAT命令行参数是否与特定字匹配?

时间:2023-01-30 00:38:31

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

你好,大家好。我打算编写一个批处理(umake.bat)文件来执行以下操作:

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

检查-f是否出现在其中一个bat参数中,以便我可以在.bat中执行不同的操作。

CASE 1:

User call

umake 

Inside umake.bat I will execute

在umake.bat里面我会执行

make -f Makefile.umk

CASE 2:

User call

umake debug=1 var="big cat"

Inside umake.bat I will execute

在umake.bat里面我会执行

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

在umake.bat里面我会执行

make -f special.mk debug=1

CASE 4:

User call

umake debug=1 -f

Inside umake.bat I will execute

在umake.bat里面我会执行

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 .

在第4种情况下,make(GNU make)将不会成功,因为在-f之后缺少文件名。为简单起见,我不必关心这个,因为它是用户的错,GNU make会报告这个问题。

Summary:

  • If user provide -f xxx in command parameter, I'll pass those parameter to make .
  • 如果用户在命令参数中提供-f xxx,我会将这些参数传递给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.
  • 如果用户没有提供-f xxx,我将使用所有用户的参数调用make,并附加-f Makefile.umk作为make的额外参数。

Thank you.

1 个解决方案

#1


0  

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.
  • 使用%~1是必须的。因为:如果%1具有值“大猫”,%〜1使它成为大猫,所以我们可以用引号来表示%〜1。

#1


0  

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.
  • 使用%~1是必须的。因为:如果%1具有值“大猫”,%〜1使它成为大猫,所以我们可以用引号来表示%〜1。