Skip to content

Commit ad8466c

Browse files
committed
Switched to ConvertUtilsBean2 for conversions.
Added getParsedOptionValues methods with default values. Fixed som javadoc,
1 parent 7ae9b1c commit ad8466c

7 files changed

Lines changed: 107 additions & 19 deletions

File tree

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

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,9 @@ public String[] getOptionValues(final String opt) {
351351
* @throws ParseException if there are problems turning the option value into the desired type
352352
* @see PatternOptionBuilder
353353
* @since 1.5.0
354+
* @param <T> The return type for the method.
354355
*/
355-
public Object getParsedOptionValue(final char opt) throws ParseException {
356+
public <T> T getParsedOptionValue(final char opt) throws ParseException {
356357
return getParsedOptionValue(String.valueOf(opt));
357358
}
358359

@@ -364,29 +365,77 @@ public Object getParsedOptionValue(final char opt) throws ParseException {
364365
* @throws ParseException if there are problems turning the option value into the desired type
365366
* @see PatternOptionBuilder
366367
* @since 1.5.0
368+
* @param <T> The return type for the method.
367369
*/
368-
public Object getParsedOptionValue(final Option option) throws ParseException {
370+
public <T> T getParsedOptionValue(final Option option) throws ParseException {
371+
return getParsedOptionValue(option, null);
372+
}
373+
374+
/**
375+
* Gets a version of this {@code Option} converted to a particular type.
376+
*
377+
* @param opt the name of the option.
378+
* @return the value parsed into a particular object.
379+
* @throws ParseException if there are problems turning the option value into the desired type
380+
* @see PatternOptionBuilder
381+
* @since 1.2
382+
* @param <T> The return type for the method.
383+
*/
384+
public <T> T getParsedOptionValue(final String opt) throws ParseException {
385+
return getParsedOptionValue(resolveOption(opt));
386+
}
387+
388+
/**
389+
* Gets a version of this {@code Option} converted to a particular type.
390+
*
391+
* @param opt the name of the option.
392+
* @param defaultValue the default value to return if opt is not set.
393+
* @return the value parsed into a particular object.
394+
* @throws ParseException if there are problems turning the option value into the desired type
395+
* @see PatternOptionBuilder
396+
* @since 1.7
397+
* @param <T> The return type for the method.
398+
*/
399+
public <T> T getParsedOptionValue(final char opt, T defaultValue) throws ParseException {
400+
return getParsedOptionValue(String.valueOf(opt), defaultValue);
401+
}
402+
403+
/**
404+
* Gets a version of this {@code Option} converted to a particular type.
405+
*
406+
* @param option the name of the option.
407+
* @param defaultValue the default value to return if opt is not set.
408+
* @return the value parsed into a particular object.
409+
* @throws ParseException if there are problems turning the option value into the desired type
410+
* @see PatternOptionBuilder
411+
* @since 1.7
412+
* @param <T> The return type for the method.
413+
*/
414+
@SuppressWarnings("unchecked")
415+
public <T> T getParsedOptionValue(final Option option, T defaultValue) throws ParseException {
369416
if (option == null) {
370417
return null;
371418
}
372419
final String res = getOptionValue(option);
373420
if (res == null) {
374-
return null;
421+
return defaultValue;
375422
}
376-
return TypeHandler.createValue(res, option.getType());
423+
return (T) TypeHandler.createValue(res, (Class<?>) option.getType());
377424
}
378425

379426
/**
380427
* Gets a version of this {@code Option} converted to a particular type.
381428
*
382429
* @param opt the name of the option.
430+
* @param defaultValue the default value to return if opt is not set.
383431
* @return the value parsed into a particular object.
384432
* @throws ParseException if there are problems turning the option value into the desired type
385433
* @see PatternOptionBuilder
386-
* @since 1.2
434+
* @since 1.7
435+
* @param <T> The return type for the method.
387436
*/
388-
public Object getParsedOptionValue(final String opt) throws ParseException {
389-
return getParsedOptionValue(resolveOption(opt));
437+
public <T> T getParsedOptionValue(final String opt, T defaultValue) throws ParseException {
438+
return getParsedOptionValue(resolveOption(opt), defaultValue);
390439
}
391440

392441
/**

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2020
import java.io.File;
2121
import java.io.FileInputStream;
2222
import java.net.URL;
23-
import java.text.SimpleDateFormat;
2423
import java.util.Date;
2524

2625
import org.apache.commons.beanutils.ConversionException;
27-
import org.apache.commons.beanutils.ConvertUtilsBean;
26+
import org.apache.commons.beanutils.ConvertUtilsBean2;
2827
import org.apache.commons.beanutils.Converter;
2928
import org.apache.commons.beanutils.converters.ConverterFacade;
3029
import org.apache.commons.beanutils.converters.DateConverter;
@@ -37,10 +36,16 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3736
*/
3837
public class TypeHandler {
3938

40-
private static ConvertUtilsBean convertUtils;
39+
/**
40+
* The conversion utilities from BeanUtils.
41+
*/
42+
private static ConvertUtilsBean2 convertUtils;
4143

44+
/**
45+
* Setup the convertUtils object
46+
*/
4247
static {
43-
convertUtils = new ConvertUtilsBean();
48+
convertUtils = new ConvertUtilsBean2();
4449
convertUtils.register(true, true, 0);
4550
/*
4651
* this can not be a ternary because the unboxing operations will result in it
@@ -59,7 +64,7 @@ public Number apply(String str) throws ConversionException {
5964
};
6065
convertUtils.register(new SimpleConverter<>(fn, Number.class), Number.class);
6166

62-
Func<Object> fo = (str) -> {
67+
Func<Object> fo = str -> {
6368
final Class<?> cl;
6469

6570
try {
@@ -238,7 +243,7 @@ public static <T> T createValue(final String str, Class<T> clazz) throws ParseEx
238243
* value of {@code str}.
239244
* @throws ParseException if the value creation for the given object type
240245
* failed
241-
* @deprecated use createValue(str, Class<?>);
246+
* @deprecated use {@link #createValue(String, Class)};
242247
*/
243248
@Deprecated // (since="1.7")
244249
public static Object createValue(final String str, final Object obj) throws ParseException {

src/main/java/org/apache/commons/cli/converters/Func.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1818

1919
/**
2020
* The definition of the functional interface to call when doing a conversion.
21-
* Like Function<String,T> but can throw an Exception.
21+
* Like {@code Function<String,T>} but can throw an Exception.
2222
*
2323
* @param <T> The return type for the function.
2424
*/
2525
@FunctionalInterface
2626
public interface Func<T> {
27+
/**
28+
* Applies the conversion function to the String argument.
29+
* @param str the String to convert
30+
* @return the Object from the conversion.
31+
* @throws Exception on error.
32+
*/
2733
T apply(String str) throws Exception;
28-
}
34+
}

src/main/java/org/apache/commons/cli/converters/SimpleConverter.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2525
* @param <T> The type of object the converter will return.
2626
*/
2727
public class SimpleConverter<T> extends AbstractConverter {
28-
28+
/**
29+
* The Func to convert string values.
30+
*/
2931
private final Func<? extends T> func;
32+
/**
33+
* The class type of the object that is returned.
34+
*/
3035
private final Class<T> type;
3136

37+
/**
38+
* @param func the Func instance to use to convert values.
39+
* @param type the Type that this Converter returns.
40+
*/
3241
public SimpleConverter(Func<? extends T> func, Class<T> type) {
3342
this.func = func;
3443
this.type = type;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* Apache Commons CLI converters to convert from String to Object of various types.
20+
*/
21+
package org.apache.commons.cli.converters;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ public void testSimplePattern() throws Exception {
153153
// expected
154154
}
155155

156-
// DATES NOT SUPPORTED YET
157156
assertEquals("date flag z", new Date(1023400137000L), line.getOptionObject('z'));
158157

159158
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2525
import java.io.File;
2626
import java.io.FileInputStream;
2727
import java.net.URL;
28-
import java.text.DateFormat;
2928
import java.util.Date;
3029

3130
import org.junit.Test;
@@ -62,7 +61,7 @@ public void testBadValueDate() {
6261
public void testCreateValueDate() throws Exception {
6362
Date d = new Date(1023400137000L);
6463
Date d2 = TypeHandler.createValue("Thu Jun 06 17:48:57 EDT 2002", PatternOptionBuilder.DATE_VALUE);
65-
assertEquals( d, d2 );
64+
assertEquals(d, d2);
6665
}
6766

6867

0 commit comments

Comments
 (0)