批处理:检查参数数量 [英] Batch : Checking the number of parameters

查看:211
本文介绍了批处理:检查参数数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保在调用我的批处理时,传递的参数不超过2个.

I'd like to make sure that when calling my batch, no more than 2 parameters are passed.

是否有一种简便的方法来检查该值,或者我必须根据需要多次调用SHIFT直到参数值为空?

Is there an easy way to check that, or do I have to call SHIFT as many times as needed until the parameter value is empty ?

推荐答案

您可以简单地测试是否存在第三个参数,如果存在则取消:

You can simply test for existence of a third parameter and cancel if present:

if not "%~3"=="" (
    echo No more than two arguments, please
    goto :eof
)

但是,更具体地说,没有直接方法来获取传递给批处理的参数数量,除非shift并对其进行计数.因此,如果您要确保传递的参数不超过19个,则需要执行此操作.但是,如果预期参数的数量小于9,则上述方法效果很好.

But more specifically, there is no direct way of getting the number of arguments passed to a batch, short of shifting and counting them. So if you want to make sure that no more than 19 arguments are passed, then you need to do exactly that. But if the number of expected arguments is below 9 above method works well.

这篇关于批处理:检查参数数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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