From a746e30f968e6d0e544e7d30b18245b3018c2838 Mon Sep 17 00:00:00 2001
From: lutoff <lutoff@localhost>
Date: Thu, 24 May 2007 08:13:25 +0000
Subject: [PATCH] fix for issue #1635 (In CLi Usage, the 'help' information shoud be the last element)

---
 opends/src/ads/org/opends/admin/ads/DsServiceCLI.java                       |    2 
 opends/src/server/org/opends/server/util/args/ArgumentParser.java           |  276 ++++++++++++++++-------------
 opends/src/server/org/opends/server/util/args/SubCommandArgumentParser.java |  237 +++++++++++++++++++------
 3 files changed, 328 insertions(+), 187 deletions(-)

diff --git a/opends/src/ads/org/opends/admin/ads/DsServiceCLI.java b/opends/src/ads/org/opends/admin/ads/DsServiceCLI.java
index 67bb56d..c420bb0 100644
--- a/opends/src/ads/org/opends/admin/ads/DsServiceCLI.java
+++ b/opends/src/ads/org/opends/admin/ads/DsServiceCLI.java
@@ -312,7 +312,7 @@
     }
 
     // If we should just display usage information, then print it and exit.
-    if (argParser.usageDisplayed())
+    if (argParser.usageOrVersionDisplayed())
     {
       return 0;
     }
diff --git a/opends/src/server/org/opends/server/util/args/ArgumentParser.java b/opends/src/server/org/opends/server/util/args/ArgumentParser.java
index 5428335..aed1cc7 100644
--- a/opends/src/server/org/opends/server/util/args/ArgumentParser.java
+++ b/opends/src/server/org/opends/server/util/args/ArgumentParser.java
@@ -1050,6 +1050,7 @@
     buffer.append( getMessage(MSGID_DESCRIPTION_PRODUCT_VERSION));
     buffer.append(EOL);
 
+    Argument helpArgument = null ;
     for (Argument a : argumentList)
     {
       // If this argument is hidden, then skip it.
@@ -1058,133 +1059,20 @@
         continue;
       }
 
-
-      // Write a line with the short and/or long identifiers that may be used
-      // for the argument.
-      Character shortID = a.getShortIdentifier();
-      String longID = a.getLongIdentifier();
-      if (shortID != null)
+      // Help argument should be printed at the end
+      if ((usageArgument != null) ? usageArgument.getName().equals(a.getName())
+          : false)
       {
-        int currentLength = buffer.length();
-
-        if (usageArgument.getName().equals(a.getName()))
-        {
-          buffer.append("-?, ");
-        }
-
-        buffer.append("-");
-        buffer.append(shortID.charValue());
-
-        if (a.needsValue() && longID == null)
-        {
-          buffer.append(" ");
-          buffer.append(a.getValuePlaceholder());
-        }
-
-        if (longID != null)
-        {
-          StringBuilder newBuffer = new StringBuilder();
-          newBuffer.append(", --");
-          newBuffer.append(longID);
-
-          if (a.needsValue())
-          {
-            newBuffer.append(" ");
-            newBuffer.append(a.getValuePlaceholder());
-          }
-
-          int lineLength = (buffer.length() - currentLength) +
-                           newBuffer.length();
-          if (lineLength > 80)
-          {
-            buffer.append(EOL);
-            buffer.append(newBuffer.toString());
-          }
-          else
-          {
-            buffer.append(newBuffer.toString());
-          }
-        }
-
-        buffer.append(EOL);
+        helpArgument = a ;
+        continue ;
       }
