【问题标题】:Displaying skipped batch comments显示跳过的批注释
【发布时间】:2015-07-29 08:29:24
【问题描述】:

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

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

@echo off
goto start
:help

some
text
here not commands


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

显示这个:

一些

文字

这里不是命令

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

【问题讨论】:

  • 听起来像是echo 的工作:echo can you read this line?
  • 试试rojo's :heredoc子程序
  • 同意。试试我的 heredoc 子程序。
  • @rojo 在回答中发布了回复
  • 你的策略是个坏主意——想想如果你的文档中有一个实际的命令会发生什么。重要的是,您的代码永远不能执行您的文档。

标签: windows batch-file cmd stdout


【解决方案1】:

我通常以:: 开头表示注释,::: 表示文档。然后我可以使用 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

【讨论】:

    【解决方案2】:

    又一个! ;-)

    @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
    

    【讨论】:

      【解决方案3】:

      @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
      

      输出:

      一些
      正文
      这里不是命令

      按任意键继续。 . .

      绝对有效!

      【讨论】:

      • 对我来说看起来很可爱,除了你在粘贴中换了几行。我为你修好了。无论如何,现在你可以在你的脚本中include some fancy figlet text 为正义伸张正义。
      猜你喜欢
      • 2020-01-18
      • 2013-08-24
      • 2012-06-01
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多