Apache Commons CLI:获取选项的值列表 [英] Apache Commons CLI : Getting list of values for an option

查看:179
本文介绍了Apache Commons CLI:获取选项的值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于CLI,我需要传入一个int数组作为特定选项的输入。

For a CLI, I have a requirement to pass in an array of ints as input for a particular option.

示例 - 以下命令将接受一个customerIds数组并执行一些操作。

Example - The below command would take in an array of customerIds and perform some operations.


myCommand -c 123 124 125

myCommand -c 123 124 125

我使用Apache commons CLI实现了CLI,我正在使用getOptionValues( c)来检索这个数组。

I have implemented the CLI using Apache commons CLI, and I am using getOptionValues("c") to retrieve this array.

问题是,这只返回数组中的第一个元素,即[123],而我期望它返回[123,124,125] 。

The problem is that, this is returning only the first element in the array, i.e. [123], while I am expecting it to return [123, 124, 125].

我的代码的精简版本,

CommandLine cmd;
CommandLineParser parser = new BasicParser();
cmd = parser.parse(options, args);
if (cmd.hasOption("c")){
String[] customerIdArray = cmd.getOptionValues("c");
// Code to parse data into int
}

有人可以帮助我在这里找出问题?

Can someone help me identify the issue here?

推荐答案

你必须设置该选项可以采用的参数值的最大数量,否则它将采用该选项只有1个参数值

You have to set maximum the number of argument values the option can take, otherwise it assumes the option only has 1 argument value

Options options = new Options();
Option option = new Option("c", "c desc");
// Set option c to take maximum of 10 arguments
option.setArgs(10);
options.addOption(option);

这篇关于Apache Commons CLI:获取选项的值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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