Modified:
trunk/src/main/java/com/google/jacli/compilers/AbstractCompiler.java
trunk/src/main/java/com/google/jacli/compilers/OptionCompiler.java
trunk/src/test/java/com/google/jacli/ListArgumentTest.java
Log:
* Support the option collections
Modified: trunk/src/main/java/com/google/jacli/compilers/AbstractCompiler.java
==============================================================================
---
trunk/src/main/java/com/google/jacli/compilers/AbstractCompiler.java (original)
+++
trunk/src/main/java/com/google/jacli/compilers/AbstractCompiler.java
Thu May 22 23:35:53 2008
@@ -5,7 +5,12 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.LinkedList;
import java.util.List;
+import java.util.Queue;
+import java.util.Set;
import com.google.jacli.Argument;
@@ -16,11 +21,46 @@
return (Argument) field.getAnnotation(Argument.class);
}
+ private boolean isCollection(final Field field) {
+ return field.getType() == List.class
+ || field.getType() == Set.class
+ || field.getType() == Queue.class;
+ }
+
+ private Collection<Object> newCollection(final Field field) {
+ if (field.getType() == Set.class) {
+ return new HashSet<Object>();
+ }
+ if (field.getType() == Queue.class) {
+ return new LinkedList<Object>();
+ }
+ return new ArrayList<Object>();
+ }
+
+ @SuppressWarnings("unchecked")
protected void setValue(final Field field, Object target, String
value) {
- Object v = getValue(field, value);
- if (v == null) {
- return;
+ Object v = null;
+
+ Object o = null;
+ try {
+ o = field.get(target);
+ } catch (IllegalAccessException e) {
+ // no action here
+ }
+ if (isCollection(field)) {
+ v = getValue(getActualType(field.getGenericType()), value);
+
+ Collection<Object> c = (Collection<Object>) o;
+ if (null == c) {
+ c = newCollection(field);
+ }
+ c.add(v);
+ v = c;
+ } else {
+ v = getValue(field.getType(), value);
}
+
+
try {
field.setAccessible(true);
field.set(target, v);
@@ -30,22 +70,6 @@
}
- protected Object getValue(final Field field, final String value) {
- Class clz = field.getType();
-
- if (clz == List.class) {
- List<Object> r = new ArrayList<Object>();
-
- Type genericType = field.getGenericType();
- Class actualType = getActualType(genericType);
-
- r.add(getValue(actualType, value));
- return r;
- } else {
- return getValue(clz, value);
- }
- }
-
protected Class getActualType(Type genericType) {
if (genericType != null) {
Type[] types = ((ParameterizedType)genericType).getActualTypeArguments();
@@ -70,7 +94,7 @@
}
protected boolean hasOption(final Argument argument, final String
arg) {
- return arg.equals("-" + argument.option()) || arg.equals("-" + argument.simpleName());
+ return ("-" + argument.option()).equals(arg) || ("-" + argument.simpleName()).equals(arg);
}
protected boolean hasOption(final Argument argument, final
String[] args) {
@@ -83,16 +107,20 @@
}
- protected String getOptionValue(final Argument argument, String[]
args) {
+ protected List<String> getOptionValue(final Argument argument,
String[] args) {
+ List<String> values = new ArrayList<String>();
+
boolean found = false;
for (String arg : args) {
if (found) {
- return arg;
+ values.add(arg);
}
if (hasOption(argument, arg)) {
found = true;
+ } else {
+ found = false;
}
}
- return null;
+ return values;
}
}
Modified: trunk/src/main/java/com/google/jacli/compilers/OptionCompiler.java
==============================================================================
--- trunk/src/main/java/com/google/jacli/compilers/OptionCompiler.java (original)
+++ trunk/src/main/java/com/google/jacli/compilers/OptionCompiler.java
Thu May 22 23:35:53 2008
@@ -20,18 +20,22 @@
setValue(field, target, "false");
}
} else if (argument.optionType() == String.class &&
argument.option().length() > 0) {
- String optionValue = getOptionValue(argument, args);
- if (null == optionValue) {
+ List<String> optionValues = getOptionValue(argument, args);
+
+ if (optionValues.isEmpty()) {
if (!argument.required()) {
continue;
}
if (argument.defaultValue().length() != 0) {
- optionValue = argument.defaultValue();
+ optionValues.add(argument.defaultValue());
} else {
throw new IllegalArgumentException("Required
option [" + argument.option() + "] is missing or it contains no value");
}
}
- setValue(field, target, optionValue);
+
+ for (String optionValue : optionValues) {
+ setValue(field, target, optionValue);
+ }
} else {
setValue(field, target, args[args.length - 1]);
// this should be the last option in case of
optionBased parse
Modified: trunk/src/test/java/com/google/jacli/ListArgumentTest.java
==============================================================================
--- trunk/src/test/java/com/google/jacli/ListArgumentTest.java (original)
+++ trunk/src/test/java/com/google/jacli/ListArgumentTest.java Thu May
22 23:35:53 2008
@@ -30,6 +30,17 @@
assertTrue(list.get(0) instanceof File);
}
+ @Test
+ public void testParseOptionListFile() {
+ ListOptionFileBean bean = new ListOptionFileBean();
+
+ CommandLine command = CommandLineParser.parse(bean, new String[]{"-p", "/data", "-p", "11"});
+
+ List<File> files = bean.getB();
+ assertEquals(2, files.size());
+ assertTrue(files.get(0) instanceof File);
+ }
+
private class ListStringBean {
@Argument(id = 0)
private int a;
@@ -55,6 +66,15 @@
return this.a;
}
+ public List<File> getB() {
+ return this.b;
+ }
+ }
+
+ private class ListOptionFileBean {
+ @Argument(option = "p")
+ private List<File> b;
+
public List<File> getB() {
return this.b;
}