Skip to content

Commit 543bf4a

Browse files
committed
Move validation to constructor
1 parent a8e61ae commit 543bf4a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ public Builder desc(final String description) {
191191
* @throws IllegalStateException if neither {@code opt} or {@code longOpt} has been set.
192192
*/
193193
public Option get() {
194-
if (option == null && longOption == null) {
195-
throw new IllegalStateException("Either opt or longOpt must be specified");
196-
}
197194
return new Option(this);
198195
}
199196

@@ -436,8 +433,12 @@ public static Builder builder(final String option) {
436433
* Private constructor used by the nested Builder class.
437434
*
438435
* @param builder builder used to create this option.
436+
* @throws IllegalStateException if neither {@code opt} or {@code longOpt} has been set.
439437
*/
440438
private Option(final Builder builder) {
439+
if (builder.option == null && builder.longOption == null) {
440+
throw new IllegalStateException("Either opt or longOpt must be specified");
441+
}
441442
this.argName = builder.argName;
442443
this.description = builder.description;
443444
this.longOption = builder.longOption;

0 commit comments

Comments
 (0)