我有这样的 .bat 代码。例如:
@ECHO OFF
setlocal EnableDelayedExpansion
call :setESC
set arg1=%1
set "noArgs=true"
for %%a in (%*) do (
if not %%a == "" (
:: if it is not empty arguments.
set "noArgs=false"
)
)
if "%noArgs%" == "true" (
if "%arg1%" == "" (
:: it is empty args AND argument1 (/arg) is empty
:: set default argument here. <-----------
set "%%1=build"
)
)
:: for debugging.
echo all args: %* :end.
:: run external command (gulp).
gulp --cwd "/my/project" %*
gulpbat脚本
从 CLI,如果我输入命令,gulpbat build
它将被调用gulp build
。但是如果我调用gulpbat
,它将被响应任务从未定义:默认。
如果命令行中缺少默认任务,我想添加默认任务并将其自动build
使用。%*
我尝试过这些但失败了。
set "%1=build" :: error: The syntax of the command is incorrect.
set "%~1=build" :: error: The syntax of the command is incorrect.
set "%%1=build" :: not working in %*
答案1
@echo off && setlocal EnableDelayedExpansion
:: // call :setESC // :label |or| :function not exist in your code! //
if "%~1" == "build" (
set "_arg1=build"
)else if not "%~1" == "" (
set "_arg1=%~1" && for %%Z in (%*)do set "_args_=!_args_!"%%~Z" "
)else set "_args_default=/define /your /defaults /args /here"
:: for debugging.
echo\ all args: !_args_! :end
:: run external command (gulp).
if defined _args_ (
gulp --cwd "/my/project" !_args_!
)else if defined _arg1 (
gulp --cwd "/my/project" !_arg1!
)else gulp --cwd "/my/project" !_args_default!
endlocal