commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r954899 - in /commons/proper/cli/trunk/src: java/org/apache/commons/cli/HelpFormatter.java test/org/apache/commons/cli/HelpFormatterTest.java
Date Tue, 15 Jun 2010 14:11:05 GMT
Author: ebourg
Date: Tue Jun 15 14:11:04 2010
New Revision: 954899

URL: http://svn.apache.org/viewvc?rev=954899&view=rev
Log:
Added a parameter in HelpFormatter to specify the separator displayed between a long option
and its value (CLI-169)

Modified:
    commons/proper/cli/trunk/src/java/org/apache/commons/cli/HelpFormatter.java
    commons/proper/cli/trunk/src/test/org/apache/commons/cli/HelpFormatterTest.java

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli/HelpFormatter.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli/HelpFormatter.java?rev=954899&r1=954898&r2=954899&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli/HelpFormatter.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli/HelpFormatter.java Tue Jun 15
14:11:04 2010
@@ -24,9 +24,6 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
-import java.util.StringTokenizer;
-import java.io.InputStream;
-import java.io.ByteArrayOutputStream;
 
 /** 
  * A formatter of help messages for the current command line options
@@ -60,6 +57,9 @@ public class HelpFormatter
     /** default prefix for long Option */
     public static final String DEFAULT_LONG_OPT_PREFIX = "--";
 
+    /** default separator displayed between a long Option and its value */
+    public static final String DEFAULT_LONG_OPT_SEPARATOR = " ";
+
     /** default name for an argument */
     public static final String DEFAULT_ARG_NAME = "arg";
 
@@ -122,6 +122,9 @@ public class HelpFormatter
      */
     public String defaultLongOptPrefix = DEFAULT_LONG_OPT_PREFIX;
 
+    /** The separator displayed between the long option and its value. */
+    private String longOptSeparator = DEFAULT_LONG_OPT_SEPARATOR;
+
     /**
      * the name of the argument
      *
@@ -278,6 +281,30 @@ public class HelpFormatter
     }
 
     /**
+     * Set the separator displayed between a long option and its value.
+     * Ensure that the separator specified is supported by the parser used,
+     * typically ' ' or '='.
+     * 
+     * @param longOptSeparator the separator, typically ' ' or '='.
+     * @since 1.3
+     */
+    public void setLongOptSeparator(String longOptSeparator)
+    {
+        this.longOptSeparator = longOptSeparator;
+    }
+
+    /**
+     * Returns the separator displayed between a long option and its value.
+     * 
+     * @return the separator
+     * @since 1.3
+     */
+    public String getLongOptSeparator()
+    {
+        return longOptSeparator;
+    }
+
+    /**
      * Sets the 'argName'.
      *
      * @param name the new value of 'argName'
@@ -500,13 +527,12 @@ public class HelpFormatter
     }
 
     /**
-     * <p>Prints the usage statement for the specified application.</p>
+     * Prints the usage statement for the specified application.
      *
      * @param pw The PrintWriter to print the usage statement 
      * @param width The number of characters to display per line
      * @param app The application name
      * @param options The command line Options
-     *
      */
     public void printUsage(PrintWriter pw, int width, String app, Options options)
     {
@@ -607,7 +633,7 @@ public class HelpFormatter
      * @param option the Option to append
      * @param required whether the Option is required or not
      */
-    private static void appendOption(final StringBuffer buff, final Option option, final
boolean required)
+    private void appendOption(final StringBuffer buff, final Option option, final boolean
required)
     {
         if (!required)
         {
@@ -626,7 +652,8 @@ public class HelpFormatter
         // if the Option has a value
         if (option.hasArg() && option.hasArgName())
         {
-            buff.append(" <").append(option.getArgName()).append(">");
+            buff.append(option.getOpt() == null ? longOptSeparator : " ");
+            buff.append("<").append(option.getArgName()).append(">");
         }
 
         // if the Option is not a required option
@@ -652,8 +679,8 @@ public class HelpFormatter
     }
 
     /**
-     * <p>Print the help for the specified Options to the specified writer, 
-     * using the specified width, left padding and description padding.</p>
+     * Print the help for the specified Options to the specified writer, 
+     * using the specified width, left padding and description padding.
      *
      * @param pw The printWriter to write the help to
      * @param width The number of characters to display per line
@@ -756,7 +783,8 @@ public class HelpFormatter
             {
                 if (option.hasArgName())
                 {
-                    optBuf.append(" <").append(option.getArgName()).append(">");
+                    optBuf.append(option.hasLongOpt() ? longOptSeparator : " ");
+                    optBuf.append("<").append(option.getArgName()).append(">");
                 }
                 else
                 {

Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli/HelpFormatterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli/HelpFormatterTest.java?rev=954899&r1=954898&r2=954899&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli/HelpFormatterTest.java (original)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli/HelpFormatterTest.java Tue Jun
15 14:11:04 2010
@@ -451,4 +451,46 @@ public class HelpFormatterTest extends T
                 ,out.toString());
     }
     
+    public void testHelpWithLongOptSeparator() throws Exception
+    {
+        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());
+        
+        HelpFormatter formatter = new HelpFormatter();
+        assertEquals(HelpFormatter.DEFAULT_LONG_OPT_SEPARATOR, formatter.getLongOptSeparator());
+        formatter.setLongOptSeparator("=");
+        assertEquals("=", formatter.getLongOptSeparator());
+        
+        StringWriter out = new StringWriter();
+
+        formatter.printHelp(new PrintWriter(out), 80, "create", "header", options, 2, 2,
"footer");
+
+        assertEquals(
+                "usage: create" + EOL +
+                "header" + EOL +
+                "     --age=<arg>    the age" + EOL +
+                "  -f <arg>          the file" + EOL +
+                "  -s,--size=<SIZE>  the size" + EOL +
+                "footer" + EOL,
+                out.toString());
+    }
+
+    public void testUsageWithLongOptSeparator() throws Exception
+    {
+        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());
+        
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.setLongOptSeparator("=");
+        
+        StringWriter out = new StringWriter();
+        
+        formatter.printUsage(new PrintWriter(out), 80, "create", options);
+        
+        assertEquals("usage: create [--age=<arg>] [-f <arg>] [-s <SIZE>]",
out.toString().trim());
+    }
 }



Mime
View raw message