Skip to content

Commit 39c6996

Browse files
committed
Add test branches
1 parent 6279d46 commit 39c6996

4 files changed

Lines changed: 69 additions & 47 deletions

File tree

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

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,39 +53,52 @@ public class Option implements Cloneable, Serializable {
5353
*/
5454
public static final class Builder {
5555

56-
/** The name of the option. */
57-
private String option;
56+
/** The default type. */
57+
private static final Class<String> DEFAULT_TYPE = String.class;
5858

59-
/** Description of the option. */
60-
private String description;
59+
/**
60+
* Returns the input Class or the default type (String) if null.
61+
*
62+
* @param type the candidate Class.
63+
* @return the input Class or the default type (String) if null.
64+
*/
65+
private static Class<?> toType(final Class<?> type) {
66+
return type != null ? type : DEFAULT_TYPE;
67+
}
6168

62-
/** The long representation of the option. */
63-
private String longOption;
69+
/** The number of argument values this option can have. */
70+
private int argCount = UNINITIALIZED;
6471

6572
/** The name of the argument for this option. */
6673
private String argName;
6774

75+
/** The converter to convert to type. **/
76+
private Converter<?, ?> converter;
77+
6878
/** Specifies whether this option is deprecated. */
6979
private DeprecatedAttributes deprecated;
7080

71-
/** Specifies whether this option is required to be present. */
72-
private boolean required;
81+
/** Description of the option. */
82+
private String description;
83+
84+
/** The long representation of the option. */
85+
private String longOption;
86+
87+
/** The name of the option. */
88+
private String option;
7389

7490
/** Specifies whether the argument value of this Option is optional. */
7591
private boolean optionalArg;
7692

77-
/** The number of argument values this option can have. */
78-
private int argCount = UNINITIALIZED;
93+
/** Specifies whether this option is required to be present. */
94+
private boolean required;
7995

8096
/** The type of this Option. */
81-
private Class<?> type = String.class;
97+
private Class<?> type = DEFAULT_TYPE;
8298

8399
/** The character that is the value separator. */
84100
private char valueSeparator;
85101

86-
/** The converter to convert to type. **/
87-
private Converter<?, ?> converter;
88-
89102
/**
90103
* Constructs a new {@code Builder} with the minimum required parameters for an {@code Option} instance.
91104
*
@@ -276,7 +289,7 @@ public Builder required(final boolean required) {
276289
* @return this builder.
277290
*/
278291
public Builder type(final Class<?> type) {
279-
this.type = type;
292+
this.type = toType(type);
280293
return this;
281294
}
282295

@@ -316,18 +329,18 @@ public Builder valueSeparator(final char valueSeparator) {
316329

317330
}
318331

332+
/** Empty array. */
333+
static final Option[] EMPTY_ARRAY = {};
334+
335+
/** The serial version UID. */
336+
private static final long serialVersionUID = 1L;
337+
319338
/** Specifies the number of argument values has not been specified. */
320339
public static final int UNINITIALIZED = -1;
321340

322341
/** Specifies the number of argument values is infinite. */
323342
public static final int UNLIMITED_VALUES = -2;
324343

325-
/** The serial version UID. */
326-
private static final long serialVersionUID = 1L;
327-
328-
/** Empty array. */
329-
static final Option[] EMPTY_ARRAY = {};
330-
331344
/**
332345
* Returns a {@link Builder} to create an {@link Option} using descriptive methods.
333346
*
@@ -350,17 +363,14 @@ public static Builder builder(final String option) {
350363
return new Builder(option);
351364
}
352365

353-
/** The name of the option. */
354-
private final String option;
355-
356-
/** The long representation of the option. */
357-
private String longOption;
366+
/** The number of argument values this option can have. */
367+
private int argCount = UNINITIALIZED;
358368

359369
/** The name of the argument for this option. */
360370
private String argName;
361371

362-
/** Description of the option. */
363-
private String description;
372+
/** The explicit converter for this option. May be null. */
373+
private transient Converter<?, ?> converter;
364374

365375
/**
366376
* Specifies whether this option is deprecated, may be null.
@@ -370,14 +380,20 @@ public static Builder builder(final String option) {
370380
*/
371381
private final transient DeprecatedAttributes deprecated;
372382

373-
/** Specifies whether this option is required to be present. */
374-
private boolean required;
383+
/** Description of the option. */
384+
private String description;
385+
386+
/** The long representation of the option. */
387+
private String longOption;
388+
389+
/** The name of the option. */
390+
private final String option;
375391

376392
/** Specifies whether the argument value of this Option is optional. */
377393
private boolean optionalArg;
378394

379-
/** The number of argument values this option can have. */
380-
private int argCount = UNINITIALIZED;
395+
/** Specifies whether this option is required to be present. */
396+
private boolean required;
381397

382398
/** The type of this Option. */
383399
private Class<?> type = String.class;
@@ -388,9 +404,6 @@ public static Builder builder(final String option) {
388404
/** The character that is the value separator. */
389405
private char valuesep;
390406

391-
/** The explicit converter for this option. May be null. */
392-
private transient Converter<?, ?> converter;
393-
394407
/**
395408
* Private constructor used by the nested Builder class.
396409
*
@@ -932,7 +945,7 @@ public void setRequired(final boolean required) {
932945
* @since 1.3
933946
*/
934947
public void setType(final Class<?> type) {
935-
this.type = type;
948+
this.type = Builder.toType(type);
936949
}
937950

938951
/**
@@ -997,11 +1010,13 @@ public String toString() {
9971010
} else if (hasArg()) {
9981011
buf.append(" [ARG]");
9991012
}
1000-
buf.append(" :: ").append(description);
1001-
if (type != null) {
1002-
buf.append(" :: ").append(type);
1003-
}
1004-
buf.append(" ]");
1005-
return buf.toString();
1013+
// @formatter:off
1014+
return buf.append(" :: ")
1015+
.append(description)
1016+
.append(" :: ")
1017+
.append(type)
1018+
.append(" ]")
1019+
.toString();
1020+
// @formatter:on
10061021
}
10071022
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,14 @@ public static Options parsePattern(final String pattern) {
184184
// details about it
185185
if (!isValueCode(ch)) {
186186
if (opt != Char.SP) {
187-
final Option option = Option.builder(String.valueOf(opt)).hasArg(type != null).required(required).type(type)
188-
.converter(converter).build();
189-
187+
// @formatter:off
188+
final Option option = Option.builder(String.valueOf(opt))
189+
.hasArg(type != null)
190+
.required(required)
191+
.type(type)
192+
.converter(converter)
193+
.build();
194+
// @formatter:on
190195
// we have a previous one to deal with
191196
options.addOption(option);
192197
required = false;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ public void testBuilderMethods() {
175175
String.class, null, null, null);
176176
checkOption(Option.builder("a").desc("desc").type(Integer.class).build(), "a", "desc", null, Option.UNINITIALIZED, null, false, false, defaultSeparator,
177177
Integer.class, null, null, null);
178+
checkOption(Option.builder("a").desc("desc").type(null).build(), "a", "desc", null, Option.UNINITIALIZED, null, false, false, defaultSeparator,
179+
String.class, null, null, null);
178180
checkOption(Option.builder().option("a").desc("desc").type(Integer.class).build(), "a", "desc", null, Option.UNINITIALIZED, null, false, false,
179181
defaultSeparator, Integer.class, null, null, null);
180182
// Deprecated
@@ -331,7 +333,7 @@ public void testTypeObject() {
331333
final Option option = new Option("f", null);
332334
assertEquals(String.class, option.getType());
333335
@SuppressWarnings("cast")
334-
final Object type = (Object) CharSequence.class; // Do NOT remove cast
336+
final Object type = CharSequence.class; // Do NOT remove cast
335337
option.setType(type);
336338
assertEquals(CharSequence.class, option.getType());
337339
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3838
@SuppressWarnings("deprecation") // tests some deprecated classes
3939
public class OptionsTest {
4040

41-
private void assertToStrings(Option option) {
41+
private void assertToStrings(final Option option) {
4242
// Should never throw.
4343
// Should return a String, not null.
4444
assertNotNull(option.toString());

0 commit comments

Comments
 (0)