Skip to content

Commit c886434

Browse files
authored
Added setter for Builder.option (apache#33)
* added separate setter for Builder.opt * added setter for option name with validation * fixed checkstyle error
1 parent 0b980a1 commit c886434

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class Option implements Cloneable, Serializable {
5151
public static final class Builder {
5252

5353
/** The name of the option */
54-
private final String option;
54+
private String option;
5555

5656
/** description of the option */
5757
private String description;
@@ -87,6 +87,18 @@ private Builder(final String option) throws IllegalArgumentException {
8787
this.option = OptionValidator.validate(option);
8888
}
8989

90+
/**
91+
* Sets the name of the Option.
92+
*
93+
* @param opt the name of the Option
94+
* @return this builder, to allow method chaining
95+
* @throws IllegalArgumentException if there are any non valid Option characters in {@code opt}
96+
*/
97+
public Builder opt(String opt) throws IllegalArgumentException {
98+
this.option = OptionValidator.validate(opt);
99+
return this;
100+
}
101+
90102
/**
91103
* Sets the display name for the argument value.
92104
*

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public void testBuilderMethods() {
109109
String.class);
110110
checkOption(Option.builder("a").desc("desc").type(Integer.class).build(), "a", "desc", null, Option.UNINITIALIZED, null, false, false, defaultSeparator,
111111
Integer.class);
112+
checkOption(Option.builder().opt("a").desc("desc").type(Integer.class).build(), "a", "desc", null, Option.UNINITIALIZED, null, false, false,
113+
defaultSeparator, Integer.class);
112114
}
113115

114116
@Test

0 commit comments

Comments
 (0)