commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r956303 - in /commons/proper/cli/trunk/src: main/java/org/apache/commons/cli/ test/java/org/apache/commons/cli/
Date Sat, 19 Jun 2010 22:06:00 GMT
Author: ebourg
Date: Sat Jun 19 22:06:00 2010
New Revision: 956303

URL: http://svn.apache.org/viewvc?rev=956303&view=rev
Log:
The default argument name displayed is now properly controlled by the help formatter (CLI-205)

Modified:
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java
    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

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java?rev=956303&r1=956302&r2=956303&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java Sat Jun
19 22:06:00 2010
@@ -648,14 +648,14 @@ public class HelpFormatter
         {
             buff.append("--").append(option.getLongOpt());
         }
-
-        // if the Option has a value
-        if (option.hasArg() && option.hasArgName())
+        
+        // if the Option has a value and a non blank argname
+        if (option.hasArg() && (option.getArgName() == null || option.getArgName().length()
!= 0))
         {
             buff.append(option.getOpt() == null ? longOptSeparator : " ");
-            buff.append("<").append(option.getArgName()).append(">");
+            buff.append("<").append(option.getArgName() != null ? option.getArgName()
: getArgName()).append(">");
         }
-
+        
         // if the Option is not a required option
         if (!required)
         {
@@ -781,14 +781,16 @@ public class HelpFormatter
 
             if (option.hasArg())
             {
-                if (option.hasArgName())
+                String argName = option.getArgName();
+                if (argName != null && argName.length() == 0)
                 {
-                    optBuf.append(option.hasLongOpt() ? longOptSeparator : " ");
-                    optBuf.append("<").append(option.getArgName()).append(">");
+                    // if the option has a blank argname
+                    optBuf.append(' ');
                 }
                 else
                 {
-                    optBuf.append(' ');
+                    optBuf.append(option.hasLongOpt() ? longOptSeparator : " ");
+                    optBuf.append("<").append(argName != null ? option.getArgName() :
getArgName()).append(">");
                 }
             }
 

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=956303&r1=956302&r2=956303&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 Sat Jun 19 22:06:00
2010
@@ -54,7 +54,7 @@ public class Option implements Cloneable
     private String longOpt;
 
     /** the name of the argument for this option */
-    private String argName = "arg";
+    private String argName;
 
     /** description of the option */
     private String description;
@@ -319,11 +319,9 @@ public class Option implements Cloneable
     }
 
     /**
-     * Returns whether the display name for the argument value
-     * has been set.
+     * Returns whether the display name for the argument value has been set.
      *
-     * @return if the display name for the argument value has been
-     * set.
+     * @return if the display name for the argument value has been set.
      */
     public boolean hasArgName()
     {

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=956303&r1=956302&r2=956303&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 Sat Jun
19 22:06:00 2010
@@ -77,7 +77,7 @@ public final class OptionBuilder
     private static void reset()
     {
         description = null;
-        argName = "arg";
+        argName = null;
         longopt = null;
         type = null;
         required = false;

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=956303&r1=956302&r2=956303&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 Sat
Jun 19 22:06:00 2010
@@ -305,6 +305,22 @@ public class HelpFormatterTest extends T
         assertEquals("usage: app -f" + EOL, out.toString());
     }
 
+    public void testDefaultArgName()
+    {
+        Option option = OptionBuilder.hasArg().isRequired().create("f");
+        
+        Options options = new Options();
+        options.addOption(option);
+        
+        StringWriter out = new StringWriter();
+
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.setArgName("argument");
+        formatter.printUsage(new PrintWriter(out), 80, "app", options);
+
+        assertEquals("usage: app -f <argument>" + EOL, out.toString());
+    }
+
     public void testRtrim()
     {
         HelpFormatter formatter = new HelpFormatter();



Mime
View raw message