与开关将参数传递给一个批处理文件 [英] Passing parameters with switches to a batch file

查看:109
本文介绍了与开关将参数传递给一个批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何显示与开关A关联的值,也不管交换机B,其中A和B是指定的顺序呢?

How do I display the value associated with switch A and switch B regardless of the order in which A and B was specified?

考虑以下调用批处理文件ParamTest.cmd:

Consider the following call to batch file ParamTest.cmd:

C:\Paramtest.cmd \A valueA \B valueB

下面是C的内容:\\ Paramtest.cmd:

Here's the contents of C:\Paramtest.cmd:

ECHO Param1=%1
ECHO Param2=%2

ECHO Param3=%3
ECHO Param4=%4

输出:

Param1=\A 
Param2=valueA
Param3=\B
Param4=valueB

我想能够识别的两个的其开关的名称,A和B,通过价值无论在哪个这些交换机获得通过德秩序

I would like to be able to identify TWO values passed by their switch names, A and B, regardless of teh order in which these switches were passed

例如,如果我执行以下调用:

For example, if I execute the following call:

C:\Paramtest.cmd \B valueB \A valueA

我想能够显示

A=ValueA
B=ValueB

..并具有相同的输出,即使我打电话的参数顺序批处理文件转换:

..and have the same output even if I called the batch file with the param order switched:

C:\\ Paramtest.cmd \\ A值a \\乙valueB,则

C:\Paramtest.cmd \A valueA \B valueB

我如何做到这一点?

推荐答案

在总之,你需要定义一个循环,并在对处理参数。

In short, you need to define a loop and process the parameters in pairs.

我通常使用过程涉及标签及放大器的方法的参数列表; GOTO语句,以及班次,基本上是这样的:

I typically process the parameter list using an approach that involves labels & GOTOs, as well as SHIFTs, basically like this:

…
SET ArgA=default for A
SET ArgB=default for B

:loop
IF [%1] == [] GOTO continue
IF [%1] == [/A] …
IF [%1] == [/B] …
SHIFT & GOTO loop

:continue
…

也可以处理使用%* 面具和循环,这样的参数:

It is also possible to process parameters using the %* mask and the FOR loop, like this:

…
SET ArgA=default for A
SET ArgB=default for B

FOR %%p IN (%*) DO (
  IF [%%p] == [/A] …
  IF [%%p] == [/B] …
)
…

不过,这对你的情况有点困难,因为你需要处理的参数成对。第一种方法更灵活,在我看来。

But it's a bit more difficult for your case, because you need to process the arguments in pairs. The first method is more flexible, in my opinion.

这篇关于与开关将参数传递给一个批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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