Apache Commons CLI:获取选项的值列表 [英] Apache Commons CLI : Getting list of values for an option
问题描述
对于 CLI,我需要传入一个整数数组作为特定选项的输入.
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屋!