实现参数重新排序的代码基本已经搞定。
@echo off & setlocal enabledelayedexpansion
call :callee /n p1 /i p2 /x p3
pause
goto :EOF
:callee _param1 _param2 _param3 /n /i /x
set index=1
set options=
:loop
call set param=%%~%index%
if defined param (
if "%param:~0,1%"=="/" (
set options=%options% %param%
shift /%index%
) else (
set /a index+=1
)
goto :loop
)
echo %1 %2 %3 %options%
goto :EOF
现在还有一个问题:我怎么把调整排序的代码“模块化”:作为一个单独的过程,存放到另一个bat文件中?
我的想法是这样:
1. 用goto:调整完成后再调用goto跳回来。但是,如果用goto,调整参数次序的代码应该不能放到另一个bat文件中吧?
@echo off & setlocal enabledelayedexpansion
call :callee /n p1 /i p2 /x p3
pause
goto :EOF
:callee _param1 _param2 _param3 /n /i /x
goto :adjust
:adjust_finished
echo %1 %2 %3 %options%
goto :EOF
:adjust
set index=1
set options=
:loop
call set param=%%~%index%
if defined param (
if "%param:~0,1%"=="/" (
set options=%options% %param%
shift /%index%
) else (
set /a index+=1
)
goto :loop
)
goto adjust_finished
2. 用call:这样就需要把参数传递给adjust,但是调整后的参数要怎样应用到callee中去呢?在adjust中再调用一次callee,确保callee这一次完全执行并忽略adjust。callee调用完成后,退回到adjust,再退回到callee,在callee中需要一些代码来阻止它再执行一遍。
这种方法好像会让代码变得很奇怪。
PS:想错了,因为在adjust内使用了标签:callee,所以它也不能存到另一个bat文件中
@REM 假设参数在9个以内
@echo off & setlocal enabledelayedexpansion
call :callee /n p1 /i p2 /x p3
pause
goto :EOF
:callee _param1 _param2 _param3 /n /i /x
if not defined calling call :adjust callee %*
if defined called (
set calling=
set called=
goto :EOF
)
echo %*
goto :EOF
:adjust _caller %*
set calling=true
set caller=%1
shift
set index=1
set params=
set options=
:loop
call set param=%%~%index%
if defined param (
if "%param:~0,1%"=="/" (
set options=%options% %param%
shift /%index%
) else (
set /a index+=1
)
goto :loop
)
set index-=1
for /l %%i in (1,1,%index%) do call set params=!params! %%%%i
call :%caller% %params%%options%
set called=true
goto :EOF
请问一下有其它的实现方法吗?或纠正一下我上面的两种想法?谢谢!
Last edited by obsolete on 2008-7-20 at 06:47 PM ]