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

matthew_swift
22.00.2007 27995a8327fd4af5c902ecd62e7b174383d8ec13
Update the sub-command argument parser as follows:

* myapp --help: sub-command help options (e.g. --help-xxx) are now listed in the sub-command usage section when there are sub-command groups defined

* myapp --help-xxx: no longer displays global options, just the sub-commands
2 files modified
150 ■■■■■ changed files
opends/src/server/org/opends/server/messages/UtilityMessages.java 9 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/util/args/SubCommandArgumentParser.java 141 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/messages/UtilityMessages.java
@@ -1643,6 +1643,13 @@
  public static final int MSGID_SUBCMDPARSER_GLOBAL_HEADING =
    CATEGORY_MASK_UTIL | SEVERITY_MASK_INFORMATIONAL | 155;
  /**
   * The message ID for the message that will be used as the description of the
   * Global option reference.  This does take one argument.
   */
  public static final int MSGID_GLOBAL_HELP_REFERENCE =
    CATEGORY_MASK_UTIL | SEVERITY_MASK_INFORMATIONAL | 156;
  /**
   * Associates a set of generic messages with the message IDs defined in this
@@ -2188,6 +2195,8 @@
                    "See \"%s --help-{category}\"");
    registerMessage(MSGID_SUBCMDPARSER_GLOBAL_HEADING,
                    "The accepted value for global options are:");
    registerMessage(MSGID_GLOBAL_HELP_REFERENCE,
                    "See \"%s --help\" to get more usage help");
  }
}
opends/src/server/org/opends/server/util/args/SubCommandArgumentParser.java
@@ -1486,10 +1486,10 @@
      if (usageGroupArguments.size() > 1) {
        // We have sub-command groups, so don't display any
        // sub-commands by default.
        getFullUsage(Collections.<SubCommand> emptySet(), buffer);
        getFullUsage(Collections.<SubCommand> emptySet(), true, buffer);
      } else {
        // No grouping, so display all sub-commands.
        getFullUsage(subCommands.values(), buffer);
        getFullUsage(subCommands.values(), true, buffer);
      }
    } else {
      getSubCommandUsage(buffer, subCommand);
@@ -1501,6 +1501,28 @@
  /**
   * Retrieves a string describing how the user can get more help.
   *
   * @return A string describing how the user can get more help.
   */
  public String getHelpUsageReference()
  {
    usageOrVersionDisplayed = true;
    String scriptName = System.getProperty(PROPERTY_SCRIPT_NAME);
    if ((scriptName == null) || (scriptName.length() == 0))
    {
      scriptName = "java " + mainClassName;
    }
    StringBuilder buffer = new StringBuilder();
    buffer.append(getMessage(MSGID_GLOBAL_HELP_REFERENCE, scriptName));
    buffer.append(EOL);
    return buffer.toString();
  }
  /**
   * Retrieves the set of unnamed trailing arguments that were
   * provided on the command line.
   *
@@ -1552,9 +1574,15 @@
    if (a.equals(usageArgument) && subCommand != null) {
      getSubCommandUsage(buffer, subCommand);
    } else if (a.equals(usageArgument) && usageGroupArguments.size() <= 1) {
      getFullUsage(subCommands.values(), buffer);
      // No groups - so display all sub-commands.
      getFullUsage(subCommands.values(), true, buffer);
    } else if (a.equals(usageArgument)) {
      // Using groups - so display all sub-commands group help.
      getFullUsage(Collections.<SubCommand> emptySet(), true, buffer);
    } else {
      getFullUsage(usageGroupArguments.get(a), buffer);
      // Requested help on specific group - don't display global
      // options.
      getFullUsage(usageGroupArguments.get(a), false, buffer);
    }
    outputStream.write(getBytes(buffer.toString()));
@@ -1572,7 +1600,8 @@
  // Appends complete usage information for the specified set of
  // sub-commands.
  private void getFullUsage(Collection<SubCommand> c, StringBuilder buffer) {
  private void getFullUsage(Collection<SubCommand> c,
      boolean showGlobalOptions, StringBuilder buffer) {
    usageOrVersionDisplayed = true;
    if ((toolDescription != null) && (toolDescription.length() > 0))
    {
@@ -1598,10 +1627,18 @@
    buffer.append(EOL);
    if (c.isEmpty()) {
      buffer.append("    ");
      buffer.append(getMessage(MSGID_SUBCMDPARSER_SUBCMD_REFERENCE,
          scriptName));
      buffer.append(EOL);
      // Display usage arguments (except the default one).
      for (Argument a : globalArgumentList) {
        if (a.isHidden()) {
          continue;
        }
        if (usageGroupArguments.containsKey(a)) {
          if (!a.equals(usageArgument)) {
            printArgumentUsage(a, buffer);
          }
        }
      }
    } else {
      int indentNb = 0;
      for (SubCommand sc : c) {
@@ -1627,67 +1664,57 @@
    }
    buffer.append(EOL);
    buffer.append(getMessage(MSGID_SUBCMDPARSER_GLOBAL_HEADING));
    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 (showGlobalOptions) {
      buffer.append(getMessage(MSGID_SUBCMDPARSER_GLOBAL_HEADING));
      buffer.append(EOL);
      // --version is a builtin option
      boolean dashVAccepted = true;
      if (globalShortIDMap.containsKey(OPTION_SHORT_PRODUCT_VERSION))
      {
        if (subCmd.getArgument(OPTION_SHORT_PRODUCT_VERSION) != null)
        dashVAccepted = false;
      }
      else
      {
        for (SubCommand subCmd : subCommands.values())
        {
          dashVAccepted = false;
          break;
          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);
    // Display non-usage arguments.
    for (Argument a : globalArgumentList) {
      if (a.isHidden()) {
        continue;
      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);
      if (!usageGroupArguments.containsKey(a)) {
        printArgumentUsage(a, buffer);
      }
    }
      // Display non-usage arguments.
      for (Argument a : globalArgumentList) {
        if (a.isHidden()) {
          continue;
        }
    // Display usage arguments (except the default one).
    for (Argument a : globalArgumentList) {
      if (a.isHidden()) {
        continue;
      }
      if (usageGroupArguments.containsKey(a)) {
        if (!a.equals(usageArgument)) {
        if (!usageGroupArguments.containsKey(a)) {
          printArgumentUsage(a, buffer);
        }
      }
    }
    // Finally print default usage argument.
    if (usageArgument != null) {
      printArgumentUsage(usageArgument, buffer);
    } else {
      buffer.append("-?");
      // Finally print default usage argument.
      if (usageArgument != null) {
        printArgumentUsage(usageArgument, buffer);
      } else {
        buffer.append("-?");
      }
      buffer.append(EOL);
    }
    buffer.append(EOL);
  }