-      else
-      {
-        if (longID != null)
-        {
-          if (usageArgument.getName().equals(a.getName()))
-          {
-            buffer.append("-?, ");
-          }
-          buffer.append("--");
-          buffer.append(longID);
-
-          if (a.needsValue())
-          {
-            buffer.append(" ");
-            buffer.append(a.getValuePlaceholder());
-          }
-
-          buffer.append(EOL);
-        }
-      }
-
-
-      // Write one or more lines with the description of the argument.  We will
-      // indent the description five characters and try our best to wrap at or
-      // before column 79 so it will be friendly to 80-column displays.
-      String description = a.getDescription();
-      if (description.length() <= 75)
-      {
-        buffer.append("    ");
-        buffer.append(description);
-        buffer.append(EOL);
-      }
-      else
-      {
-        String s = description;
-        while (s.length() > 75)
-        {
-          int spacePos = s.lastIndexOf(' ', 75);
-          if (spacePos > 0)
-          {
-            buffer.append("    ");
-            buffer.append(s.substring(0, spacePos).trim());
-            s = s.substring(spacePos+1).trim();
-            buffer.append(EOL);
-          }
-          else
-          {
-            // There are no spaces in the first 74 columns.  See if there is one
-            // after that point.  If so, then break there.  If not, then don't
-            // break at all.
-            spacePos = s.indexOf(' ');
-            if (spacePos > 0)
-            {
-              buffer.append("    ");
-              buffer.append(s.substring(0, spacePos).trim());
-              s = s.substring(spacePos+1).trim();
-              buffer.append(EOL);
-            }
-            else
-            {
-              buffer.append("    ");
-              buffer.append(s);
-              s = "";
-              buffer.append(EOL);
-            }
-          }
-        }
-
-        if (s.length() > 0)
-        {
-          buffer.append("    ");
-          buffer.append(s);
-          buffer.append(EOL);
-        }
-      }
+      printArgumentUsage(a, buffer);
     }
-    if (usageArgument == null)
+    if (helpArgument != null)
+    {
+      printArgumentUsage(helpArgument, buffer);
+    }
+    else
     {
       buffer.append(EOL);
       buffer.append("-?");
@@ -1245,5 +1133,145 @@
     return usageOrVersionDisplayed;
   }
 
+  /**
+  * Appends argument usage information to the provided buffer.
+  *
+  * @param a The argument to handle.
+  * @param buffer
+  *          The buffer to which the usage information should be
+  *          appended.
+  */
+   private void printArgumentUsage(Argument a, StringBuilder buffer)
+  {
+    // Write a line with the short and/or long identifiers that may be
+    // used
+    // for the argument.
+    Character shortID = a.getShortIdentifier();
+    String longID = a.getLongIdentifier();
+    if (shortID != null)
+    {
+      int currentLength = buffer.length();
+
+      if (usageArgument.getName().equals(a.getName()))
+      {
+        buffer.append("-?, ");
+      }
+
+      buffer.append("-");
+      buffer.append(shortID.charValue());
+
+      if (a.needsValue() && longID == null)
+      {
+        buffer.append(" ");
+        buffer.append(a.getValuePlaceholder());
+      }
+
+      if (longID != null)
+      {
+        StringBuilder newBuffer = new StringBuilder();
+        newBuffer.append(", --");
+        newBuffer.append(longID);
+
+        if (a.needsValue())
+        {
+          newBuffer.append(" ");
+          newBuffer.append(a.getValuePlaceholder());
+        }
+
+        int lineLength = (buffer.length() - currentLength) +
+                         newBuffer.length();
+        if (lineLength > 80)
+        {
+          buffer.append(EOL);
+          buffer.append(newBuffer.toString());
+        }
+        else
+        {
+          buffer.append(newBuffer.toString());
+        }
+      }
+
+      buffer.append(EOL);
+    }
+    else
+    {
+      if (longID != null)
+      {
+        if (usageArgument.getName().equals(a.getName()))
+        {
+          buffer.append("-?, ");
+        }
+        buffer.append("--");
+        buffer.append(longID);
+
+        if (a.needsValue())
+        {
+          buffer.append(" ");
+          buffer.append(a.getValuePlaceholder());
+        }
+
+        buffer.append(EOL);
+      }
+    }
+
+
+    // Write one or more lines with the description of the argument.
+    // We will
+    // indent the description five characters and try our best to wrap
+    // at or
+    // before column 79 so it will be friendly to 80-column displays.
+    String description = a.getDescription();
+    if (description.length() <= 75)
+    {
+      buffer.append("    ");
+      buffer.append(description);
+      buffer.append(EOL);
+    }
+    else
+    {
+      String s = description;
+      while (s.length() > 75)
+      {
+        int spacePos = s.lastIndexOf(' ', 75);
+        if (spacePos > 0)
+        {
+          buffer.append("    ");
+          buffer.append(s.substring(0, spacePos).trim());
+          s = s.substring(spacePos+1).trim();
+          buffer.append(EOL);
+        }
+        else
+        {
+          // There are no spaces in the first 74 columns. See if there
+          // is one
+          // after that point. If so, then break there. If not, then
+          // don't
+          // break at all.
+          spacePos = s.indexOf(' ');
+          if (spacePos > 0)
+          {
+            buffer.append("    ");
+            buffer.append(s.substring(0, spacePos).trim());
+            s = s.substring(spacePos+1).trim();
+            buffer.append(EOL);
+          }
+          else
+          {
+            buffer.append("    ");
+            buffer.append(s);
+            s = "";
+            buffer.append(EOL);
+          }
+        }
+      }
+
+      if (s.length() > 0)
+      {
+        buffer.append("    ");
+        buffer.append(s);
+        buffer.append(EOL);
+      }
+    }
+  }
 }
 
