显示跳过的批评论

Displaying skipped batch comments

所以我想在我的 bat 文件中使用纯文本注释,并在顶部使用 goto 跳过文本,但想在说 / 时将文本显示为帮助信息?使用了开关..

我设法用这种方法显示了文本

@echo off
goto start
:help

some
text
here not commands


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

显示的是:

some

text

here not commands

有谁知道删除空行的方法吗?

@rojo

像这样?

@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

输出:

some
text
here not commands

Press any key to continue . . .

绝对有效!

我通常以 :: 开头表示注释,以 ::: 表示文档。然后我可以使用 FOR /F 和 FINDSTR 轻松打印文档,只要显示的文档行没有以 :.

开头
@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

如果我有很多文档,那么我可能会在顶部放置一个 GOTO :START 以缩短脚本启动时间。

@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

又一个! ;-)

@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