[jacli commit] r12 - in trunk/src: main/java/com/google/jacli main/java/com/google/jacli/compilers test/java/com...

3 views
Skip to first unread message

codesite...@google.com

unread,
May 29, 2008, 6:51:06 AM5/29/08
to fant...@googlegroups.com
Author: maomaode
Date: Thu May 29 03:49:44 2008
New Revision: 12

Added:
trunk/src/main/java/com/google/jacli/CommonBean.java
trunk/src/main/java/com/google/jacli/Messages.properties
Modified:
trunk/src/main/java/com/google/jacli/Argument.java
trunk/src/main/java/com/google/jacli/CommandLineParser.java
trunk/src/main/java/com/google/jacli/DefaultUsage.java
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/CommandLineParserTest.java
trunk/src/test/java/com/google/jacli/OptionParserTest.java

Log:
* Support Map with the delim which is a regex
* Add the CommonBean for the generic options, such as help/verbose

--____________--

M src/test/java/com/google/jacli/CommandLineParserTest.java
M src/test/java/com/google/jacli/OptionParserTest.java
M src/main/java/com/google/jacli/Argument.java
A src/main/java/com/google/jacli/Messages.properties
M src/main/java/com/google/jacli/CommandLineParser.java
M src/main/java/com/google/jacli/DefaultUsage.java
M src/main/java/com/google/jacli/compilers/OptionCompiler.java
M src/main/java/com/google/jacli/compilers/AbstractCompiler.java
A src/main/java/com/google/jacli/CommonBean.java


Modified: trunk/src/main/java/com/google/jacli/Argument.java
==============================================================================
--- trunk/src/main/java/com/google/jacli/Argument.java (original)
+++ trunk/src/main/java/com/google/jacli/Argument.java Thu May 29
03:49:44 2008
@@ -12,10 +12,12 @@
int id() default 0;

String option() default "";
- Class optionType() default String.class;
- String simpleName() default "";
+
+ String shortName() default "";

String defaultValue() default "";

String documentation() default "";
+
+ String delimiter() default "=";
}

