Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support of all native Java types.

  • Loading branch information...
commit 53cfbb72ee532570c4d5139466344ba09fc36fd7 1 parent a6fb6b2
jhm authored
View
12 .classpath
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="args4j-tools/src"/>
+ <classpathentry kind="src" output="args4j/target/examples" path="args4j/examples"/>
+ <classpathentry kind="src" output="args4j/target/classes" path="args4j/src"/>
+ <classpathentry kind="src" output="args4j/target/test-classes" path="args4j/test"/>
+ <classpathentry kind="lib" path="args4j/lib/ant.jar"/>
+ <classpathentry kind="lib" path="args4j/lib/junit.jar"/>
+ <classpathentry kind="lib" path="C:/jdk/150_14/lib/tools.jar"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/150_14"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
View
17 .project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>args4j</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
View
15 args4j/src/org/kohsuke/args4j/CmdLineParser.java
@@ -21,13 +21,18 @@
import java.util.TreeMap;
import org.kohsuke.args4j.spi.BooleanOptionHandler;
+import org.kohsuke.args4j.spi.ByteOptionHandler;
+import org.kohsuke.args4j.spi.CharOptionHandler;
import org.kohsuke.args4j.spi.DoubleOptionHandler;
import org.kohsuke.args4j.spi.EnumOptionHandler;
import org.kohsuke.args4j.spi.FileOptionHandler;
+import org.kohsuke.args4j.spi.FloatOptionHandler;
import org.kohsuke.args4j.spi.IntOptionHandler;
+import org.kohsuke.args4j.spi.LongOptionHandler;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
+import org.kohsuke.args4j.spi.ShortOptionHandler;
import org.kohsuke.args4j.spi.StringOptionHandler;
@@ -528,6 +533,16 @@ public static void registerHandler( Class valueType, Class<? extends OptionHandl
registerHandler(Double.class, DoubleOptionHandler.class);
registerHandler(double.class,DoubleOptionHandler.class);
registerHandler(String.class,StringOptionHandler.class);
+ registerHandler(Byte.class, ByteOptionHandler.class);
+ registerHandler(byte.class, ByteOptionHandler.class);
+ registerHandler(Character.class, CharOptionHandler.class);
+ registerHandler(char.class, CharOptionHandler.class);
+ registerHandler(Float.class, FloatOptionHandler.class);
+ registerHandler(float.class, FloatOptionHandler.class);
+ registerHandler(Long.class, LongOptionHandler.class);
+ registerHandler(long.class, LongOptionHandler.class);
+ registerHandler(Short.class, ShortOptionHandler.class);
+ registerHandler(short.class, ShortOptionHandler.class);
// enum is a special case
//registerHandler(Map.class,MapOptionHandler.class);
}
View
37 args4j/src/org/kohsuke/args4j/spi/ByteOptionHandler.java
@@ -0,0 +1,37 @@
+package org.kohsuke.args4j.spi;
+
+import org.kohsuke.args4j.CmdLineException;
+import org.kohsuke.args4j.CmdLineParser;
+import org.kohsuke.args4j.OptionDef;
+
+/**
+ * {@link Byte}
+ * {@link OptionHandler}
+ * @author Jan Matèrne
+ * @since 2.0.9
+ */
+public class ByteOptionHandler extends OptionHandler<Byte> {
+
+ public ByteOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Byte> setter) {
+ super(parser, option, setter);
+ }
+
+ @Override
+ public String getDefaultMetaVariable() {
+ return "N";
+ }
+
+ @Override
+ public int parseArguments(Parameters params) throws CmdLineException {
+ String token = params.getParameter(0);
+ try {
+ byte value = Byte.parseByte(token);
+ setter.addValue(value);
+ }
+ catch (NumberFormatException ex) {
+ throw new CmdLineException(Messages.ILLEGAL_OPERAND.format(option.toString(),token));
+ }
+ return 1;
+ }
+
+}
View
37 args4j/src/org/kohsuke/args4j/spi/CharOptionHandler.java
@@ -0,0 +1,37 @@
+package org.kohsuke.args4j.spi;
+
+import org.kohsuke.args4j.CmdLineException;
+import org.kohsuke.args4j.CmdLineParser;
+import org.kohsuke.args4j.OptionDef;
+
+/**
+ * {@link Byte}
+ * {@link OptionHandler}
+ * @author Jan Matèrne
+ * @since 2.0.9
+ */
+public class CharOptionHandler extends OptionHandler<Character> {
+
+ public CharOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Character> setter) {
+ super(parser, option, setter);
+ }
+
+ @Override
+ public String getDefaultMetaVariable() {
+ return "N";
+ }
+
+ @Override
+ public int parseArguments(Parameters params) throws CmdLineException {
+ String token = params.getParameter(0);
+ try {
+ char value = token.charAt(0);
+ setter.addValue(value);
+ }
+ catch (NumberFormatException ex) {
+ throw new CmdLineException(Messages.ILLEGAL_OPERAND.format(option.toString(),token));
+ }
+ return 1;
+ }
+
+}
View
37 args4j/src/org/kohsuke/args4j/spi/FloatOptionHandler.java
@@ -0,0 +1,37 @@
+package org.kohsuke.args4j.spi;
+
+import org.kohsuke.args4j.CmdLineException;
+import org.kohsuke.args4j.CmdLineParser;
+import org.kohsuke.args4j.OptionDef;
+
+/**
+ * {@link Byte}
+ * {@link OptionHandler}
+ * @author Jan Matèrne
+ * @since 2.0.9
+ */
+public class FloatOptionHandler extends OptionHandler<Float> {
+
+ public FloatOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Float> setter) {
+ super(parser, option, setter);
+ }
+
+ @Override
+ public String getDefaultMetaVariable() {
+ return "N";
+ }
+
+ @Override
+ public int parseArguments(Parameters params) throws CmdLineException {
+ String token = params.getParameter(0);
+ try {
+ float value = Float.parseFloat(token);
+ setter.addValue(value);
+ }
+ catch (NumberFormatException ex) {
+ throw new CmdLineException(Messages.ILLEGAL_OPERAND.format(option.toString(),token));
+ }
+ return 1;
+ }
+
+}
View
37 args4j/src/org/kohsuke/args4j/spi/LongOptionHandler.java
@@ -0,0 +1,37 @@
+package org.kohsuke.args4j.spi;
+
+import org.kohsuke.args4j.CmdLineException;
+import org.kohsuke.args4j.CmdLineParser;
+import org.kohsuke.args4j.OptionDef;
+
+/**
+ * {@link Byte}
+ * {@link OptionHandler}
+ * @author Jan Matèrne
+ * @since 2.0.9
+ */
+public class LongOptionHandler extends OptionHandler<Long> {
+
+ public LongOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Long> setter) {
+ super(parser, option, setter);
+ }
+
+ @Override
+ public String getDefaultMetaVariable() {
+ return "N";
+ }
+
+ @Override
+ public int parseArguments(Parameters params) throws CmdLineException {
+ String token = params.getParameter(0);
+ try {
+ long value = Long.parseLong(token);
+ setter.addValue(value);
+ }
+ catch (NumberFormatException ex) {
+ throw new CmdLineException(Messages.ILLEGAL_OPERAND.format(option.toString(),token));
+ }
+ return 1;
+ }
+
+}
View
37 args4j/src/org/kohsuke/args4j/spi/ShortOptionHandler.java
@@ -0,0 +1,37 @@
+package org.kohsuke.args4j.spi;
+
+import org.kohsuke.args4j.CmdLineException;
+import org.kohsuke.args4j.CmdLineParser;
+import org.kohsuke.args4j.OptionDef;
+
+/**
+ * {@link Byte}
+ * {@link OptionHandler}
+ * @author Jan Matèrne
+ * @since 2.0.9
+ */
+public class ShortOptionHandler extends OptionHandler<Short> {
+
+ public ShortOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Short> setter) {
+ super(parser, option, setter);
+ }
+
+ @Override
+ public String getDefaultMetaVariable() {
+ return "N";
+ }
+
+ @Override
+ public int parseArguments(Parameters params) throws CmdLineException {
+ String token = params.getParameter(0);
+ try {
+ short value = Short.parseShort(token);
+ setter.addValue(value);
+ }
+ catch (NumberFormatException ex) {
+ throw new CmdLineException(Messages.ILLEGAL_OPERAND.format(option.toString(),token));
+ }
+ return 1;
+ }
+
+}
View
29 args4j/test/org/kohsuke/args4j/NativeTypes.java
@@ -0,0 +1,29 @@
+package org.kohsuke.args4j;
+
+@SuppressWarnings("unused")
+public class NativeTypes {
+
+ @Option(name="-boolean")
+ public boolean _boolean;
+
+ @Option(name="-byte")
+ public byte _byte;
+
+ @Option(name="-char")
+ public char _char;
+
+ @Option(name="-double")
+ public double _double;
+
+ @Option(name="-float")
+ public float _float;
+
+ @Option(name="-int")
+ public int _int;
+
+ @Option(name="-long")
+ public long _long;
+
+ @Option(name="-short")
+ public short _short;
+}
View
63 args4j/test/org/kohsuke/args4j/NativeTypesTest.java
@@ -0,0 +1,63 @@
+package org.kohsuke.args4j;
+
+public class NativeTypesTest extends Args4JTestBase<NativeTypes> {
+
+ @Override
+ public NativeTypes getTestObject() {
+ return new NativeTypes();
+ }
+
+ public void testBooleanTrue() throws CmdLineException {
+ args = new String[]{"-boolean"};
+ parser.parseArgument(args);
+ assertTrue(testObject._boolean);
+ }
+
+ public void testBooleanFalse() throws CmdLineException {
+ args = new String[]{};
+ parser.parseArgument(args);
+ assertFalse(testObject._boolean);
+ }
+
+ public void testByte() throws CmdLineException {
+ args = new String[]{"-byte", "42"};
+ parser.parseArgument(args);
+ assertEquals(42, testObject._byte);
+ }
+
+ public void testChar() throws CmdLineException {
+ args = new String[]{"-char", "a"};
+ parser.parseArgument(args);
+ assertEquals('a', testObject._char);
+ }
+
+ public void testDouble() throws CmdLineException {
+ args = new String[]{"-double", "42"};
+ parser.parseArgument(args);
+ assertEquals(42, testObject._double, 0);
+ }
+
+ public void testFloat() throws CmdLineException {
+ args = new String[]{"-float", "42"};
+ parser.parseArgument(args);
+ assertEquals(42, testObject._float, 0);
+ }
+
+ public void testInt() throws CmdLineException {
+ args = new String[]{"-int", "42"};
+ parser.parseArgument(args);
+ assertEquals(42, testObject._int);
+ }
+
+ public void testLong() throws CmdLineException {
+ args = new String[]{"-long", "42"};
+ parser.parseArgument(args);
+ assertEquals(42, testObject._long);
+ }
+
+ public void testShort() throws CmdLineException {
+ args = new String[]{"-short", "42"};
+ parser.parseArgument(args);
+ assertEquals(42, testObject._short);
+ }
+}
View
4 xdocs/changes.xml
@@ -3,8 +3,12 @@
<title>Args4J changes</title>
<author email="kk@kohsuke.org">Kohsuke Kawaguchi</author>
<author email="jhm@apache.org">Jan Materne</author>
+ <author email="marksinke@dev.java.net">Mark Sinke</author>
</properties>
<body>
+ <release version="2.0.9" date="2008-??-??" description="Next version after a long time">
+ <action dev="jhm" type="add">Support of all Java native types</action>
+ </release>
<release version="2.0.8" date="2007-05-28" description="Bug-Fixes and Clean-up">
<!--
<action dev="jhm" type="fix" issue="5">Option without "usage" are hidden.</action>
Please sign in to comment.
Something went wrong with that request. Please try again.