commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r1091485 - in /commons/proper/cli/trunk/src: main/java/org/apache/commons/cli/ test/java/org/apache/commons/cli/
Date Tue, 12 Apr 2011 15:55:04 GMT
Author: ebourg
Date: Tue Apr 12 15:55:03 2011
New Revision: 1091485

URL: http://svn.apache.org/viewvc?rev=1091485&view=rev
Log:
Option type is now set to String by default (Fixes CLI-215)
The type is now explicitly a Class instance instead of an unspecified Object

Modified:
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Option.java
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionBuilder.java
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionBuilderTest.java

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java?rev=1091485&r1=1091484&r2=1091485&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java Tue Apr
12 15:55:03 2011
@@ -113,16 +113,14 @@ public class CommandLine implements Seri
     public Object getParsedOptionValue(String opt) throws ParseException
     {
         String res = getOptionValue(opt);
-
         Option option = resolveOption(opt);
-        if (option == null)
+        
+        if (option == null || res == null)
         {
             return null;
         }
-
-        Object type = option.getType();
-
-        return (res == null) ? null : TypeHandler.createValue(res, type);
+        
+        return TypeHandler.createValue(res, option.getType());
     }
 
     /**

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Option.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Option.java?rev=1091485&r1=1091484&r2=1091485&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Option.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Option.java Tue Apr 12 15:55:03
2011
@@ -69,7 +69,7 @@ public class Option implements Cloneable
     private int numberOfArgs = UNINITIALIZED;
 
     /** the type of this Option */
-    private Object type;
+    private Class type = String.class;
 
     /** the list of argument values **/
     private List values = new ArrayList();
@@ -193,7 +193,7 @@ public class Option implements Cloneable
      *
      * @param type the type of this Option
      */
-    public void setType(Object type)
+    public void setType(Class type)
     {
         this.type = type;
     }

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionBuilder.java?rev=1091485&r1=1091484&r2=1091485&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionBuilder.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionBuilder.java Tue Apr
12 15:55:03 2011
@@ -48,7 +48,7 @@ public final class OptionBuilder
     private static int numberOfArgs = Option.UNINITIALIZED;
 
     /** option type */
-    private static Object type;
+    private static Class type;
 
     /** option can have an optional argument value */
     private static boolean optionalArg;
@@ -81,7 +81,7 @@ public final class OptionBuilder
         description = null;
         argName = null;
         longopt = null;
-        type = null;
+        type = String.class;
         required = false;
         numberOfArgs = Option.UNINITIALIZED;
         optionalArg = false;
@@ -287,7 +287,7 @@ public final class OptionBuilder
      * @param newType the type of the Options argument value
      * @return the OptionBuilder instance
      */
-    public static OptionBuilder withType(Object newType)
+    public static OptionBuilder withType(Class newType)
     {
         OptionBuilder.type = newType;
 

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java?rev=1091485&r1=1091484&r2=1091485&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java
(original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java
Tue Apr 12 15:55:03 2011
@@ -91,7 +91,7 @@ public class PatternOptionBuilder
      * @param ch the specified character
      * @return The class that <code>ch</code> represents
      */
-    public static Object getValueClass(char ch)
+    public static Class getValueClass(char ch)
     {
         switch (ch)
         {
@@ -149,7 +149,7 @@ public class PatternOptionBuilder
     {
         char opt = ' ';
         boolean required = false;
-        Object type = null;
+        Class type = null;
 
         Options options = new Options();
 

Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java?rev=1091485&r1=1091484&r2=1091485&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java Tue
Apr 12 15:55:03 2011
@@ -61,4 +61,16 @@ public class CommandLineTest extends Tes
         
         assertEquals(3, cmd.getOptions().length);
     }
+
+    public void testGetParsedOptionValue() throws Exception {
+        Options options = new Options();
+        options.addOption(OptionBuilder.hasArg().withType(Number.class).create("i"));
+        options.addOption(OptionBuilder.hasArg().create("f"));
+        
+        CommandLineParser parser = new DefaultParser();
+        CommandLine cmd = parser.parse(options, new String[] { "-i", "123", "-f", "foo" });
+        
+        assertEquals(123, ((Number) cmd.getParsedOptionValue("i")).intValue());
+        assertEquals("foo", cmd.getParsedOptionValue("f"));
+    }
 }

Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionBuilderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionBuilderTest.java?rev=1091485&r1=1091484&r2=1091485&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionBuilderTest.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionBuilderTest.java Tue
Apr 12 15:55:03 2011
@@ -26,14 +26,14 @@ public class OptionBuilderTest extends T
                                      .hasArg( )
                                      .isRequired( )
                                      .hasArgs( )
-                                     .withType( new Float( 10 ) )
+                                     .withType( Float.class )
                                      .withDescription( "this is a simple option" )
                                      .create( 's' );
 
         assertEquals( "s", simple.getOpt() );
         assertEquals( "simple option", simple.getLongOpt() );
         assertEquals( "this is a simple option", simple.getDescription() );
-        assertEquals( simple.getType().getClass(), Float.class );
+        assertEquals( simple.getType(), Float.class );
         assertTrue( simple.hasArg() );
         assertTrue( simple.isRequired() );
         assertTrue( simple.hasArgs() );
@@ -44,14 +44,14 @@ public class OptionBuilderTest extends T
                                      .hasArg( )
                                      .isRequired( )
                                      .hasArgs( )
-                                     .withType( new Float( 10 ) )
+                                     .withType( Float.class )
                                      .withDescription( "this is a simple option" )
                                      .create( 's' );
 
         assertEquals( "s", simple.getOpt() );
         assertEquals( "simple option", simple.getLongOpt() );
         assertEquals( "this is a simple option", simple.getDescription() );
-        assertEquals( simple.getType().getClass(), Float.class );
+        assertEquals( simple.getType(), Float.class );
         assertTrue( simple.hasArg() );
         assertTrue( simple.isRequired() );
         assertTrue( simple.hasArgs() );
@@ -64,7 +64,7 @@ public class OptionBuilderTest extends T
         assertEquals( "d", simple.getOpt() );
         assertEquals( "dimple option", simple.getLongOpt() );
         assertEquals( "this is a dimple option", simple.getDescription() );
-        assertNull( simple.getType() );
+        assertEquals( String.class, simple.getType() );
         assertTrue( simple.hasArg() );
         assertTrue( !simple.isRequired() );
         assertTrue( !simple.hasArgs() );



Mime
View raw message