I'm checking this in.

This fixes the long option problem for Raif.  Raif, if you still want
wrapping... I didn't do that.

This also fixes a bug where sometimes option text would not line up
properly.

This moves the -J option from appletviewer into Parser.  This seemed
like a good idea and ideally all our wrappers would support it.

Finally this puts the standard options into their own group which
appears at the end of --help output.  That seemed nicer than always
having --help and --version at the top.

Tom

2006-05-09  Tom Tromey  <[EMAIL PROTECTED]>

        * tools/gnu/classpath/tools/getopt/OptionGroup.java (printHelp): Added
        an initial pass to look for short options.  Added 'longOnly' option.
        * tools/gnu/classpath/tools/appletviewer/Main.java (main): Removed -J
        option.
        * tools/gnu/classpath/tools/getopt/Parser.java (parsed): Put stadnard
        options into final group.  Added -J.
        (add): Insert new groups before final group.
        (printHelp): Updated.

Index: tools/gnu/classpath/tools/appletviewer/Main.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/tools/gnu/classpath/tools/appletviewer/Main.java,v
retrieving revision 1.1
diff -u -r1.1 Main.java
--- tools/gnu/classpath/tools/appletviewer/Main.java    8 May 2006 20:36:46 
-0000       1.1
+++ tools/gnu/classpath/tools/appletviewer/Main.java    9 May 2006 16:48:13 
-0000
@@ -227,16 +227,6 @@
                                        // is the status of Classpath's 
implementation?
                                      }
                                    });
-    compatibilityGroup.add(new Option('J', Main.messages.getString
-                                      ("gcjwebplugin.j_description"),
-                                      "<virtual machine option>")
-                                   {
-                                     public void parsed(String argument) 
throws OptionException
-                                     {
-                                       // -J should be handled by the 
appletviewer wrapper binary.
-                                       // We add it here so that it shows up 
in the --help output.
-                                     }
-                                   });
     parser.add(attributeGroup);
     parser.add(pluginGroup);
     parser.add(debuggingGroup);
Index: tools/gnu/classpath/tools/getopt/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
--- tools/gnu/classpath/tools/getopt/OptionGroup.java   8 May 2006 18:38:21 
-0000       1.1
+++ tools/gnu/classpath/tools/getopt/OptionGroup.java   9 May 2006 16:48:13 
-0000
@@ -84,12 +84,26 @@
    * 
    * @param out the stream to which to print
    */
-  public void printHelp(PrintStream out)
+  public void printHelp(PrintStream out, boolean longOnly)
   {
     // Compute maximum lengths.
     int maxArgLen = 0;
-    int maxShortLen = 0;
-    Iterator it = options.iterator();
+    boolean shortOptionSeen = false;
+    Iterator it;
+
+    // The first pass only looks to see if we have a short option.
+    it = options.iterator();
+    while (it.hasNext())
+      {
+        Option option = (Option) it.next();
+        if (option.getShortName() != '\0')
+          {
+            shortOptionSeen = true;
+            break;
+          }
+      }
+
+    it = options.iterator();
     while (it.hasNext())
       {
         Option option = (Option) it.next();
@@ -100,21 +114,16 @@
         // a short option if there is also a long name for
         // the option.
         int thisArgLen = 2;
-        if (option.getShortName() != '\0')
-          {
-            // The name of the option plus some padding.
-            thisArgLen += 4;
-          }
-        maxShortLen = Math.max(thisArgLen, maxShortLen);
+        if (shortOptionSeen)
+          thisArgLen += 4;
         if (option.getLongName() != null)
           {
-            // FIXME: different if parsing in 'long option only'
-            // mode.
-            thisArgLen += 2 + option.getLongName().length();
+            // Handle either '-' or '--'.
+            thisArgLen += 1 + option.getLongName().length();
+            if (! longOnly)
+              ++thisArgLen;
           }
-        // We only need to add the argument name in once,
-        // and we don't let it contribute to the width of
-        // the short name.
+        // Add in the width of the argument name.
         if (argName != null)
           thisArgLen += 1 + argName.length();
         maxArgLen = Math.max(maxArgLen, thisArgLen);
@@ -148,13 +157,15 @@
               out.print(", ");
             column += 2;
           }
-        for (; column < maxShortLen; ++column)
+        // Indent the long option past the short options, if one
+        // was seen.
+        for (; column < (shortOptionSeen ? 6 : 2); ++column)
           out.print(' ');
         if (option.getLongName() != null)
           {
-            out.print("--");
+            out.print(longOnly ? "-" : "--");
             out.print(option.getLongName());
-            column += 2 + option.getLongName().length();
+            column += (longOnly ? 1 : 2) + option.getLongName().length();
             if (argName != null)
               {
                 out.print("=" + argName);
Index: tools/gnu/classpath/tools/getopt/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
--- tools/gnu/classpath/tools/getopt/Parser.java        8 May 2006 18:38:21 
-0000       1.1
+++ tools/gnu/classpath/tools/getopt/Parser.java        9 May 2006 16:48:13 
-0000
@@ -95,7 +95,10 @@
   {
     this.programName = programName;
     this.longOnly = longOnly;
-    defaultGroup.add(new Option("help", "print this help, then exit")
+
+    // Put standard options in their own section near the end.
+    OptionGroup finalGroup = new OptionGroup("Standard options");
+    finalGroup.add(new Option("help", "print this help, then exit")
     {
       public void parsed(String argument) throws OptionException
       {
@@ -103,7 +106,7 @@
         System.exit(0);
       }
     });
-    defaultGroup.add(new Option("version", "print version number, then exit")
+    finalGroup.add(new Option("version", "print version number, then exit")
     {
       public void parsed(String argument) throws OptionException
       {
@@ -111,6 +114,16 @@
         System.exit(0);
       }
     });
+    finalGroup.add(new Option('J', "pass argument to the Java runtime", 
"OPTION")
+    {
+      public void parsed(String argument) throws OptionException
+      {
+        // -J should be handled by the appletviewer wrapper binary.
+        // We add it here so that it shows up in the --help output.
+      }
+    });
+    add(finalGroup);
+
     add(defaultGroup);
   }
 
@@ -155,7 +168,12 @@
   public synchronized void add(OptionGroup group)
   {
     options.addAll(group.options);
-    optionGroups.add(group);
+    // This ensures that the final group always appears at the end
+    // of the options.
+    if (optionGroups.isEmpty())
+      optionGroups.add(group);
+    else
+      optionGroups.add(optionGroups.size() - 1, group);
   }
 
   void printHelp(PrintStream out)
@@ -170,7 +188,7 @@
     while (it.hasNext())
       {
         OptionGroup group = (OptionGroup) it.next();
-        group.printHelp(out);
+        group.printHelp(out, longOnly);
         out.println();
       }
 

Reply via email to