【发布时间】:2016-06-06 22:58:10
【问题描述】:
所以我相信我在尝试编写的批处理文件中存在语法问题。我是编写批处理文件的新手,我仍在学习该语言的工作原理。本质上,我有一个可以使用-run 和其他命令运行的脚本:
ScriptName -run -prereq
我还希望能够为可以使用脚本运行的任何命令提供帮助菜单:
ScriptName -help
后跟更多用户需要帮助的输入参数
** Enter any additional input commands for Script help:
用户输入将类似于:
-prereq -one -two -three
-run 命令应该实际运行并应用这些命令。
-help 命令应该只回显有关请求命令的信息。
只运行没有-run 或-help 命令的脚本会打印一个非常基本的标题,其中包含-help 存在的版本、时间和解释(如下面的代码所示)
基本上,-run 应该只根据应用的进程调用进程,从标签:process_args 可以看出,同样,-help 将首先采用-help 命令启动,然后它会询问有关更多参数,如 :help_process_args 中所见,它将调用回显信息的标签。
帮助功能似乎有效,但如果我运行没有-run 参数的脚本,它默认为-run 功能。此外,在尝试修复默认行为时,我不断看到语法错误。
谁能明白为什么-help 有效,为什么代码默认为不带参数的-run 行为,而不是打印短标题?此外,是否有任何明显的语法错误? (再次,我是批处理文件的新手)
这里是显示帮助菜单创建和调用运行和帮助进程的相关代码。
echo *********************************************
echo **
echo ** Script Version: x.x
echo ** Started at: %time%
echo ** For options use: ScriptNameHere -help
echo **
echo *********************************************
if /i [%1]==[-help] (
echo ** Options:
echo *********************************************
echo **
echo ** -run = Runs the script
echo ** -prereq = Skip prereq steps
echo **
echo ** Enter any additional input commands for Script help:
echo **
goto help_input
endlocal
exit /b 0
)
if /i [%1]==[-run] (
call :process_args %*
call :labelOne
call :labelTwo
call :labelThree
endlocal
exit /b 0
)
else (
goto end
)
:help_input
set input=
set /p input= ** :
goto help_process
:help_process
call :process_help_args %input%
if /i not "%_help_opts:-echo=%"=="%_help_opts%" goto:eof
call :helpLabelOne
call :helpLabelTwo
call :helpLabelThree
:process_help_args
if %errorlevel% neq 0 exit /B %errorlevel%
echo. && echo * Setting Special Arguments
:help_args_loop
if not [%1] equ [] (
if [%_help_opts%] neq [] set _help_opts=%_help_opts%/%1
if [%_help_opts%] equ [] set _help_opts=%1
echo -- Found %1
shift
goto help_args_loop
) else (
if not defined _help_opts (
echo * No Options Provided
set _help_opts=**EMPTY**
)
)
goto:eof
:process_args
if %errorlevel% neq 0 exit /B %errorlevel%
echo. && echo * Setting Special Arguments
:args_loop
if not [%1] equ [] (
if [%_opts%] neq [] set _opts=%_opts%/%1
if [%_opts%] equ [] set _opts=%1
echo -- Found %1
shift
goto args_loop
) else (
if not defined _opts (
echo * No Options Provided
set _opts=**EMPTY**
)
)
goto:eof
:end
endlocal
echo ** Script Complete at %time%
exit /B 0
【问题讨论】:
-
else部分必须放在“then”部分的右括号的同一行中(就像您拥有的另外两个if一样),否则“else”是作为另一个命令。 -
我已经尝试过了,但似乎并没有给我不同的结果。我还读到括号周围需要有空格,所以我也在寻找它。
-
不是答案,而是:LabelOne、:LabelTwo、:LabelThree、:HelpLabelOne 等这些东西在哪里?
-
@Liturgist 他们是调用标签的例子,只是回显信息,所以他们不应该对这个问题有任何意义。
标签: batch-file cmd