Skip to content

Commit 62a3b36

Browse files
committed
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 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1091485 13f79535-47bb-0310-9956-ffa450edef68
1 parent 40c4892 commit 62a3b36

6 files changed

Lines changed: 28 additions & 18 deletions

File tree

src/main/java/org/apache/commons/cli/CommandLine.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,14 @@ public Object getOptionObject(String opt)
113113
public Object getParsedOptionValue(String opt) throws ParseException
114114
{
115115
String res = getOptionValue(opt);
116-
117116
Option option = resolveOption(opt);
118-
if (option == null)
117+
118+
if (option == null || res == null)
119119
{
120120
return null;
121121
}
122-
123-
Object type = option.getType();
124-
125-
return (res == null) ? null : TypeHandler.createValue(res, type);
122+
123+
return TypeHandler.createValue(res, option.getType());
126124
}
127125

128126
/**

src/main/java/org/apache/commons/cli/Option.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class Option implements Cloneable, Serializable
6969
private int numberOfArgs = UNINITIALIZED;
7070

7171
/** the type of this Option */
72-
private Object type;
72+
private Class type = String.class;
7373

7474
/** the list of argument values **/
7575
private List values = new ArrayList();
@@ -193,7 +193,7 @@ public Object getType()
193193
*
194194
* @param type the type of this Option
195195
*/
196-
public void setType(Object type)
196+
public void setType(Class type)
197197
{
198198
this.type = type;
199199
}

src/main/java/org/apache/commons/cli/OptionBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public final class OptionBuilder
4848
private static int numberOfArgs = Option.UNINITIALIZED;
4949

5050
/** option type */
51-
private static Object type;
51+
private static Class type;
5252

5353
/** option can have an optional argument value */
5454
private static boolean optionalArg;
@@ -81,7 +81,7 @@ private static void reset()
8181
description = null;
8282
argName = null;
8383
longopt = null;
84-
type = null;
84+
type = String.class;
8585
required = false;
8686
numberOfArgs = Option.UNINITIALIZED;
8787
optionalArg = false;
@@ -287,7 +287,7 @@ public static OptionBuilder hasOptionalArgs(int numArgs)
287287
* @param newType the type of the Options argument value
288288
* @return the OptionBuilder instance
289289
*/
290-
public static OptionBuilder withType(Object newType)
290+
public static OptionBuilder withType(Class newType)
291291
{
292292
OptionBuilder.type = newType;
293293

src/main/java/org/apache/commons/cli/PatternOptionBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public class PatternOptionBuilder
9191
* @param ch the specified character
9292
* @return The class that <code>ch</code> represents
9393
*/
94-
public static Object getValueClass(char ch)
94+
public static Class getValueClass(char ch)
9595
{
9696
switch (ch)
9797
{
@@ -149,7 +149,7 @@ public static Options parsePattern(String pattern)
149149
{
150150
char opt = ' ';
151151
boolean required = false;
152-
Object type = null;
152+
Class type = null;
153153

154154
Options options = new Options();
155155

src/test/java/org/apache/commons/cli/CommandLineTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,16 @@ public void testGetOptions()
6161

6262
assertEquals(3, cmd.getOptions().length);
6363
}
64+
65+
public void testGetParsedOptionValue() throws Exception {
66+
Options options = new Options();
67+
options.addOption(OptionBuilder.hasArg().withType(Number.class).create("i"));
68+
options.addOption(OptionBuilder.hasArg().create("f"));
69+
70+
CommandLineParser parser = new DefaultParser();
71+
CommandLine cmd = parser.parse(options, new String[] { "-i", "123", "-f", "foo" });
72+
73+
assertEquals(123, ((Number) cmd.getParsedOptionValue("i")).intValue());
74+
assertEquals("foo", cmd.getParsedOptionValue("f"));
75+
}
6476
}

src/test/java/org/apache/commons/cli/OptionBuilderTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ public void testCompleteOption( ) {
2626
.hasArg( )
2727
.isRequired( )
2828
.hasArgs( )
29-
.withType( new Float( 10 ) )
29+
.withType( Float.class )
3030
.withDescription( "this is a simple option" )
3131
.create( 's' );
3232

3333
assertEquals( "s", simple.getOpt() );
3434
assertEquals( "simple option", simple.getLongOpt() );
3535
assertEquals( "this is a simple option", simple.getDescription() );
36-
assertEquals( simple.getType().getClass(), Float.class );
36+
assertEquals( simple.getType(), Float.class );
3737
assertTrue( simple.hasArg() );
3838
assertTrue( simple.isRequired() );
3939
assertTrue( simple.hasArgs() );
@@ -44,14 +44,14 @@ public void testTwoCompleteOptions( ) {
4444
.hasArg( )
4545
.isRequired( )
4646
.hasArgs( )
47-
.withType( new Float( 10 ) )
47+
.withType( Float.class )
4848
.withDescription( "this is a simple option" )
4949
.create( 's' );
5050

5151
assertEquals( "s", simple.getOpt() );
5252
assertEquals( "simple option", simple.getLongOpt() );
5353
assertEquals( "this is a simple option", simple.getDescription() );
54-
assertEquals( simple.getType().getClass(), Float.class );
54+
assertEquals( simple.getType(), Float.class );
5555
assertTrue( simple.hasArg() );
5656
assertTrue( simple.isRequired() );
5757
assertTrue( simple.hasArgs() );
@@ -64,7 +64,7 @@ public void testTwoCompleteOptions( ) {
6464
assertEquals( "d", simple.getOpt() );
6565
assertEquals( "dimple option", simple.getLongOpt() );
6666
assertEquals( "this is a dimple option", simple.getDescription() );
67-
assertNull( simple.getType() );
67+
assertEquals( String.class, simple.getType() );
6868
assertTrue( simple.hasArg() );
6969
assertTrue( !simple.isRequired() );
7070
assertTrue( !simple.hasArgs() );

0 commit comments

Comments
 (0)