This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new 543bf4a5 Move validation to constructor
543bf4a5 is described below

commit 543bf4a5dd29b32438489e72ba9f1fd142fbb36f
Author: Gary D. Gregory <[email protected]>
AuthorDate: Tue Jul 29 13:31:38 2025 -0400

    Move validation to constructor
---
 src/main/java/org/apache/commons/cli/Option.java | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/commons/cli/Option.java 
b/src/main/java/org/apache/commons/cli/Option.java
index aac02d78..2b6964dd 100644
--- a/src/main/java/org/apache/commons/cli/Option.java
+++ b/src/main/java/org/apache/commons/cli/Option.java
@@ -191,9 +191,6 @@ public class Option implements Cloneable, Serializable {
          * @throws IllegalStateException if neither {@code opt} or {@code 
longOpt} has been set.
          */
         public Option get() {
-            if (option == null && longOption == null) {
-                throw new IllegalStateException("Either opt or longOpt must be 
specified");
-            }
             return new Option(this);
         }
 
@@ -436,8 +433,12 @@ public class Option implements Cloneable, Serializable {
      * Private constructor used by the nested Builder class.
      *
      * @param builder builder used to create this option.
+     * @throws IllegalStateException if neither {@code opt} or {@code longOpt} 
has been set.
      */
     private Option(final Builder builder) {
+        if (builder.option == null && builder.longOption == null) {
+            throw new IllegalStateException("Either opt or longOpt must be 
specified");
+        }
         this.argName = builder.argName;
         this.description = builder.description;
         this.longOption = builder.longOption;

Reply via email to