Modified: trunk/src/main/java/com/google/jacli/CommandLineParser.java
==============================================================================
--- trunk/src/main/java/com/google/jacli/CommandLineParser.java (original)
+++ trunk/src/main/java/com/google/jacli/CommandLineParser.java Thu May
29 03:49:44 2008
@@ -25,7 +25,7 @@
if (argument == null) {
continue;
}
- if (argument.option().length() > 0 ||
argument.optionType() == Boolean.class) {
+ if (argument.option().length() > 0) {
command.setPattern(Pattern.OPT);
}
try {

Added: trunk/src/main/java/com/google/jacli/CommonBean.java
==============================================================================
--- (empty file)
+++ trunk/src/main/java/com/google/jacli/CommonBean.java Thu May 29
03:49:44 2008
@@ -0,0 +1,25 @@
+package com.google.jacli;
+
+public class CommonBean {
+ @Argument(shortName = "h", option = "help", documentation = "HELP")
+ private boolean help;
+
+ @Argument(shortName = "v", option = "verbose", documentation = "VERBOSE")
+ private boolean verbose;
+
+ public final boolean isHelp() {
+ return help;
+ }
+
+ public final void setHelp(final boolean newHelp) {
+ this.help = newHelp;
+ }
+
+ public final boolean isVerbose() {
+ return verbose;
+ }
+
+ public final void setVerbose(final boolean newVerbose) {
+ this.verbose = newVerbose;
+ }
+}
\ No newline at end of file

Modified: trunk/src/main/java/com/google/jacli/DefaultUsage.java
==============================================================================
--- trunk/src/main/java/com/google/jacli/DefaultUsage.java (original)
+++ trunk/src/main/java/com/google/jacli/DefaultUsage.java Thu May 29
03:49:44 2008
@@ -26,20 +26,20 @@
buffer.append("[");
}

- if (argument.optionType() == String.class &&
argument.option().length() == 0) {
+ if (argument.option().length() == 0) {
// the last argument
continue;
}

- if (argument.simpleName().length() > 0) {
+ if (argument.shortName().length() > 0) {
buffer.append("-");
- buffer.append(argument.simpleName());
+ buffer.append(argument.shortName());
buffer.append(", ");
}
buffer.append("-" + argument.option());
buffer.append(getDots(argument.option().length()));

- if (argument.optionType() == String.class) {
+ if (field.getType() == String.class) {
if (argument.documentation().length() > 0) {
if (bundle != null) {
try {

Added: trunk/src/main/java/com/google/jacli/Messages.properties
==============================================================================
--- (empty file)
+++ trunk/src/main/java/com/google/jacli/Messages.properties Thu May 29
03:49:44 2008
@@ -0,0 +1,2 @@
+HELP = print the help messages
+VERBOSE = print the verbose messages
\ No newline at end of file

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 29 03:49:44 2008
@@ -6,9 +6,11 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import java.util.Queue;
import java.util.Set;

@@ -27,6 +29,10 @@
|| field.getType() == Queue.class;
}

+ private boolean isMap(final Field field) {
+ return field.getType() == Map.class;
+ }
+
private Collection<Object> newCollection(final Field field) {
if (field.getType() == Set.class) {
return new HashSet<Object>();
@@ -37,6 +43,10 @@
return new ArrayList<Object>();
}

+ private String getDelim(final Field field) {
+ return getArgument(field).delimiter();
+ }
+
@SuppressWarnings("unchecked")
protected void setValue(final Field field, Object target, String
value) {
Object v = null;
@@ -60,6 +70,21 @@
v = getValue(field.getType(), value);
}

+ if (isMap(field)) {
+ Class[] clsz = getActualTypes(field.getGenericType());
+ String delim = getDelim(field);
+ String[] parts = value.split(delim);
+ Object mapKey = getValue(clsz[0], parts[0].trim());
+ Object mapValue = getValue(clsz[1], parts[1].trim());
+
+ Map<Object, Object> c = (Map<Object, Object>) o;
+ if (null == c) {
+ c = new HashMap<Object, Object>();
+ }
+
+ c.put(mapKey, mapValue);
+ v = c;
+ }

try {
field.setAccessible(true);
@@ -80,6 +105,19 @@
return null;
}

+ protected Class[] getActualTypes(Type genericType) {
+ if (genericType != null) {
+ Class[] clzes = new Class[2];
+ Type[] types = ((ParameterizedType)genericType).getActualTypeArguments();
+ if (types != null && types.length == 2) {
+ clzes[0] = (Class) types[0];
+ clzes[1] = (Class) types[1];
+ return clzes;
+ }
+ }
+ return null;
+ }
+
protected Object getValue(final Class clz, final String value) {
if (clz == String.class) {
return value;
@@ -94,7 +132,7 @@
}

protected boolean hasOption(final Argument argument, final String
arg) {
- return ("-" + argument.option()).equals(arg) || ("-" + argument.simpleName()).equals(arg);
+ return ("-" + argument.option()).equals(arg) || ("-" + argument.shortName()).equals(arg);
}

protected boolean hasOption(final Argument argument, final
String[] args) {

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 29 03:49:44 2008
@@ -13,13 +13,13 @@
return;
}
Argument argument = getArgument(field);
- if (argument.optionType() == Boolean.class) {
+ if (field.getType() == Boolean.class || field.getType() ==
boolean.class) {
if (hasOption(argument, args)) {
setValue(field, target, "true");
} else {
setValue(field, target, "false");
}
- } else if (argument.optionType() == String.class &&
argument.option().length() > 0) {
+ } else if (argument.option().length() > 0) {
List<String> optionValues = getOptionValue(argument, args);

if (optionValues.isEmpty()) {

Modified: trunk/src/test/java/com/google/jacli/CommandLineParserTest.java
==============================================================================
--- trunk/src/test/java/com/google/jacli/CommandLineParserTest.java (original)
+++ trunk/src/test/java/com/google/jacli/CommandLineParserTest.java Thu
May 29 03:49:44 2008
@@ -1,6 +1,7 @@
package com.google.jacli;

import java.io.File;
+import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
@@ -28,6 +29,26 @@
}
}

+ @Test
+ public void testMap() {
+ TestMap mapBean = new TestMap();
+
+ try {
+ CommandLineParser.parse(mapBean, new
String[]{"-p", "abc.xyz = txt", "-p", "xyz.abc = txt"});
+
+ Map<String, File> map = mapBean.getM();
+ assertEquals(2, map.size());
+
+ assertTrue(map.get("xyz.abc") instanceof File);
+ assertTrue("txt".equals(map.get("xyz.abc").toString()));
+
+ assertTrue(map.get("abc.xyz") instanceof File);
+ assertTrue("txt".equals(map.get("abc.xyz").toString()));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
private class TestMain {
@Argument(id = 0)
private int a;
@@ -61,6 +82,19 @@

public String getF() {
return this.f;
+ }
+ }
+
+ private class TestMap {
+ @Argument(option = "p")
+ private Map<String, File> m;
+
+ public Map<String, File> getM() {
+ return m;
+ }
+
+ public void setM(Map<String, File> map) {
+ this.m = map;
}
}
}

Modified: trunk/src/test/java/com/google/jacli/OptionParserTest.java
==============================================================================
--- trunk/src/test/java/com/google/jacli/OptionParserTest.java (original)
+++ trunk/src/test/java/com/google/jacli/OptionParserTest.java Thu May
29 03:49:44 2008
@@ -43,7 +43,7 @@
@Argument(option = "basedir", documentation = "base_directory")
private File c;

- @Argument(simpleName = "v", option = "verbose", optionType = Boolean.class)
+ @Argument(shortName = "v", option = "verbose")
private boolean d;

@Argument()

Reply all
Reply to author
Forward
0 new messages