I'm checking this in.

Andrew Cagney pointed out that the getopt Parser does not work if you
add options to an OptionGroup after adding the group to the Parser.
There doesn't seem to be a good reason to reject this, so this patch
changes Parser to compute the 'options' array only when absolutely
needed.

Tom

ChangeLog:
2008-03-20  Tom Tromey  <[EMAIL PROTECTED]>

        * tools/gnu/classpath/tools/getopt/Parser.java (options): Don't
        initialize.
        (add, addFinal): Don't update options.
        (requireOptions): New method.
        (printHelp): Synchronize.  Call requireOptions.
        (parse): Call requireOptions.

Index: tools/gnu/classpath/tools/getopt/Parser.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/tools/gnu/classpath/tools/getopt/Parser.java,v
retrieving revision 1.9
diff -u -r1.9 Parser.java
--- tools/gnu/classpath/tools/getopt/Parser.java        22 Sep 2006 01:01:26 
-0000      1.9
+++ tools/gnu/classpath/tools/getopt/Parser.java        20 Mar 2008 18:02:58 
-0000
@@ -1,5 +1,5 @@
 /* Parser.java - parse command line options
- Copyright (C) 2006 Free Software Foundation, Inc.
+ Copyright (C) 2006, 2008 Free Software Foundation, Inc.
 
  This file is part of GNU Classpath.
 
@@ -66,7 +66,9 @@
 
   private boolean longOnly;
 
-  private ArrayList options = new ArrayList();
+  // All of the options.  This is null initially; users must call
+  // requireOptions before access.
+  private ArrayList options;
 
   private ArrayList optionGroups = new ArrayList();
 
@@ -218,7 +220,6 @@
    */
   public synchronized void add(Option opt)
   {
-    options.add(opt);
     defaultGroup.add(opt);
   }
 
@@ -230,7 +231,6 @@
    */
   protected synchronized void addFinal(Option opt)
   {
-    options.add(opt);
     finalGroup.add(opt);
   }
 
@@ -242,7 +242,6 @@
    */
   public synchronized void add(OptionGroup group)
   {
-    options.addAll(group.options);
     // This ensures that the final group always appears at the end
     // of the options.
     if (optionGroups.isEmpty())
@@ -251,13 +250,29 @@
       optionGroups.add(optionGroups.size() - 1, group);
   }
 
+  // Make sure the 'options' field is properly initialized.
+  private void requireOptions()
+  {
+    if (options != null)
+      return;
+    options = new ArrayList();
+    Iterator it = optionGroups.iterator();
+    while (it.hasNext())
+      {
+       OptionGroup group = (OptionGroup) it.next();
+       options.addAll(group.options);
+      }
+  }
+
   public void printHelp()
   {
     this.printHelp(System.out);
   }
 
-  void printHelp(PrintStream out)
+  synchronized void printHelp(PrintStream out)
   {
+    requireOptions();
+
     if (headerText != null)
       {
         formatText(out, headerText);
@@ -417,6 +432,7 @@
    */
   public synchronized void parse(String[] inArgs, FileArgumentCallback files)
   {
+    requireOptions();
     try
       {
         args = inArgs;

Reply via email to