Search code examples
batch-filecommentsline-continuation

Combining a multi line batch command with inline comments


I have a very long command line string and i want to comment some arguments/switches and describe what they do and what can be adjusted with them. the following simplified batch-file does not work, but its more or less what I want to do:

executable^
 -switch1=3^                   rem should be 3 or 5, three means xxx and 5 means yyy
 -argument1=some-file.txt^
 -file-format=csv^             rem output file format, supported are csv and xml
 -out=result                   rem written file, file extension is copied from file format

I've created a simple script to test the syntax of combining comments with the split command string but I could not find a working solution:

rem WORKS, no space after caret
echo aaa ^
bbb

rem does not work, space after caret
echo aaa ^ 
bbb

rem WORKS space in the next line
echo aaa ^
 bbb
echo(
echo and now with rem-comments
echo(

rem does not work
echo aaa ^rem comment
bbb

rem does not work
echo aaa ^&rem comment
bbb

rem does not work
echo aaa ^ rem comment
bbb

rem does not work
echo aaa ^ &rem comment
bbb
echo(
echo and with double-colon-comments
echo(

:: does not work
echo aaa ^:: comment
bbb

:: does not work
echo aaa ^&:: comment
bbb

:: does not work
echo aaa ^ :: comment
bbb

:: does not work
echo aaa ^ &:: comment
bbb

Any character after the caret seems to break it, so it might not be possible at all to have a inline-comment after a caret? Does someone has an idea/solution?


Solution

  • You can use percent comments.
    They disappears completely from the line, so they don't interfere.

    echo aaa ^%=     My first comment here =%
    bbb^%=           Second comment        =%
    ccc%=            My last comment       =%
    

    It works because the parser tries to expand the comment as a variable, but if it can't find such a variable, it expands to an empty string.
    Therefore you should start with %=, because variables can not be created with a starting equal sign.
    It's important not to include colons into the comments, because they are treated as special variable modification operators and breaks the comment completely.

    A percent comment can be used at nearly any position in the line.

    echo aaa ^%= My comment here =%
    bbb%= This comment is in front of the caret=%^
    c%=This comment is inside the text =%cc