commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1447094 - in /commons/proper/cli/trunk/src: main/java/org/apache/commons/cli/ test/java/org/apache/commons/cli/
Date Sun, 17 Feb 2013 22:32:22 GMT
Author: tn
Date: Sun Feb 17 22:32:21 2013
New Revision: 1447094

URL: http://svn.apache.org/r1447094
Log:
Add more descriptive methods to Option.Builder, adapt unit tests.

Modified:
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Option.java
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionBuilder.java
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionBuilderTest.java
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionTest.java

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Option.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Option.java?rev=1447094&r1=1447093&r2=1447094&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Option.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Option.java Sun Feb 17 22:32:21
2013
@@ -895,6 +895,16 @@ public class Option implements Cloneable
         }
         
         /**
+         * Marks this Option as required.
+         *
+         * @return this builder, to allow method chaining
+         */
+        public Builder required()
+        {
+            return required(true);
+        }
+
+        /**
          * Sets whether the Option is mandatory.
          *
          * @param required specifies whether the Option is mandatory
@@ -917,10 +927,31 @@ public class Option implements Cloneable
             this.type = type;
             return this;
         }
-        
+
         /**
-         * Sets the value separator. For example if the argument value
-         * was a Java property, the value separator would be '='.
+         * The Option will use '=' as a means to separate argument value.
+         *
+         * @return this builder, to allow method chaining
+         */
+        public Builder valueSeparator()
+        {
+            return valueSeparator('=');
+        }
+
+        /**
+         * The Option will use <code>sep</code> as a means to
+         * separate argument values.
+         * <p>
+         * <b>Example:</b>
+         * <pre>
+         * Option opt = Option.builder("D").valueSeparator('=')
+         *                                 .build();
+         *
+         * String args = "-Dkey=value";
+         * CommandLine line = parser.parse(args);
+         * String propertyName = opt.getValue(0);  // will be "key"
+         * String propertyValue = opt.getValue(1); // will be "value"
+         * </pre>
          *
          * @param sep The value separator.
          * @return this builder, to allow method chaining

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionBuilder.java?rev=1447094&r1=1447093&r2=1447094&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionBuilder.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionBuilder.java Sun Feb
17 22:32:21 2013
@@ -155,15 +155,16 @@ public final class OptionBuilder
     /**
      * The next Option created uses <code>sep</code> as a means to
      * separate argument values.
-     *
+     * <p>
      * <b>Example:</b>
      * <pre>
-     * Option opt = OptionBuilder.withValueSeparator(':')
+     * Option opt = OptionBuilder.withValueSeparator('=')
      *                           .create('D');
      *
+     * String args = "-Dkey=value";
      * CommandLine line = parser.parse(args);
-     * String propertyName = opt.getValue(0);
-     * String propertyValue = opt.getValue(1);
+     * String propertyName = opt.getValue(0);  // will be "key"
+     * String propertyValue = opt.getValue(1); // will be "value"
      * </pre>
      *
      * @param sep The value separator to be used for the argument values.

Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java?rev=1447094&r1=1447093&r2=1447094&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java Sun
Feb 17 22:32:21 2013
@@ -322,9 +322,9 @@ public class HelpFormatterTest
     public void testPrintOptionGroupUsage()
     {
         OptionGroup group = new OptionGroup();
-        group.addOption(OptionBuilder.create("a"));
-        group.addOption(OptionBuilder.create("b"));
-        group.addOption(OptionBuilder.create("c"));
+        group.addOption(Option.builder("a").build());
+        group.addOption(Option.builder("b").build());
+        group.addOption(Option.builder("c").build());
 
         Options options = new Options();
         options.addOptionGroup(group);
@@ -341,9 +341,9 @@ public class HelpFormatterTest
     public void testPrintRequiredOptionGroupUsage()
     {
         OptionGroup group = new OptionGroup();
-        group.addOption(OptionBuilder.create("a"));
-        group.addOption(OptionBuilder.create("b"));
-        group.addOption(OptionBuilder.create("c"));
+        group.addOption(Option.builder("a").build());
+        group.addOption(Option.builder("b").build());
+        group.addOption(Option.builder("c").build());
         group.setRequired(true);
 
         Options options = new Options();
@@ -378,7 +378,7 @@ public class HelpFormatterTest
     @Test
     public void testDefaultArgName()
     {
-        Option option = OptionBuilder.hasArg().isRequired().create("f");
+        Option option = Option.builder("f").hasArg().required(true).build();
         
         Options options = new Options();
         options.addOption(option);
@@ -501,35 +501,36 @@ public class HelpFormatterTest
         Option newRun = new Option("n", "new", false, "Create NLT cache entries only for
new items");
         Option trackerRun = new Option("t", "tracker", false, "Create NLT cache entries only
for tracker items");
         
-        Option timeLimit = OptionBuilder.withLongOpt("limit")
-                                        .hasArg()
-                                        .withValueSeparator()
-                                        .withDescription("Set time limit for execution, in
mintues")
-                                        .create("l");
-        
-        Option age = OptionBuilder.withLongOpt("age")
-                                        .hasArg()
-                                        .withValueSeparator()
-                                        .withDescription("Age (in days) of cache item before
being recomputed")
-                                        .create("a");
-        
-        Option server = OptionBuilder.withLongOpt("server")
-                                        .hasArg()
-                                        .withValueSeparator()
-                                        .withDescription("The NLT server address")
-                                        .create("s");
-        
-        Option numResults = OptionBuilder.withLongOpt("results")
-                                        .hasArg()
-                                        .withValueSeparator()
-                                        .withDescription("Number of results per item")
-                                        .create("r");
+        Option timeLimit = Option.builder("l")
+                                 .longOpt("limit")
+                                 .hasArg()
+                                 .valueSeparator()
+                                 .desc("Set time limit for execution, in mintues")
+                                 .build();
         
-        Option configFile = OptionBuilder.withLongOpt("config")
+        Option age = Option.builder("a").longOpt("age")
                                         .hasArg()
-                                        .withValueSeparator()
-                                        .withDescription("Use the specified configuration
file")
-                                        .create();
+                                        .valueSeparator()
+                                        .desc("Age (in days) of cache item before being recomputed")
+                                        .build();
+        
+        Option server = Option.builder("s").longOpt("server")
+                                           .hasArg()
+                                           .valueSeparator()
+                                           .desc("The NLT server address")
+                                           .build();
+        
+        Option numResults = Option.builder("r").longOpt("results")
+                                               .hasArg()
+                                               .valueSeparator()
+                                               .desc("Number of results per item")
+                                               .build();
+        
+        Option configFile = Option.builder().longOpt("config")
+                                            .hasArg()
+                                            .valueSeparator()
+                                            .desc("Use the specified configuration file")
+                                            .build();
         
         Options mOptions = new Options();
         mOptions.addOption(help);
@@ -568,8 +569,8 @@ public class HelpFormatterTest
     {
         Options options = new Options();
         options.addOption( "f", true, "the file" );
-        options.addOption(OptionBuilder.withLongOpt("size").withDescription("the size").hasArg().withArgName("SIZE").create('s'));
-        options.addOption(OptionBuilder.withLongOpt("age").withDescription("the age").hasArg().create());
+        options.addOption(Option.builder("s").longOpt("size").desc("the size").hasArg().argName("SIZE").build());
+        options.addOption(Option.builder().longOpt("age").desc("the age").hasArg().build());
         
         HelpFormatter formatter = new HelpFormatter();
         assertEquals(HelpFormatter.DEFAULT_LONG_OPT_SEPARATOR, formatter.getLongOptSeparator());
@@ -595,8 +596,8 @@ public class HelpFormatterTest
     {
         Options options = new Options();
         options.addOption( "f", true, "the file" );
-        options.addOption(OptionBuilder.withLongOpt("size").withDescription("the size").hasArg().withArgName("SIZE").create('s'));
-        options.addOption(OptionBuilder.withLongOpt("age").withDescription("the age").hasArg().create());
+        options.addOption(Option.builder("s").longOpt("size").desc("the size").hasArg().argName("SIZE").build());
+        options.addOption(Option.builder().longOpt("age").desc("the age").hasArg().build());
         
         HelpFormatter formatter = new HelpFormatter();
         formatter.setLongOptSeparator("=");

Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionBuilderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionBuilderTest.java?rev=1447094&r1=1447093&r2=1447094&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionBuilderTest.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionBuilderTest.java Sun
Feb 17 22:32:21 2013
@@ -24,6 +24,7 @@ import static org.junit.Assert.fail;
 
 import org.junit.Test;
 
+@SuppressWarnings("deprecation") // OptionBuilder is marked deprecated
 public class OptionBuilderTest
 {
     @Test

Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionTest.java?rev=1447094&r1=1447093&r2=1447094&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionTest.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionTest.java Sun Feb
17 22:32:21 2013
@@ -28,6 +28,8 @@ public class OptionTest
 {
     private static class TestOption extends Option
     {
+        private static final long serialVersionUID = 1L;
+
         public TestOption(String opt, boolean hasArg, String description) throws IllegalArgumentException
         {
             super(opt, hasArg, description);
@@ -72,6 +74,8 @@ public class OptionTest
 
     private static class DefaultOption extends Option
     {
+        private static final long serialVersionUID = 1L;
+
         private final String defaultValue;
 
         public DefaultOption(String opt, String description, String defaultValue) throws
IllegalArgumentException



Mime
View raw message