diff --git a/opends/src/server/org/opends/server/util/args/SubCommandArgumentParser.java b/opends/src/server/org/opends/server/util/args/SubCommandArgumentParser.java
index ad45b30..a02bebb 100644
--- a/opends/src/server/org/opends/server/util/args/SubCommandArgumentParser.java
+++ b/opends/src/server/org/opends/server/util/args/SubCommandArgumentParser.java
@@ -41,6 +41,7 @@
 import org.opends.server.core.DirectoryServer;
 
 import static org.opends.server.messages.MessageHandler.*;
+import static org.opends.server.messages.ToolMessages.*;
 import static org.opends.server.messages.UtilityMessages.*;
 import static org.opends.server.util.ServerConstants.*;
 import static org.opends.server.util.StaticUtils.*;
@@ -72,7 +73,7 @@
   private boolean longArgumentsCaseSensitive;
 
   // Indicates whether the usage information has been displayed.
-  private boolean usageDisplayed;
+  private boolean usageOrVersionDisplayed;
 
   // The set of global arguments defined for this parser, referenced by short
   // ID.
@@ -140,7 +141,7 @@
     globalShortIDMap   = new HashMap<Character,Argument>();
     globalLongIDMap    = new HashMap<String,Argument>();
     subCommands        = new TreeMap<String,SubCommand>();
-    usageDisplayed     = false;
+    usageOrVersionDisplayed     = false;
     rawArguments       = null;
     subCommand         = null;
     usageArgument      = null;
@@ -658,7 +659,7 @@
     this.rawArguments = rawArguments;
     this.subCommand = null;
     this.trailingArguments = new ArrayList<String>();
-    this.usageDisplayed = false;
+    this.usageOrVersionDisplayed = false;
 
     boolean inTrailingArgs = false;
 
@@ -747,6 +748,7 @@
               try
               {
                 DirectoryServer.printVersion(usageOutputStream);
+                usageOrVersionDisplayed = true ;
               } catch (Exception e) {}
 
               return;
@@ -783,6 +785,7 @@
                 try
                 {
                   DirectoryServer.printVersion(usageOutputStream);
+                  usageOrVersionDisplayed = true ;
                 } catch (Exception e) {}
 
                 return;
