0

So I wanted to have plaintext comments in my bat file with useage information at the top by skipping the text using goto but wanted to display the text as help info if say /? switch was used..

I managed to get the text to display with this method

@echo off
goto start
:help

some
text
here not commands


@echo off
goto:eof
:start
echo on && prompt $h && call :help 2>nul

which displays this:

some

text

here not commands

Does anyone know a way to remove the blank lines?

Az-
  • 55
  • 4

3 Answers3

1

I typically begin lines with :: to indicate a comment, and ::: to indicate documentation. I can then use FOR /F with FINDSTR to easily print out the documentation, as long as no displayed documentation line begins with :.

@echo off
::Documentation
:::
:::some
:::text
:::here not commands
:::
:::

::Show help
call :help
exit /b

:help
for /f "tokens=* delims=:" %%A in ('findstr "^:::" "%~f0"') do @echo(%%A
exit /b

If I have a lot of documentation, then I may put a GOTO :START at the top to improve script start-up time.

@echo off
goto :start
::Documentation
:::
:::some
:::text
:::here not commands
:::
:::

:start
::Show help
call :help
exit /b

:help
for /f "tokens=* delims=:" %%A in ('findstr "^:::" "%~f0"') do @echo(%%A
exit /b
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

@rojo

like this?

@echo off
goto start
:help
call :heredoc help && goto helpend

some
text
here not commands


:helpend
goto:eof




:start

call :help
pause


:heredoc <uniqueIDX>
setlocal enabledelayedexpansion
set go=
for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
    set "line=%%A" && set "line=!line:*:=!"
    if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
    if "!line:~0,13!"=="call :heredoc" (
        for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
            if #%%i==#%1 (
                for /f "tokens=2 delims=&" %%I in ("!line!") do (
                    for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
                )
            )
        )
    )
)
goto :EOF

Output:

some
text
here not commands

Press any key to continue . . .

It definitely works!

rojo
  • 24,000
  • 5
  • 55
  • 101
Az-
  • 55
  • 4
  • Looks lovely to me, except you line wrapped a couple of lines in your paste. I fixed it for you. Anyway, now you can [include some fancy figlet text](http://patorjk.com/software/taag/#p=display&f=Larry%203D&t=rojo%20ftw!) within your script for great justice. – rojo Jul 29 '15 at 12:49
0

Another one! ;-)

@echo off
goto help-end
:help-start

some
text
here not commands


:help-end
if "%~1" neq "/?" goto exitHelp
set "start="
for /F "delims=:" %%a in ('findstr /N /B ":help" "%~F0"') do (
   if not defined start (set "start=%%a") else set "end=%%a"
)
for /F "skip=%start% tokens=1* delims=:" %%a in ('findstr /N "^" "%~F0"') do (
   if "%%a" equ "%end%" goto exitHelp
   echo(%%b
)
:exitHelp

echo Normal process continue here
Aacini
  • 65,180
  • 12
  • 72
  • 108