2

I want to pass a bunch of boolean flags to a Windows batch script function. Something like this:

1   SETLOCAL
2   SET _debug=1
3   SET _x64=1
4   
5   call :COMPILE %_debug% %_x64%
6   :: call again where %_debug% is now 0
7   :: call again where %_x64% is now 0
8   :: etc...
9   
10  :COMPILE
11  :: stuff depending on input args

I am using variables _debug and _x64 to make the calls (line 5-7) more readable, as opposed to something like this:

call :COMPILE 0 1

Is there a simple way to pass the equivalent of not variable? Like:

call :COMPILE ~%_debug% ~%_64%

or

call :COMPILE (%_debug% eq 1) (%_64% eq 1)

or do I have to declare not-variables like:

SET _debug=1
SET _not_debug=0
SET _x64=1
SET _not_x64=0

It's easy enough when I just have these 2 variables, but I anticipate having more.


edit based on initial response:

Regarding lines 6 and 7, where I wrote:

6   :: call again where %_debug% is now 0
7   :: call again where %_x64% is now 0

I am not interested in actually changing the values of _debug and _x64 to 0. What I want to know is if there is a way to pass "not _var" to the function. This way I can preserve the meaning of the argument.

Blair Fonville
  • 908
  • 1
  • 11
  • 23

2 Answers2

5

You could minimize the code it takes to set up your various variables:

for %%V in (debug x64) do set /a _%%V=1, _not_%%V=0

It might seem silly to do the above for just two flags with four values. But it becomes more and more attractive as you add more flags.

But I don't see why you don't take this one step further - why not have your routines expect meaningful text arguments instead of cryptic integral values. Not only will your CALLs be well documented, but the routine code will also be easier to follow. And the CALLer no longer needs to worry about translating meaningful text into an integer value.

If you find yourself developing complex routines with optional arguments, then you should have a look at https://stackoverflow.com/a/8162578/1012053

dbenham
  • 127,446
  • 28
  • 251
  • 390
2

Unfortunately, there is no way to do any kind of (bit) arithmetics in an arbitrary command line, so you always have to use interim variables together with the set /A command:

:: ...
call :COMPILE %_debug% %_x64%
set /A "_not_debug=!_debug, _not_x64=!_x64"
call :COMPILE %_not_debug% %_not_x64%
:: ...
aschipfl
  • 33,626
  • 12
  • 54
  • 99