@@ -905,13 +908,45 @@
             else
             if (argCharacter == OPTION_SHORT_PRODUCT_VERSION)
             {
-              // "-V" will always be interpreted as requesting
-              // version information.
-              try
+              //  "-V" will always be interpreted as requesting
+              // version information except if it's already defined.
+              boolean dashVAccepted = true;
+              if (globalShortIDMap.containsKey(OPTION_SHORT_PRODUCT_VERSION))
+              {
+                dashVAccepted = false;
+              }
+              else
+              {
+                for (SubCommand subCmd : subCommands.values())
                 {
-                getUsage(usageOutputStream);
-              } catch (Exception e) {}
-              return;
+                  if (subCmd.getArgument(OPTION_SHORT_PRODUCT_VERSION) != null)
+                  {
+                    dashVAccepted = false;
+                    break;
+                  }
+                }
+              }
+              if (dashVAccepted)
+              {
+                usageOrVersionDisplayed = true;
+                try
+                {
+                  DirectoryServer.printVersion(usageOutputStream);
+                }
+                catch (Exception e)
+                {
+                }
+                return;
+              }
+              else
+              {
+                // -V is defined in another suncommand, so we can
+                // accepted it as the version information argument
+                int msgID = MSGID_SUBCMDPARSER_NO_GLOBAL_ARGUMENT_FOR_SHORT_ID;
+                String message = getMessage(msgID,
+                    String.valueOf(argCharacter));
+                throw new ArgumentException(msgID, message);
+              }
             }
             else
             {
@@ -939,13 +974,36 @@
               else
               if (argCharacter == OPTION_SHORT_PRODUCT_VERSION)
               {
-                // "-V" will always be interpreted as requesting
-                // version information.
-                try
+                  // "-V" will always be interpreted as requesting
+                  // version information except if it's alreadydefined.
+                boolean dashVAccepted = true;
+                if (globalShortIDMap.containsKey(OPTION_SHORT_PRODUCT_VERSION))
                 {
-                  getUsage(usageOutputStream);
-                } catch (Exception e) {}
-                return;
+                  dashVAccepted = false;
+                }
+                else
+                {
+                  for (SubCommand subCmd : subCommands.values())
+                  {
+                    if (subCmd.getArgument(OPTION_SHORT_PRODUCT_VERSION)!=null)
+                    {
+                      dashVAccepted = false;
+                      break;
+                    }
+                  }
+                }
+                if (dashVAccepted)
+                {
+                  usageOrVersionDisplayed = true;
+                  try
+                  {
+                    DirectoryServer.printVersion(usageOutputStream);
+                  }
+                  catch (Exception e)
+                  {
+                  }
+                  return;
+                }
               }
               else
               {
@@ -1228,7 +1286,7 @@
    */
   public void getFullUsage(StringBuilder buffer)
   {
-    usageDisplayed = true;
+    usageOrVersionDisplayed = true;
     if ((toolDescription != null) && (toolDescription.length() > 0))
     {
       buffer.append(wrapText(toolDescription, 79));
@@ -1277,6 +1335,33 @@
     buffer.append("The accepted value for global options are:");
     buffer.append(EOL);
 
+    // --version is a builtin option
+    boolean dashVAccepted = true;
+    if (globalShortIDMap.containsKey(OPTION_SHORT_PRODUCT_VERSION))
+    {
+      dashVAccepted = false;
+    }
+    else
+    {
+      for (SubCommand subCmd : subCommands.values())
+      {
+        if (subCmd.getArgument(OPTION_SHORT_PRODUCT_VERSION) != null)
+        {
+          dashVAccepted = false;
+          break;
+        }
+      }
+    }
+    if (dashVAccepted)
+    {
+      buffer.append("-" + OPTION_SHORT_PRODUCT_VERSION + ", ");
+    }
+    buffer.append("--" + OPTION_LONG_PRODUCT_VERSION);
+    buffer.append(EOL);
+    buffer.append("    ");
+    buffer.append( getMessage(MSGID_DESCRIPTION_PRODUCT_VERSION));
+    buffer.append(EOL);
+    Argument helpArgument = null ;
     for (Argument a : globalArgumentList)
     {
       if (a.isHidden())
@@ -1284,65 +1369,93 @@
         continue;
       }
 
-      String value;
-      if (a.needsValue())
+      // Help argument should be printed at the end
+      if ((usageArgument != null) ? usageArgument.getName().equals(a.getName())
+          : false)
       {
-        String valuePlaceholder = a.getValuePlaceholder();
-        if (valuePlaceholder == null)
-        {
-          value = " {value}";
-        }
-        else
-        {
-          value = " " + valuePlaceholder;
-        }
+        helpArgument = a ;
+        continue ;
+      }
+
+      printArgumentUsage(a, buffer);
+    }
+    if (helpArgument != null)
+    {
+      printArgumentUsage(helpArgument, buffer);
+    }
+    else
+    {
+      buffer.append("-?");
+    }
+    buffer.append(EOL);
+  }
+
+
+/**
+ * Appends argument usage information to the provided buffer.
+ *
+ * @param a The argument to handle.
+ * @param buffer
+ *          The buffer to which the usage information should be
+ *          appended.
+ */
+  private void printArgumentUsage(Argument a, StringBuilder buffer)
+  {
+    String value;
+    if (a.needsValue())
+    {
+      String valuePlaceholder = a.getValuePlaceholder();
+      if (valuePlaceholder == null)
+      {
+        value = " {value}";
       }
       else
       {
-        value = "";
+        value = " " + valuePlaceholder;
       }
+    }
+    else
+    {
+      value = "";
+    }
 
-      Character shortIDChar = a.getShortIdentifier();
-      boolean isHelpArg = usageArgument.getName().equals(a.getName());
-      if (shortIDChar != null)
+    Character shortIDChar = a.getShortIdentifier();
+    boolean isHelpArg = (usageArgument != null) ? usageArgument.getName()
+        .equals(a.getName()) : false;
+    if (shortIDChar != null)
+    {
+      if (isHelpArg)
+      {
+        buffer.append("-?, ");
+      }
+      buffer.append("-");
+      buffer.append(shortIDChar);
+
+      String longIDString = a.getLongIdentifier();
+      if (longIDString != null)
+      {
+        buffer.append(", --");
+        buffer.append(longIDString);
+      }
+      buffer.append(value);
+    }
+    else
+    {
+      String longIDString = a.getLongIdentifier();
+      if (longIDString != null)
       {
         if (isHelpArg)
         {
           buffer.append("-?, ");
         }
-        buffer.append("-");
-        buffer.append(shortIDChar);
-
-        String longIDString = a.getLongIdentifier();
-        if (longIDString != null)
-        {
-          buffer.append(", --");
-          buffer.append(longIDString);
-        }
+        buffer.append("--");
+        buffer.append(longIDString);
         buffer.append(value);
       }
-      else
-      {
-        String longIDString = a.getLongIdentifier();
-        if (longIDString != null)
-        {
-          if (isHelpArg)
-          {
-            buffer.append("-?, ");
-          }
-          buffer.append("--");
-          buffer.append(longIDString);
-          buffer.append(value);
-        }
-      }
-
-      buffer.append(EOL);
-      indentAndWrap("    ", a.getDescription(), buffer);
     }
 
     buffer.append(EOL);
-
-
+    indentAndWrap("    ", a.getDescription(), buffer);
   }
 
 
@@ -1358,7 +1471,7 @@
    */
   public void getSubCommandUsage(StringBuilder buffer, SubCommand subCommand)
   {
-    usageDisplayed = true;
+    usageOrVersionDisplayed = true;
     String scriptName = System.getProperty(PROPERTY_SCRIPT_NAME);
     String printName;
     if ((scriptName == null) || (scriptName.length() == 0))
@@ -1615,9 +1728,9 @@
    * @return  {@code true} if the usage information has been displayed, or
    *          {@code false} if not.
    */
-  public boolean usageDisplayed()
+  public boolean usageOrVersionDisplayed()
   {
-    return usageDisplayed;
+    return usageOrVersionDisplayed;
   }
 
 

--
Gitblit v1.10.0