[jacli commit] r22 - in trunk: samples samples/src/main/java/com/google/jacli/samples/opt src/main/java/com/goog...

1 view
Skip to first unread message

codesite...@google.com

unread,
Jul 2, 2008, 4:24:25 AM7/2/08
to fant...@googlegroups.com
Author: maomaode
Date: Wed Jul 2 01:23:58 2008
New Revision: 22

Modified:
trunk/samples/build.xml
trunk/samples/src/main/java/com/google/jacli/samples/opt/Sample.java
trunk/src/main/java/com/google/jacli/CommandLine.java
trunk/src/main/java/com/google/jacli/CommandLineParser.java
trunk/src/main/java/com/google/jacli/Pattern.java
trunk/src/main/java/com/google/jacli/compilers/AbstractCompiler.java
trunk/src/main/java/com/google/jacli/compilers/OptionCompiler.java
trunk/src/main/java/com/google/jacli/compilers/SequenceCompiler.java
trunk/src/test/java/com/google/jacli/CommandLineParserTest.java

Log:
* CommandLine hold all the exceptions

Modified: trunk/samples/build.xml
==============================================================================
--- trunk/samples/build.xml (original)
+++ trunk/samples/build.xml Wed Jul 2 01:23:58 2008
@@ -15,7 +15,7 @@
<echo message="Run Option Based Sample..."/>
<antcall target="java.run">
<param name="class.name" value="com.google.jacli.samples.opt.Sample"/>
- <!--param name="args" value="-p urn:google=com.google.jacli -p
urn:apache=org.apache -d /~mmao/test/"/-->
+ <param name="args" value="-p urn:google=com.google.jacli -p
urn:apache=org.apache -d /~mmao/test/"/>
</antcall>
</target>


Modified: trunk/samples/src/main/java/com/google/jacli/samples/opt/Sample.java
==============================================================================
---
trunk/samples/src/main/java/com/google/jacli/samples/opt/Sample.java (original)
+++
trunk/samples/src/main/java/com/google/jacli/samples/opt/Sample.java
Wed Jul 2 01:23:58 2008
@@ -1,7 +1,5 @@
package com.google.jacli.samples.opt;

-import java.lang.reflect.Method;
-
import com.google.jacli.CommandLine;
import com.google.jacli.CommandLineParser;

@@ -14,13 +12,5 @@

CommandLine commandLine = CommandLineParser.parse(bean, args, "--", "-");
commandLine.printUsage();
-
- Method[] fields = SampleBean.class.getDeclaredMethods();
-
- //System.out.println(CommandLineParser.getAllDeclaredFields(SampleBean.class));
-
-// for (int i = 0; i < fields.length; i++) {
-// System.out.println(fields[i]);
-// }
}
}

Modified: trunk/src/main/java/com/google/jacli/CommandLine.java
==============================================================================
--- trunk/src/main/java/com/google/jacli/CommandLine.java (original)
+++ trunk/src/main/java/com/google/jacli/CommandLine.java Wed Jul 2
01:23:58 2008
@@ -8,9 +8,15 @@

private List<Field> fields = new ArrayList<Field>();
private Object bean;
- private Pattern pattern = Pattern.SEQ;
+ private Pattern pattern;
private String prefix = CommandLineParser.DEFAULT_PREFIX;
private String shortPrefix = CommandLineParser.DEFAULT_SHORT_PREFIX;
+
+ public CommandLine() {
+ setPattern(Pattern.SEQ);
+ }
+
+ private List<Exception> exceptions = new ArrayList<Exception>();

