.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

관련 정보