如何将我所有的功能打包到一个单独的批处理文件中? [英] How to package all my functions in a batch file as a seperate file?

查看:182
本文介绍了如何将我所有的功能打包到一个单独的批处理文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与问题有关.我有一些需要从批处理文件执行的动作,我想将它们建模为函数并从主序列中调用.从上述问题来看,很明显,我可以使用调用语法来做到这一点

My question is related to this question. I have several bunch of actions that need to be executed from a batch file and I would like to model them as functions and call from a master sequence. From the above question, it is clear that I can do this with the call syntax

call:myDosFunc

我的问题是,我可以将所有这些功能放在单独的批处理文件(functions.bat)中,然后以某种方式包含"在主批处理文件中并对其进行调用吗?另一种选择是利用调用语法从main.bat调用functions.bat的可能性,但是我不确定是否可以使用特定函数来调用它,而不是执行整个批处理文件.

My question is that can I place all these functions in a seperate batch file (functions.bat) and somehow 'include' that in the main batch file and call them? Another option would be to utilize the possibility to invoke functions.bat from main.bat with the call syntaxt, but I'm not sure if I can invoke that with a specific function instead of executing the whole batch file.

简而言之,我正在寻找与C编程世界类似的东西,其中我的函数驻留在DLL中,而主程序仅包含高级逻辑并从DLL中调用函数.

In short, I'm looking for something similar to the C programming world where my functions reside in a DLL and the main program contains only the high-level logic and calls the functions from the DLL.

推荐答案

这是一个简单的示例,说明如何完成.

Here is a simple example of how it might be done.

以功能名称作为第一个参数调用功能脚本,并将功能参数作为arg2,arg3,...

The function script is called with the name of the function as the first argument, and function arguments as arg2, arg3, ...

假设脚本被正确调用,脚本将转移参数并对原始arg1执行GOTO.然后,该函数的参数以新的arg1开头.这意味着您可以采用已经编写的例程并将其放入实用程序中,而不必担心调整参数编号.

Assuming it is called properly, the script shifts the arguments and performs GOTO to the original arg1. Then the function has its arguments starting with the new arg1. This means you can take already written routines and plop them in the utility without having to worry about adjusting the parameter numbers.

如果未提供功能参数,或者功能参数与脚本中的有效标签不匹配,则脚本会给出错误消息.

The script gives an error if the function argument is not supplied, or if the function argument does not match a valid label within the script.

@echo off
if "%~1" neq "" (
  2>nul >nul findstr /rc:"^ *:%~1\>" "%~f0" && (
    shift /1
    goto %1
  ) || (
    >&2 echo ERROR: routine %~1 not found
  )
) else >&2 echo ERROR: missing routine
exit /b

:test1
echo executing :test1
echo arg1 = %1
exit /b

:test2
echo executing :test2
echo arg1 = %1
echo arg2 = %2
exit /b

:test3
echo executing :test3
echo arg1 = %1
echo arg2 = %2
echo arg3 = %3
exit /b


我更喜欢上面使用的GOTO方法.另一个选择是改为使用CALL,就像Thomas在他的回答中所做的那样.


I prefer the GOTO approach that I used above. Another option is to use CALL instead, as Thomas did in his answer.

有关使用CALL技术的批处理函数的有用库的工作示例,请参见 CHARLIB.BAT ,一个例程库,用于处理批处理文件中的字符和字符串. 此处

For a working example of a usefull library of batch functions that uses the CALL technique, see CHARLIB.BAT, a library of routines for processing characters and strings within a batch file. A thread showing development of the library is available here

几年前,我写了CharLib.bat.如果我今天要编写它,我可能会使用GOTO而不是CALL.

I wrote CharLib.bat a few years ago. Were I to write it today, I would probably use GOTO instead of CALL.

引入CALL的问题在于,在将字符串文字作为参数传递时会产生问题.额外的CALL意味着包含%的字符串文字必须使百分比增加一倍.这也意味着未引用的有毒字符(如&|)将需要转义额外的时间.呼叫者可以解决这两个问题.但是真正的问题是每个CALL都将加引号的插入号加倍:"^"变为"^^".解决插入符加倍问题没有很好的方法.

The problem with introducing a CALL is that it creates issues when passing string literals as parameters. The extra CALL means that a string literal containing % must have the percents doubled an extra time. It also means unquoted poison characters like & and | would need to be escaped an extra time. Those two issues can be addressed by the caller. But the real problem is that each CALL doubles up quoted carets: "^" becomes "^^". There isn't a good way to work around the caret doubling problem.

额外的CALL问题不会影响CharLib.bat,因为字符串值是通过引用(变量名)而不是作为字符串文字传递的.

The problems with the extra CALL don't impact CharLib.bat because string values are passed by reference (variable name) and not as string literals.

将GOTO与SHIFT/1一起使用的唯一缺点是您不能使用%0来获取当前正在执行的例程的名称.我本可以在没有/1的情况下使用SHIFT,但是您将无法在例程中使用%~f0来获取执行批处理文件的完整路径.

The only down side to using GOTO with SHIFT /1 is that you cannot use %0 to get the name of the currently executing routine. I could have used SHIFT without the /1, but then you wouldn't be able to use %~f0 within a routine to get the full path to the executing batch file.

这篇关于如何将我所有的功能打包到一个单独的批处理文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