如何为自定义 BAT 文件创建 HELP 命令?
How do I create HELP command for custom BAT files?
我已经编写了将文本转换为 uppercase
的小脚本,如下所示,并将此文件保存为 .BAT
扩展名
`converttoupper.bat`
我希望用户尝试“help”命令,以便他们获得有关使用命令的语法帮助,如下所示
help converttoupper
像这样
# help converttoupper
For more information on a specific command, type HELP command-name
CONVERTTOUPPER This converts the text to upper case
更新
即使我得到如下所示的东西,我也很好。我不想覆盖任何 windows 命令。
helpme converttoupper
or
helpme connectvpn
我有很多BAT文件,希望每个执行时都能显示各自的帮助
下面是安全处理 arg 捕获和帮助查询的示例。
安全捕获参数后,使用 Findstr 测试有效帮助开关的内容:
Set Args | %SystemRoot%\System32\Findstr.exe /bli "Args=\/? Args=-? Args=Help?" > nul && (Rem commands)
Set Args
:允许将参数字符串通过管道传输到 findstr 而不会因有毒字符而导致失败。
/bli
: findstr sawitches : 在忽略大小写的行开头匹配文字字符串。
"Args=\/? Args=-? Args=Help?"
: Space 分隔的要匹配的字符串列表;视为匹配字符串 a 或 b 或 c
> nul
:抑制任何匹配的输出
&&
:条件运算符; 'On command success'
注意:以?
终止每个帮助开关允许使用子字符串修改来删除前导开关和space并直接调用以查询关键字为前缀的标签
@Echo off & SETLOCAL
=========================================================================
Rem -- Arg capture method is a modified version of Dave Benhams method:
Rem -- https://www.dostips.com/forum/viewtopic.php?t=4288#p23980
SETLOCAL EnableDelayedExpansion
1>"%~f0:Params.dat" <"%~f0:Params.dat" (
SETLOCAL DisableExtensions
Set prompt=#
Echo on
For %%a in (%%a) do rem . %*.
Echo off
ENDLOCAL
Set /p "Args="
Set /p "Args="
Set "Args=!Args:~7,-2!"
@Rem duplicate Args for the purpose of counting doublequotes [destructive].
Set "DQcount=!Args!"
) || (
Echo(%~nx0 requires an NTFS drive system to function as intended.
CMD /C Exit -1073741510
) || Goto:Eof
If Not defined Args Goto:NoArgs
REM substitute doublequotes in Args clone 'DQcount'; count substring in string;
REM assess if count is even; If false "||": Remove doublequotes from string. If true "&&" and if entire
REM arg line is doublequoted, remove outer quotes.
Set Div="is=#", "1/(is<<9)"
Set "{DQ}=0"
Set ^"DQcount=!DQcount:"={DQ}!"
2> nul Set "null=%DQcount:{DQ}=" & Set /A {DQ}+=1& set "null=%"
Set /A !Div:#={DQ} %% 2! 2> nul && Set ^"Args=!Args:"=!" || If [^%Args:~0,1%^%Args:~-1%] == [""] Set "Args=!Args:~1,-1!")
For /f Delims^= %%G in ("!Args!")Do Endlocal & Set "Args=%%G" 2> nul
:NoArgs
=====================================================================
Rem help query assessment
(
Set Args | %SystemRoot%\System32\Findstr.exe /bli "Args=\/? Args=-? Args=Help?" > nul && (
Rem Args value has leading /? -? or help?
If not "%Args:*?=%"=="" (
Rem Args value contains leading /? -? or help? with additional Parameter
Call:%Args:*? =%_Syntax && Goto:Eof || (
Rem quit after Call to Syntax info if valid Parameter; else notify invalid and show valid syntax queries.
Echo(Invalid query: "%Args:*? =%" : Does not Match a valid Help Query:
)
)
Rem show valid syntax queries.
For /F "Tokens=1 Delims=:_" %%G in ('%SystemRoot%\System32\Findstr.exe /R "^:.*_Syntax" "%~f0"') Do Echo(%~nx0 /? %%G
ENDLOCAL & Exit /b 0
)
) 2> nul
Set Args
Goto:Eof
Rem Demo syntax labels
:Demo_Syntax
Echo %~0 help info
Exit /b 0
:Example_Syntax
Echo %~0 help info
Exit /b 0
您可以创建一个“假”函数。我们将其命名为 define.cmd
并将其放入 %systemroot%\system32
我们添加代码:
@echo off
for /f "tokens=1,*delims=? " %%i in ('type "%~1" ^|findstr ":?"') do echo %%j
然后在您希望人们阅读帮助的所有批处理文件中,以 :?
开始添加帮助行,使用您的 convertoupper.cmd
文件作为示例:
@echo off & set upper=
if "%~1" == "" echo incorrect usage & call define.cmd "%0"
if "%~1" == "/?" call define.cmd "%0"
for /f "skip=2 delims=" %%I in ('tree "\%~1"') do if not defined upper set "upper=%%~I"
set "upper=%upper:~3%"
echo %upper%
goto :eof
:? # help converttoupper
:? "define %0" or "%0 /?" will display this help content
:? For more information on a specific command, type HELP command-name
:? CONVERTTOUPPER This converts the text to upper case
现在您可以 运行 define converttoupper
或 converttoupper /?
。如果你 运行 converttoupper
没有任何参数,它也会显示相同的帮助。
我已经编写了将文本转换为 uppercase
的小脚本,如下所示,并将此文件保存为 .BAT
扩展名
`converttoupper.bat`
我希望用户尝试“help”命令,以便他们获得有关使用命令的语法帮助,如下所示
help converttoupper
像这样
# help converttoupper
For more information on a specific command, type HELP command-name
CONVERTTOUPPER This converts the text to upper case
更新
即使我得到如下所示的东西,我也很好。我不想覆盖任何 windows 命令。
helpme converttoupper
or
helpme connectvpn
我有很多BAT文件,希望每个执行时都能显示各自的帮助
下面是安全处理 arg 捕获和帮助查询的示例。
安全捕获参数后,使用 Findstr 测试有效帮助开关的内容:
Set Args | %SystemRoot%\System32\Findstr.exe /bli "Args=\/? Args=-? Args=Help?" > nul && (Rem commands)
Set Args
:允许将参数字符串通过管道传输到 findstr 而不会因有毒字符而导致失败。/bli
: findstr sawitches : 在忽略大小写的行开头匹配文字字符串。"Args=\/? Args=-? Args=Help?"
: Space 分隔的要匹配的字符串列表;视为匹配字符串 a 或 b 或 c> nul
:抑制任何匹配的输出&&
:条件运算符; 'On command success'
注意:以?
终止每个帮助开关允许使用子字符串修改来删除前导开关和space并直接调用以查询关键字为前缀的标签
@Echo off & SETLOCAL
=========================================================================
Rem -- Arg capture method is a modified version of Dave Benhams method:
Rem -- https://www.dostips.com/forum/viewtopic.php?t=4288#p23980
SETLOCAL EnableDelayedExpansion
1>"%~f0:Params.dat" <"%~f0:Params.dat" (
SETLOCAL DisableExtensions
Set prompt=#
Echo on
For %%a in (%%a) do rem . %*.
Echo off
ENDLOCAL
Set /p "Args="
Set /p "Args="
Set "Args=!Args:~7,-2!"
@Rem duplicate Args for the purpose of counting doublequotes [destructive].
Set "DQcount=!Args!"
) || (
Echo(%~nx0 requires an NTFS drive system to function as intended.
CMD /C Exit -1073741510
) || Goto:Eof
If Not defined Args Goto:NoArgs
REM substitute doublequotes in Args clone 'DQcount'; count substring in string;
REM assess if count is even; If false "||": Remove doublequotes from string. If true "&&" and if entire
REM arg line is doublequoted, remove outer quotes.
Set Div="is=#", "1/(is<<9)"
Set "{DQ}=0"
Set ^"DQcount=!DQcount:"={DQ}!"
2> nul Set "null=%DQcount:{DQ}=" & Set /A {DQ}+=1& set "null=%"
Set /A !Div:#={DQ} %% 2! 2> nul && Set ^"Args=!Args:"=!" || If [^%Args:~0,1%^%Args:~-1%] == [""] Set "Args=!Args:~1,-1!")
For /f Delims^= %%G in ("!Args!")Do Endlocal & Set "Args=%%G" 2> nul
:NoArgs
=====================================================================
Rem help query assessment
(
Set Args | %SystemRoot%\System32\Findstr.exe /bli "Args=\/? Args=-? Args=Help?" > nul && (
Rem Args value has leading /? -? or help?
If not "%Args:*?=%"=="" (
Rem Args value contains leading /? -? or help? with additional Parameter
Call:%Args:*? =%_Syntax && Goto:Eof || (
Rem quit after Call to Syntax info if valid Parameter; else notify invalid and show valid syntax queries.
Echo(Invalid query: "%Args:*? =%" : Does not Match a valid Help Query:
)
)
Rem show valid syntax queries.
For /F "Tokens=1 Delims=:_" %%G in ('%SystemRoot%\System32\Findstr.exe /R "^:.*_Syntax" "%~f0"') Do Echo(%~nx0 /? %%G
ENDLOCAL & Exit /b 0
)
) 2> nul
Set Args
Goto:Eof
Rem Demo syntax labels
:Demo_Syntax
Echo %~0 help info
Exit /b 0
:Example_Syntax
Echo %~0 help info
Exit /b 0
您可以创建一个“假”函数。我们将其命名为 define.cmd
并将其放入 %systemroot%\system32
我们添加代码:
@echo off
for /f "tokens=1,*delims=? " %%i in ('type "%~1" ^|findstr ":?"') do echo %%j
然后在您希望人们阅读帮助的所有批处理文件中,以 :?
开始添加帮助行,使用您的 convertoupper.cmd
文件作为示例:
@echo off & set upper=
if "%~1" == "" echo incorrect usage & call define.cmd "%0"
if "%~1" == "/?" call define.cmd "%0"
for /f "skip=2 delims=" %%I in ('tree "\%~1"') do if not defined upper set "upper=%%~I"
set "upper=%upper:~3%"
echo %upper%
goto :eof
:? # help converttoupper
:? "define %0" or "%0 /?" will display this help content
:? For more information on a specific command, type HELP command-name
:? CONVERTTOUPPER This converts the text to upper case
现在您可以 运行 define converttoupper
或 converttoupper /?
。如果你 运行 converttoupper
没有任何参数,它也会显示相同的帮助。