繁体   English   中英

显示跳过的批处理注释

[英]Displaying skipped batch comments

因此,我想通过使用goto跳过文本在bat文件中的明文注释中使用信息位于顶部,但想显示文本作为帮助信息(如果说/?)。 使用了开关。

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

@echo off
goto start
:help

some
text
here not commands


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

显示以下内容:

一些

文本

这里不是命令

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

我通常以::表示注释,以:::表示文档开始。 然后,只要没有显示的文档行以:开头,我就可以将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

@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

输出:

一些
文本
这里不是命令

按任意键继续 。

绝对可以!

另一个! ;-)

@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM