Как установить аргумент по умолчанию в .bat-файле?

Как установить аргумент по умолчанию в .bat-файле?

У меня есть такой .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.bat

Из 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

Связанный контент