public final Object getBean() {
return bean;
@@ -59,5 +65,13 @@

public void setShortPrefix(String p) {
this.shortPrefix = p;
+ }
+
+ public List<Exception> getExceptions() {
+ return this.exceptions;
+ }
+
+ public boolean hasException() {
+ return this.exceptions.size() > 0;
}
}

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 Wed Jul
2 01:23:58 2008
@@ -35,7 +35,9 @@
}
try {
if (null != fieldList.get(argument.id())) {
- throw new IllegalArgumentException("Argument
position # " + argument.id() + " already occupied!");
+ command.getExceptions().add(new
IllegalArgumentException("Argument position # "
+
+ argument.id()
+
+ " already occupied!"));
}
} catch (Exception e) {
// ignore

Modified: trunk/src/main/java/com/google/jacli/Pattern.java
==============================================================================
--- trunk/src/main/java/com/google/jacli/Pattern.java (original)
+++ trunk/src/main/java/com/google/jacli/Pattern.java Wed Jul 2
01:23:58 2008
@@ -14,6 +14,7 @@
AbstractCompiler compiler = new SequenceCompiler();
compiler.setPrefix(getPrefix());
compiler.setShortPrefix(getShortPrefix());
+ compiler.setCommand(command);
compiler.compile(fieldList, target, args);
}
},
@@ -22,6 +23,7 @@
AbstractCompiler compiler = new OptionCompiler();
compiler.setPrefix(getPrefix());
compiler.setShortPrefix(getShortPrefix());
+ compiler.setCommand(command);
compiler.compile(fieldList, target, args);
}
};

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
Wed Jul 2 01:23:58 2008
@@ -15,10 +15,12 @@
import java.util.Set;

import com.google.jacli.Argument;
+import com.google.jacli.CommandLine;

public abstract class AbstractCompiler {
private String prefix;
private String shortPrefix;
+ protected CommandLine command;

public abstract void compile(final List<Field> fieldList, final
Object target, final String[] args);

@@ -93,7 +95,7 @@
field.setAccessible(true);
field.set(target, v);
} catch (Exception e) {
- e.printStackTrace();
+ command.getExceptions().add(e);
}
}

@@ -179,5 +181,13 @@

public void setShortPrefix(String p) {
this.shortPrefix = p;
+ }
+
+ public void setCommand(CommandLine c) {
+ this.command = c;
+ }
+
+ public CommandLine getCommand() {
+ return this.command;
}
}

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
Wed Jul 2 01:23:58 2008
@@ -29,7 +29,9 @@
if (argument.defaultValue().length() != 0) {
optionValues.add(argument.defaultValue());
} else {
- throw new IllegalArgumentException("Required
option [" + argument.option() + "] is missing or it contains no value");
+ this.command.getExceptions().add(new
IllegalArgumentException("Required option ["
+
+ argument.option()
+
+ "] is missing or it contains no value"));
}
}


Modified: trunk/src/main/java/com/google/jacli/compilers/SequenceCompiler.java
==============================================================================
---
trunk/src/main/java/com/google/jacli/compilers/SequenceCompiler.java (original)
+++
trunk/src/main/java/com/google/jacli/compilers/SequenceCompiler.java
Wed Jul 2 01:23:58 2008
@@ -17,7 +17,9 @@
setValue(field, target, args[argument.id()]);
} else if (argument.required()) {
if (argument.defaultValue().length() == 0) {
- throw new IllegalArgumentException("The required
argument at position #" + argument.id() + " is missing");
+ this.command.getExceptions().add(new
IllegalArgumentException("The required argument at position #"
+
+ argument.id()
+
+ " is missing"));
}
setValue(field, target, argument.defaultValue());
continue;
@@ -31,7 +33,7 @@
f.add(args[i]);
}
} catch (IllegalAccessException e) {
- e.printStackTrace();
+ this.command.getExceptions().add(e);
}
}
}

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 Wed
Jul 2 01:23:58 2008
@@ -22,11 +22,9 @@

@Test
public void testMissingArgument() {
- try {
- CommandLineParser.parse(test, new String[]{"11", "YAO", "/nba/rockets"});
- } catch (Exception e) {
- assertTrue(e.getMessage().indexOf("missing") != -1);
- }
+ CommandLine command = CommandLineParser.parse(test, new String[]{"11", "YAO", "/nba/rockets"});
+ assertTrue(command.hasException());
+
assertTrue(command.getExceptions().iterator().next().getMessage().indexOf("missing") != -1);
}

@Test

Reply all
Reply to author
Forward
0 new messages