hello all,

i started integrating the new option parsing code for use with the 
jarsigner tool.

the attached proposed patch adds better handling of longOnly options 
when printing the tool's help text.

ok to commit?


cheers;
rsn
Index: Parser.java
===================================================================
RCS file: /cvsroot/classpath/classpath/tools/gnu/classpath/tools/getopt/Parser.java,v
retrieving revision 1.1
diff -u -r1.1 Parser.java
--- Parser.java	8 May 2006 18:38:21 -0000	1.1
+++ Parser.java	9 May 2006 14:23:02 -0000
@@ -158,11 +158,16 @@
     optionGroups.add(group);
   }

-  void printHelp(PrintStream out)
+  public void printHelp()
+  {
+    this.printHelp(System.out);
+  }
+
+  protected void printHelp(PrintStream out)
   {
     if (headerText != null)
       {
-        out.println(headerText);
+        printText(out, headerText);
         out.println();
       }

@@ -170,12 +175,37 @@
     while (it.hasNext())
       {
         OptionGroup group = (OptionGroup) it.next();
-        group.printHelp(out);
+        group.printHelp(out, longOnly);
         out.println();
       }

     if (footerText != null)
-      out.println(footerText);
+      printText(out, footerText);
+  }
+
+  private void printText(PrintStream out, String text)
+  {
+    int length = text.length();
+    int start = 0, finish, newLineNdx;
+    boolean foundNewLine;
+    while (start < length)
+      {
+        finish = start + 79;
+        if (finish >= length)
+          finish = length;
+        else
+          while (text.charAt(finish) != ' ')
+            finish--;
+
+        // is there a newline in the substring [start..finish]?
+        newLineNdx = text.indexOf('\n', start);
+        foundNewLine = newLineNdx > start && newLineNdx < finish;
+        if (foundNewLine)
+          finish = newLineNdx;
+
+        out.println(text.substring(start, finish));
+        start = finish + 1;
+      }
   }

   private String getArgument(String request) throws OptionException
Index: OptionGroup.java
===================================================================
RCS file: /cvsroot/classpath/classpath/tools/gnu/classpath/tools/getopt/OptionGroup.java,v
retrieving revision 1.1
diff -u -r1.1 OptionGroup.java
--- OptionGroup.java	8 May 2006 18:38:21 -0000	1.1
+++ OptionGroup.java	9 May 2006 14:23:53 -0000
@@ -84,7 +84,20 @@
    *
    * @param out the stream to which to print
    */
-  public void printHelp(PrintStream out)
+  void printHelp(PrintStream out, boolean longOnly)
+  {
+    if (longOnly)
+      printHelpLongOnly(out);
+    else
+      printHelp(out);
+  }
+
+  /**
+   * Print the help output for this option group.
+   *
+   * @param out the stream to which to print
+   */
+  private void printHelp(PrintStream out)
   {
     // Compute maximum lengths.
     int maxArgLen = 0;
@@ -168,4 +181,76 @@
         out.println(option.getDescription());
       }
   }
+
+  private static final String INDENT = "                                ";
+
+  /**
+   * Print the help output for this option group.
+   *
+   * @param out the stream to which to print
+   */
+  private void printHelpLongOnly(PrintStream out)
+  {
+    if (name != null)
+      out.println(name + ":");
+
+    for (Iterator it = options.iterator(); it.hasNext();)
+      {
+        Option option = (Option) it.next();
+        String argName = option.getArgumentName();
+        int column = 0;
+        String longName = option.getLongName();
+        out.print("  -" + longName);
+        column += 3 + longName.length();
+        if (argName != null)
+          {
+            out.print(" " + argName);
+            column += 1 + argName.length();
+          }
+
+        if (column < 33)
+          {
+            out.print(INDENT.substring(0, 32 - column));
+            column = 33;
+          }
+        else
+          {
+            out.print("  ");
+            column += 2;
+          }
+
+        String description = option.getDescription();
+        int length = description.length();
+        String s;
+        int start = 0, finish, newLineNdx;
+        boolean firstLine = true, foundNewLine;
+        while (start < length)
+          {
+            // find a new finish
+            finish = start + (firstLine ? 79 - column : 47);
+            if (finish >= length)
+              finish = length;
+            else
+              while (description.charAt(finish) != ' ')
+                finish--;
+
+            // is there a newline in the substring [start..finish]?
+            newLineNdx = description.indexOf('\n', start);
+            foundNewLine = newLineNdx > start && newLineNdx < finish;
+            if (foundNewLine)
+              finish = newLineNdx;
+
+            if (firstLine)
+              firstLine = false;
+            else
+              out.print(INDENT);
+
+            out.println(description.substring(start, finish));
+            if (foundNewLine)
+              out.println();
+
+            start = finish + 1;
+          }
+      }
+  }
 }

Attachment: pgpfNofyAdwyf.pgp
Description: PGP signature

Reply via email to