mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

lutoff
24.13.2007 a746e30f968e6d0e544e7d30b18245b3018c2838
fix for issue #1635 (In CLi Usage, the 'help' information shoud be the last element)

when iterating on the argument list, store the ussageArgument and handle
it at the end.
This proposed modification also adds code to improve handling of
"-V/--version"
3 files modified
313 ■■■■ changed files
opends/src/ads/org/opends/admin/ads/DsServiceCLI.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/util/args/ArgumentParser.java 160 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/util/args/SubCommandArgumentParser.java 151 ●●●● patch | view | raw | blame | history
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;
    }
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,8 +1059,92 @@
        continue;
      }
      // Help argument should be printed at the end
      if ((usageArgument != null) ? usageArgument.getName().equals(a.getName())
          : false)
      {
        helpArgument = a ;
        continue ;
      }
      printArgumentUsage(a, buffer);
    }
    if (helpArgument != null)
    {
      printArgumentUsage(helpArgument, buffer);
    }
    else
    {
      buffer.append(EOL);
      buffer.append("-?");
      buffer.append(EOL);
    }
  }
      // Write a line with the short and/or long identifiers that may be used
  /**
   * Retrieves a string containing usage information based on the defined
   * arguments.
   *
   * @return  A string containing usage information based on the defined
   *          arguments.
   */
  public String getUsage()
  {
    StringBuilder buffer = new StringBuilder();
    getUsage(buffer);
    return buffer.toString();
  }
  /**
   * Writes usage information based on the defined arguments to the provided
   * output stream.
   *
   * @param  outputStream  The output stream to which the usage information
   *                       should be written.
   *
   * @throws  IOException  If a problem occurs while attempting to write the
   *                       usage information to the provided output stream.
   */
  public void getUsage(OutputStream outputStream)
         throws IOException
  {
    StringBuilder buffer = new StringBuilder();
    getUsage(buffer);
    outputStream.write(getBytes(buffer.toString()));
  }
  /**
   * Indicates whether the version or the usage information has been
   * displayed to the end user either by an explicit argument like
   * "-H" or "--help", or by a built-in argument like "-?".
   *
   * @return {@code true} if the usage information has been displayed,
   *         or {@code false} if not.
   */
  public boolean usageOrVersionDisplayed()
  {
    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();
@@ -1130,8 +1215,10 @@
      }
      // 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
    // 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)
@@ -1155,8 +1242,10 @@
          }
          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
          // 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)
@@ -1184,66 +1273,5 @@
        }
      }
    }
    if (usageArgument == null)
    {
      buffer.append(EOL);
      buffer.append("-?");
      buffer.append(EOL);
    }
  }
  /**
   * Retrieves a string containing usage information based on the defined
   * arguments.
   *
   * @return  A string containing usage information based on the defined
   *          arguments.
   */
  public String getUsage()
  {
    StringBuilder buffer = new StringBuilder();
    getUsage(buffer);
    return buffer.toString();
  }
  /**
   * Writes usage information based on the defined arguments to the provided
   * output stream.
   *
   * @param  outputStream  The output stream to which the usage information
   *                       should be written.
   *
   * @throws  IOException  If a problem occurs while attempting to write the
   *                       usage information to the provided output stream.
   */
  public void getUsage(OutputStream outputStream)
         throws IOException
  {
    StringBuilder buffer = new StringBuilder();
    getUsage(buffer);
    outputStream.write(getBytes(buffer.toString()));
  }
  /**
   * Indicates whether the version or the usage information has been
   * displayed to the end user either by an explicit argument like
   * "-H" or "--help", or by a built-in argument like "-?".
   *
   * @return {@code true} if the usage information has been displayed,
   *         or {@code false} if not.
   */
  public boolean usageOrVersionDisplayed()
  {
    return usageOrVersionDisplayed;
  }
}
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;
@@ -906,15 +909,47 @@
            if (argCharacter == OPTION_SHORT_PRODUCT_VERSION)
            {
              // "-V" will always be interpreted as requesting
              // version information.
              // 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())
                {
                  if (subCmd.getArgument(OPTION_SHORT_PRODUCT_VERSION) != null)
                  {
                    dashVAccepted = false;
                    break;
                  }
                }
              }
              if (dashVAccepted)
              {
                usageOrVersionDisplayed = true;
              try
                {
                getUsage(usageOutputStream);
              } catch (Exception e) {}
                  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
            {
              // There is no such argument registered.
              int msgID = MSGID_SUBCMDPARSER_NO_GLOBAL_ARGUMENT_FOR_SHORT_ID;
              String message = getMessage(msgID, String.valueOf(argCharacter));
@@ -940,13 +975,36 @@
              if (argCharacter == OPTION_SHORT_PRODUCT_VERSION)
              {
                // "-V" will always be interpreted as requesting
                // version information.
                  // version information except if it's alreadydefined.
                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)
                {
                  usageOrVersionDisplayed = true;
                try
                {
                  getUsage(usageOutputStream);
                } catch (Exception e) {}
                    DirectoryServer.printVersion(usageOutputStream);
                  }
                  catch (Exception e)
                  {
                  }
                return;
              }
              }
              else
              {
                // There is no such argument registered.
@@ -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,6 +1369,38 @@
        continue;
      }
      // Help argument should be printed at the end
      if ((usageArgument != null) ? usageArgument.getName().equals(a.getName())
          : false)
      {
        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())
      {
@@ -1303,7 +1420,8 @@
      }
      Character shortIDChar = a.getShortIdentifier();
      boolean isHelpArg = usageArgument.getName().equals(a.getName());
    boolean isHelpArg = (usageArgument != null) ? usageArgument.getName()
        .equals(a.getName()) : false;
      if (shortIDChar != null)
      {
        if (isHelpArg)
@@ -1340,11 +1458,6 @@
      indentAndWrap("    ", a.getDescription(), buffer);
    }
    buffer.append(EOL);
  }
  /**
@@ -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;
  }