0

I'm looping through all command-line arguments using SHIFT. I'm getting result of ECHO is off.. It is likely printing the empty variable.

:argLoopStart
    SET paramName=
    SET arg=%1
    IF -%arg%-==-- GOTO argLoopEnd
    IF %arg:~0,2%==-- (
        SET paramName=%arg%
        ECHO %arg%
        ECHO %paramName%
    ) 
    SHIFT
    GOTO argLoopStart
:argLoopEnd

By running the command fake-command --dbs=mydbname, I got this:

--dbs
ECHO is off.

According to the code above, ECHO %arg% prints --dbs and ECHO %paramName% prints ECHO is off. The line of SET paramName=%arg% is not working as I expected. %parameName% should print --dbs as well. However, it seems printing an empty variable.

Sithu
  • 4,752
  • 9
  • 64
  • 110
  • Why are you expecting `%paramName%` to be `--dbs` and not `--dbs=mydbname`? – SomethingDark Dec 21 '14 at 06:05
  • `--dbs=mydbname` is equal to `--dbs mydbname`. `=` is ignored by the compiler. As you see, `%paramName%` should be same as`%arg%` which is printing `--dbs` because of the line `SET paramName=%arg%`. – Sithu Dec 21 '14 at 06:16

1 Answers1

1

You need to enable delayed expansion with SETLOCAL EnableDelayedExpansion at the top of your script:

Delayed Expansion will cause variables to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL command. When delayed expansion is in effect variables may be referenced using !variable_name! (in addition to the normal %variable_name% )

@echo off
setlocal enabledelayedexpansion

:argLoopStart
set paramName=
set arg=%1
if -!arg!-==-- goto argLoopEnd
if %arg:~0,2%==-- (
    set paramName=!arg!
    echo !arg!
    echo !paramName!
)
shift
goto argLoopStart

:argLoopEnd
Sithu
  • 4,752
  • 9
  • 64
  • 110
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • `EnableDelayedExpansion` is already at the top of my script. The code I mentioned is just a part of the whole script. Anyway, can you please explain why it is needed? – Sithu Dec 21 '14 at 06:37
  • You still need to replace the `%` with `!` in order for it to work. As for _why_ it works, I'm not entirely sure, as I'm not too familiar with the inner workings of batch. That said, your code is acting in a similar manner to a `for` loop, which needs delayed expansion enabled for variable values inside of it to update with every loop iteration. – SomethingDark Dec 21 '14 at 06:40
  • @sithu: Please post all significant parts of your script. Guessing games become boring after about the five thousandth time. Please search SO for `delayed expansion` for hundreds of articles explaining why the `!var!` syntax - or `call %%var%%` is necessary. Why are you invoking `enabledelayedexpansion` if you evidently don't know why it's used? – Magoo Dec 21 '14 at 06:43
  • @Magoo There are just a few lines (3 or 4 lines) of `SET` for other variables before this block of code. I'm coding those argument processing at the topmost possible. Adding explanation about `EnableDelayedExpansion` in the answer would increase the quality of the answer, right? It is sad that you did not think like that. – Sithu Dec 21 '14 at 06:55
  • 1
    @Sithu I've been looking for an explanation for why delayed expansion is needed that is better than "because it won't work otherwise," and I found [this answer](http://stackoverflow.com/a/8194279/4158862) by Jeb that suggests that `set` commands inside of parentheses blocks need it in order to be processed at the correct time. – SomethingDark Dec 21 '14 at 07:08