2

I have .bat code like this. For example:

@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

From CLI, If I enter the command gulpbat build it will be call to gulp build. But if I call gulpbat, it will be response Task never defined: default.

I would like to add default task to build if it is missing in the command line and will be use in %* automatically.

I have tried these but failed.

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 Answer 1

2
@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

2
  • I use something like this set "allArgs=%*" if "%noArgs%" == "true" ( if "%arg1%" == "" ( :: it is empty args AND argument1 (/arg) is empty set "allArgs=build" ) ) and append with %allArgs% instead of %*.
    – vee
    Jun 23 at 4:56
  • see last edit...
    – Io-oI
    Jun 23 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.