1

I'm working on a DSL that will compile to batch script (for fun...). I'm trying to make a function call, like that: the DSL defines function with parameter named param1. the batch defines a label with param1=%1. the DSL defines a call with some value. the batchs define a call with the value after a space.

The problem is that if the value has a space, it's defined as two parameters. I can escape spaces with ^, but then if i try to escape a double quote, it gets messed up.

Anyone can help me with the ultimate batch parameter escape?

BTW, its written with MPS, and it's here if you want it: https://github.com/TheAnosmic/MPSBatch

koko0
  • 41
  • 3

2 Answers2

0

I think there is no definite reliable way. The batch parser is really ugly. For example if you have a variable with a closing ) the following echo will break:

set VAR=Program Files (x86)
echo var=%VAR%.

In some circumstances it helps to use FOR

@set VAR=a * b "test" c ()
@for /F " delims==" %%V in ("%VAR%") do @echo var=%%V.

Will print

C:\Users>test.cmd
var=a * b "test" c ().
eckes
  • 10,103
  • 1
  • 59
  • 71
  • This might be a good solution for scripts, but when i'm writing a "Compiler", i want the script that it generates to be more beautiful. I don't care to change it, and i can write some logic with the variables, there must be a way to produce safe & beautiful code. – koko0 Apr 18 '14 at 00:35
0

The simplest solution would be to use double quotes to call your argument.

However, if this argument contains some double quotes, it does not work. In that case, we can implement a solution based on the solution from eckes: we can do a loop in the function to read all the parameters and copy them into the parameter of the function.

For example, this could look like that:

@echo off

set var=With "" and a space
call :call_var %var%
goto:EOF

:call_var
set param=
for /F " delims==" %%V in ("%*") do @set param=%param% %%V
REM Test parameter
echo %param%
goto:EOF
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107