[英]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.