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 c886434  Added setter for Builder.option (#33)
c886434 is described below

commit c886434a34107af01ae3cf70645e8e7d8aaa9ede
Author: Waldemar Sojka <waldemar.so...@gmail.com>
AuthorDate: Wed Oct 20 14:03:32 2021 +0200

    Added setter for Builder.option (#33)
    
    * added separate setter for Builder.opt
    
    * added setter for option name with validation
    
    * fixed checkstyle error
---
 src/main/java/org/apache/commons/cli/Option.java     | 14 +++++++++++++-
 src/test/java/org/apache/commons/cli/OptionTest.java |  2 ++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/cli/Option.java 
b/src/main/java/org/apache/commons/cli/Option.java
index 4a85907..dfaff0e 100644
--- a/src/main/java/org/apache/commons/cli/Option.java
+++ b/src/main/java/org/apache/commons/cli/Option.java
@@ -51,7 +51,7 @@ public class Option implements Cloneable, Serializable {
     public static final class Builder {
 
         /** The name of the option */
-        private final String option;
+        private String option;
 
         /** description of the option */
         private String description;
@@ -88,6 +88,18 @@ public class Option implements Cloneable, Serializable {
         }
 
         /**
+         * Sets the name of the Option.
+         *
+         * @param opt the name of the Option
+         * @return this builder, to allow method chaining
+         * @throws IllegalArgumentException if there are any non valid Option 
characters in {@code opt}
+         */
+        public Builder opt(String opt) throws IllegalArgumentException {
+            this.option = OptionValidator.validate(opt);
+            return this;
+        }
+
+        /**
          * Sets the display name for the argument value.
          *
          * @param argName the display name for the argument value.
diff --git a/src/test/java/org/apache/commons/cli/OptionTest.java 
b/src/test/java/org/apache/commons/cli/OptionTest.java
index 286aa3a..21859fe 100644
--- a/src/test/java/org/apache/commons/cli/OptionTest.java
+++ b/src/test/java/org/apache/commons/cli/OptionTest.java
@@ -109,6 +109,8 @@ public class OptionTest {
             String.class);
         
checkOption(Option.builder("a").desc("desc").type(Integer.class).build(), "a", 
"desc", null, Option.UNINITIALIZED, null, false, false, defaultSeparator,
             Integer.class);
+        
checkOption(Option.builder().opt("a").desc("desc").type(Integer.class).build(), 
"a", "desc", null, Option.UNINITIALIZED, null, false, false,
+                defaultSeparator, Integer.class);
     }
 
     @Test

Reply via email to