批处理文件if if语句是否按预期工作 - 语法错误?

时间:2016-06-06 22:58:10

标签: batch-file cmd

所以我相信我在批处理文件中遇到了一个问题,我正在尝试编写。我是编写批处理文件的新手,我仍在学习该语言的工作原理。基本上,我有一个可以使用-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

2 个答案:

答案 0 :(得分:0)

更改

    endlocal
    exit /b 0
)
else (
    goto end
)

    endlocal
    exit /b 0
) else (
    goto end
)

产生了以下结果。

C:>labstuff.bat
*********************************************
**
**     Script Version: x.x
**         Started at: 13:17:55.30
**    For options use: ScriptNameHere -help
**
*********************************************
** Script Complete at 13:17:55.32

C:>labstuff.bat -run
*********************************************
**
**     Script Version: x.x
**         Started at: 13:16:15.62
**    For options use: ScriptNameHere -help
**
*********************************************

* Setting Special Arguments
-- Found -run
The system cannot find the batch label specified - labelOne
The system cannot find the batch label specified - labelTwo
The system cannot find the batch label specified - labelThree

答案 1 :(得分:0)

所以...经过无数次面对面的拍摄,我想我已经明白了。

基本上,我从if语句中取出逻辑并将它们放在标签中,然后在if语句中,我指向标签。不知道为什么会这样,但一切都运转正常,所以我会接受它!

echo *********************************************
echo **
echo **     Script Version: x.x
echo **         Started at: %time%
echo **    For options use: ScriptNameHere -help
echo **
echo *********************************************

if /i [%1]==[-help] ( goto end )
if /i [%1]==[-run] ( goto end )
if /i [%1]==[] ( goto end )

: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

:run
call :process_args %*

call :labelOne
call :labelTwo
call :labelThree

endlocal
exit /b 0

: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