'Java apache commons CLI, repetitive options - need the last entered arguments
I'm just starting out with apache commons CLI in Java and could have CLI options which are repeated. Does apache commons automatically process?
Ex. (program -w arg 1 arg2 -w arg1 arg2).
I need the last pair of args assigned to option -w. Does apache commons automatically overwrite the args for repeated options?
Solution 1:[1]
Apache common takes in the first option/argument unless looped over. Just use two for loops, compare element at 'j' with element at 'i', return the element at 'j' and and element at 'j+1'
String[] options = {"-a","123","-b","456","-c","789","-a", "000"};
List<String> res = new ArrayList<>();
for(int i = 0; i<options.length; i++){
for(int j = i+1; j<options.length; j++) {
if(options[i].equals(options[j])){
res.add(options[j]);
res.add(options[j+1]);
}
}
}
for(String a: res)
System.out.println(a);
The above code prints
"-a" and "000"
which is your repeated option w/argument
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ribash sharma |
