解析包含逗号分隔值的命令行输入批处理文件 [英] Batch file for parsing command line input that contains comma separated values

查看:154
本文介绍了解析包含逗号分隔值的命令行输入批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,这需要设置的输入参数的命令一个命令行工具。
然后,这些输入参数进行验证对predefined名。该实用程序以这种方式调用的:

I am working on a command line utility that takes set of input parameters as command. Those input parameters are then validated against predefined names. The utility is invoked in this manner:

runUtility.cmd -A -B X,Y,Z -C是W

下面的参数是A,B和C(一个开头 - )。
现在,验证规则如下:

Here parameters are A, B and C (one that starts with -). Now the validation rules are as follows:


  1. 参数的名称应与predefined的名字,这样就可以不传递任何参数无效说-UVW

  1. Parameter's name should match predefined names, so that one can not pass any invalid parameter say -UVW

参数可以或可以不具有一个值。在上面的例子-A没有价值,而-B有X,Y,Z和-C有瓦特

Parameter may or may not have a value. In above example -A has no value, while -B has x,y,z and -C has w.

我写了这个code来验证输入:

I have written this code to validate the inputs:

:validate

set argument=%1
set argumentValue=%2


if "%argument%" == "-A" (   
    shift   
    goto validate
)

if "%argument%" == "-B" (
    if "%argumentValue%" == "" (
        echo Empty value for -B
        goto end
    )
    shift
    shift 
    goto validate       
)

if "%argument%" == "-C" (
    if "%argumentValue%" == "" (
        echo Empty value for -C
        goto end
    )
    shift
    shift  
    goto validate      
)

if %argument%" == "" (
        goto end
)

Argument %argument% is invalid

:end        

但是,这似乎并没有工作,因为-B已逗号分隔值,所以对b当它两班倒,在接下来的迭代,Y变成1%和z变为%2。由于y是不是一个参数,它失败,code的最后行权之争y是无效的。
其实逗号作为通过SHIFT命令分隔符,所以X,Y,Z确实仍然是一个单一的值。

But this does not seem to work, as -B has comma separated values, so for B when it does two shifts, in the next iteration, y becomes %1 and z becomes %2. Since y is not a parameter, it fails with last line of code that "Argument y is invalid". Actually comma is taken as delimiter by SHIFT command, so x,y,z does remain a single value.

我要X,Y,Z将被视为一个单一的值或有任何其他方式来处理呢?
我有点新的批量脚本,我试着用FOR循环但我没能在每次迭代得到%1%2在一起。

I want x,y,z to be taken as a single value OR is there any other way to process this? I am bit new to batch scripting , I tried with FOR loop but there I was not able to get %1 and %2 together in every iteration.

推荐答案

批量使用的空格,逗号,分号,制表符,和平等分隔参数的任意组合。如果你想在一个参数的任何这些字符,那么参数必须加引号。

Batch uses any combination of space, comma, semicolon, tab, and equal to delimit parameters. If you want to include any of those characters in a parameter, then the parameter must be quoted.

runUtility.cmd -A -B "x,y,z" -C w

您脚本可以删除与修改封闭引号。这也是一个好主意,引号内您的整个作业,以防止问题的字符,如&安培; | ,等在下面的分配的双引号不会被包含在值

Your script can remove the enclosing quotes with the ~ modifier. It is also a good idea to enclose your entire assignment within quotes to guard against problem characters like &, |, etc. The double quotes in the assignment below will not be included in the value.

set "argumentValue=%~2"


我发展,你可能想看看一个选项解析器:的Windows bat文件可选参数解析

解析code几乎没有变化,无论您定义的选项。有改变的唯一的东西都是使用同一个可变的选项定义, SHIFT / 3 应修改 SHIFT / 1 ,每个%〜3 变成%〜1 %〜 4 变成%〜2

The parsing code hardly changes regardless what options you define. The only things that have to change are the definition of your options using a single variable, the SHIFT /3 should be modified to SHIFT /1, every %~3 becomes %~1, and %~4 becomes %~2.

选项解析器为未指定的选项自动提供的缺省值的能力。

The option parser has the ability to automatically supply default values for unspecified options.

假设你没有默认值,你的选择将被定义为

Assuming you do not have default values, your options would be defined as

set "options=-A: -B:"" -C:"""

含义 -B -A 选项,这并不需要一个值,而 -C 称取值,但默认情况下是不确定的。选项

Meaning an -A option that does not take a value, and -B and -C options that take values but by default are undefined.

选项都存储在匹配选项名称的变量(包括破折号)。

The options are stored in variables that match the option name (dash included).

这篇关于解析包含逗号分隔值的命令行输入批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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