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

neil_a_wilson
09.06.2007 4915128605a07d7c82fda681596d4da9746e3f97
Update the argument parser so that it will always treat "-?" as as request to
see the usage information. Also, allow short identifiers to be requested using
a forward slash in addition to a single dash (e.g., "/?" will be treated the
same as "-?").

OpenDS Issue Number: 1345
32 files modified
299 ■■■■ changed files
opends/src/server/org/opends/server/core/DirectoryServer.java 4 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/plugins/profiler/ProfileViewer.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/BackUpDB.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/ConfigureDS.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/EncodePassword.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/ExportLDIF.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/ImportLDIF.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/InstallDS.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/LDAPCompare.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/LDAPDelete.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/LDAPModify.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/LDAPPasswordModify.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/LDAPSearch.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/LDIFDiff.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/LDIFModify.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/LDIFSearch.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/ListBackends.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/RestoreDB.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/StopDS.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/VerifyIndex.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/WaitForFileDelete.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/makeldif/MakeLDIF.java 2 ●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/util/args/ArgumentParser.java 68 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/util/args/SubCommandArgumentParser.java 112 ●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/EncodePasswordTestCase.java 15 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPCompareTestCase.java 6 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPDeleteTestCase.java 6 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPModifyTestCase.java 6 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPPasswordModifyTestCase.java 8 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchTestCase.java 6 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDIFDiffTestCase.java 13 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ListBackendsTestCase.java 13 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/core/DirectoryServer.java
@@ -7742,7 +7742,7 @@
      //   exit code of 0.  Otherwise, it will have an exit code that is
      //   something other than 0, 98, or 99 to indicate that a problem
      // occurred.
      if (displayUsage.isPresent())
      if (argParser.usageDisplayed())
      {
        // We're just trying to display usage, and that's already been done so
        // exit with a code of zero.
@@ -7805,7 +7805,7 @@
        }
      }
    }
    else if (displayUsage.isPresent())
    else if (argParser.usageDisplayed())
    {
      System.exit(0);
    }
opends/src/server/org/opends/server/plugins/profiler/ProfileViewer.java
@@ -160,7 +160,7 @@
    // If we should just display usage information, then print it and exit.
    if (displayUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      System.exit(0);
    }
opends/src/server/org/opends/server/tools/BackUpDB.java
@@ -253,7 +253,7 @@
    // If we should just display usage information, then print it and exit.
    if (displayUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/ConfigureDS.java
@@ -239,7 +239,7 @@
    // If we should just display usage information, then print it and exit.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/EncodePassword.java
@@ -266,7 +266,7 @@
    // If we should just display usage information, then we've already done it
    // so just return without doing anything else.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/ExportLDIF.java
@@ -289,7 +289,7 @@
    // If we should just display usage information, then print it and exit.
    if (displayUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/ImportLDIF.java
@@ -329,7 +329,7 @@
    // If we should just display usage information, then print it and exit.
    if (displayUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/InstallDS.java
@@ -306,7 +306,7 @@
    // If either the showUsage or testOnly arguments were provided, then we're
    // done.
    if (showUsage.isPresent() || testOnly.isPresent())
    if (argParser.usageDisplayed() || testOnly.isPresent())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/LDAPCompare.java
@@ -535,7 +535,7 @@
    }
    // If we should just display usage information, then print it and exit.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/LDAPDelete.java
@@ -509,7 +509,7 @@
    // If we should just display usage information, then it has already been
    // done so just exit.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/LDAPModify.java
@@ -816,7 +816,7 @@
    }
    // If we should just display usage information, then print it and exit.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/LDAPPasswordModify.java
@@ -364,7 +364,7 @@
    // If the usage argument was provided, then we don't need to do anything
    // else.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/LDAPSearch.java
@@ -844,7 +844,7 @@
    }
    // If we should just display usage information, then print it and exit.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/LDIFDiff.java
@@ -213,7 +213,7 @@
    // If we should just display usage information, then print it and exit.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/LDIFModify.java
@@ -496,7 +496,7 @@
    // If we should just display usage information, then print it and exit.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/LDIFSearch.java
@@ -256,7 +256,7 @@
    // If we should just display usage information, then print it and exit.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/ListBackends.java
@@ -219,7 +219,7 @@
    // If we should just display usage information, then it's already been done
    // so just return.
    if (displayUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/RestoreDB.java
@@ -210,7 +210,7 @@
    // If we should just display usage information, then print it and exit.
    if (displayUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/tools/StopDS.java
@@ -333,7 +333,7 @@
    // If we should just display usage information, then exit because it will
    // have already been done.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return LDAPResultCode.SUCCESS;
    }
opends/src/server/org/opends/server/tools/VerifyIndex.java
@@ -168,7 +168,7 @@
    // If we should just display usage information, then print it and exit.
    if (displayUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      System.exit(0);
    }
opends/src/server/org/opends/server/tools/WaitForFileDelete.java
@@ -181,7 +181,7 @@
    // If we should just display usage information, then print it and exit.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return EXIT_CODE_SUCCESS;
    }
opends/src/server/org/opends/server/tools/makeldif/MakeLDIF.java
@@ -199,7 +199,7 @@
    // If we should just display usage information, then print it and exit.
    if (showUsage.isPresent())
    if (argParser.usageDisplayed())
    {
      return 0;
    }
opends/src/server/org/opends/server/util/args/ArgumentParser.java
@@ -69,6 +69,9 @@
  // manner.
  private boolean longArgumentsCaseSensitive;
  // Indicates whether the usage information has been displayed.
  private boolean usageDisplayed;
  // The set of arguments defined for this parser, referenced by short ID.
  private HashMap<Character,Argument> shortIDMap;
@@ -133,6 +136,7 @@
    shortIDMap              = new HashMap<Character,Argument>();
    longIDMap               = new HashMap<String,Argument>();
    allowsTrailingArguments = false;
    usageDisplayed          = false;
    trailingArgsDisplayName = null;
    maxTrailingArguments    = 0;
    minTrailingArguments    = 0;
@@ -194,6 +198,7 @@
    shortIDMap        = new HashMap<Character,Argument>();
    longIDMap         = new HashMap<String,Argument>();
    trailingArguments = new ArrayList<String>();
    usageDisplayed    = false;
    rawArguments      = null;
    usageArgument     = null;
    usageOutputStream = System.out;
@@ -646,10 +651,24 @@
        Argument a = longIDMap.get(argName);
        if (a == null)
        {
          // There is no such argument registered.
          int    msgID   = MSGID_ARGPARSER_NO_ARGUMENT_WITH_LONG_ID;
          String message = getMessage(msgID, argName);
          throw new ArgumentException(msgID, message);
          if (argName.equals("help"))
          {
            // "--help" will always be interpreted as requesting usage
            // information.
            try
            {
              getUsage(usageOutputStream);
            } catch (Exception e) {}
            return;
          }
          else
          {
            // There is no such argument registered.
            int    msgID   = MSGID_ARGPARSER_NO_ARGUMENT_WITH_LONG_ID;
            String message = getMessage(msgID, argName);
            throw new ArgumentException(msgID, message);
          }
        }
        else
        {
@@ -715,14 +734,14 @@
          }
        }
      }
      else if (arg.startsWith("-"))
      else if (arg.startsWith("-") || arg.startsWith("/"))
      {
        // This indicates that we are using the 1-character name to reference
        // the argument.  It may be in any of the following forms:
        // -n
        // -nvalue
        // -n value
        if (arg.equals("-"))
        if (arg.equals("-") || arg.equals("/"))
        {
          int    msgID   = MSGID_ARGPARSER_INVALID_DASH_AS_ARGUMENT;
          String message = getMessage(msgID);
@@ -745,10 +764,23 @@
        Argument a = shortIDMap.get(argCharacter);
        if (a == null)
        {
          // There is no such argument registered.
          int    msgID   = MSGID_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID;
          String message = getMessage(msgID, String.valueOf(argCharacter));
          throw new ArgumentException(msgID, message);
          if (argCharacter == '?')
          {
            // "-?" will always be interpreted as requesting usage information.
            try
            {
              getUsage(usageOutputStream);
            } catch (Exception e) {}
            return;
          }
          else
          {
            // There is no such argument registered.
            int    msgID   = MSGID_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID;
            String message = getMessage(msgID, String.valueOf(argCharacter));
            throw new ArgumentException(msgID, message);
          }
        }
        else
        {
@@ -938,6 +970,7 @@
   */
  public void getUsage(StringBuilder buffer)
  {
    usageDisplayed = true;
    if ((toolDescription != null) && (toolDescription.length() > 0))
    {
      buffer.append(wrapText(toolDescription, 79));
@@ -1141,5 +1174,20 @@
    outputStream.write(getBytes(buffer.toString()));
  }
  /**
   * Indicates whether 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 usageDisplayed()
  {
    return usageDisplayed;
  }
}
opends/src/server/org/opends/server/util/args/SubCommandArgumentParser.java
@@ -62,6 +62,9 @@
  // case-sensitive manner.
  private boolean longArgumentsCaseSensitive;
  // Indicates whether the usage information has been displayed.
  private boolean usageDisplayed;
  // The set of global arguments defined for this parser, referenced by short
  // ID.
  private HashMap<Character,Argument> globalShortIDMap;
@@ -127,6 +130,7 @@
    globalShortIDMap   = new HashMap<Character,Argument>();
    globalLongIDMap    = new HashMap<String,Argument>();
    subCommands        = new HashMap<String,SubCommand>();
    usageDisplayed     = false;
    rawArguments       = null;
    subCommand         = null;
    usageArgument      = null;
@@ -698,20 +702,48 @@
        {
          if (subCommand == null)
          {
            // There is no such global argument.
            int    msgID   = MSGID_SUBCMDPARSER_NO_GLOBAL_ARGUMENT_FOR_LONG_ID;
            String message = getMessage(msgID, argName);
            throw new ArgumentException(msgID, message);
            if (argName.equals("help"))
            {
              // "--help" will always be interpreted as requesting usage
              // information.
              try
              {
                getUsage(usageOutputStream);
              } catch (Exception e) {}
              return;
            }
            else
            {
              // There is no such global argument.
              int msgID = MSGID_SUBCMDPARSER_NO_GLOBAL_ARGUMENT_FOR_LONG_ID;
              String message = getMessage(msgID, argName);
              throw new ArgumentException(msgID, message);
            }
          }
          else
          {
            a = subCommand.getArgument(argName);
            if (a == null)
            {
              // There is no such global or subcommand argument.
              int    msgID   = MSGID_SUBCMDPARSER_NO_ARGUMENT_FOR_LONG_ID;
              String message = getMessage(msgID, argName);
              throw new ArgumentException(msgID, message);
              if (argName.equals("help"))
              {
                // "--help" will always be interpreted as requesting usage
                // information.
                try
                {
                  getUsage(usageOutputStream);
                } catch (Exception e) {}
                return;
              }
              else
              {
                // There is no such global or subcommand argument.
                int    msgID   = MSGID_SUBCMDPARSER_NO_ARGUMENT_FOR_LONG_ID;
                String message = getMessage(msgID, argName);
                throw new ArgumentException(msgID, message);
              }
            }
          }
        }
@@ -777,14 +809,14 @@
          }
        }
      }
      else if (arg.startsWith("-"))
      else if (arg.startsWith("-") || arg.startsWith("/"))
      {
        // This indicates that we are using the 1-character name to reference
        // the argument.  It may be in any of the following forms:
        // -n
        // -nvalue
        // -n value
        if (arg.equals("-"))
        if (arg.equals("-") || arg.equals("/"))
        {
          int    msgID   = MSGID_SUBCMDPARSER_INVALID_DASH_AS_ARGUMENT;
          String message = getMessage(msgID);
@@ -810,20 +842,47 @@
        {
          if (subCommand == null)
          {
            // There is no such argument registered.
            int msgID = MSGID_SUBCMDPARSER_NO_GLOBAL_ARGUMENT_FOR_SHORT_ID;
            String message = getMessage(msgID, String.valueOf(argCharacter));
            throw new ArgumentException(msgID, message);
            if (argCharacter == '?')
            {
              // "-?" will always be interpreted as requesting usage.
              try
              {
                getUsage(usageOutputStream);
              } catch (Exception e) {}
              return;
            }
            else
            {
              // There is no such argument registered.
              int msgID = MSGID_SUBCMDPARSER_NO_GLOBAL_ARGUMENT_FOR_SHORT_ID;
              String message = getMessage(msgID, String.valueOf(argCharacter));
              throw new ArgumentException(msgID, message);
            }
          }
          else
          {
            a = subCommand.getArgument(argCharacter);
            if (a == null)
            {
              // There is no such argument registered.
              int    msgID   = MSGID_SUBCMDPARSER_NO_ARGUMENT_FOR_SHORT_ID;
              String message = getMessage(msgID, String.valueOf(argCharacter));
              throw new ArgumentException(msgID, message);
              if (argCharacter == '?')
              {
                // "-?" will always be interpreted as requesting usage.
                try
                {
                  getUsage(usageOutputStream);
                } catch (Exception e) {}
                return;
              }
              else
              {
                // There is no such argument registered.
                int    msgID   = MSGID_SUBCMDPARSER_NO_ARGUMENT_FOR_SHORT_ID;
                String message = getMessage(msgID,
                                            String.valueOf(argCharacter));
                throw new ArgumentException(msgID, message);
              }
            }
          }
        }
@@ -1072,6 +1131,7 @@
   */
  public void getFullUsage(StringBuilder buffer)
  {
    usageDisplayed = true;
    if ((toolDescription != null) && (toolDescription.length() > 0))
    {
      buffer.append(wrapText(toolDescription, 79));
@@ -1227,6 +1287,7 @@
   */
  public void getSubCommandUsage(StringBuilder buffer, SubCommand subCommand)
  {
    usageDisplayed = true;
    String scriptName = System.getProperty(PROPERTY_SCRIPT_NAME);
    if ((scriptName == null) || (scriptName.length() == 0))
    {
@@ -1541,5 +1602,20 @@
    outputStream.write(getBytes(buffer.toString()));
  }
  /**
   * Indicates whether 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 usageDisplayed()
  {
    return usageDisplayed;
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/EncodePasswordTestCase.java
@@ -755,16 +755,21 @@
  /**
   * Tests the EncodePassword tool with the "--help" option.
   * Tests the EncodePassword tool with the help options.
   */
  @Test()
  public void testHelp()
  {
    String[] args =
    {
      "--help"
    };
    String[] args = { "--help" };
    assertEquals(EncodePassword.encodePassword(args, false, null, null), 0);
    args = new String[] { "-H" };
    assertEquals(EncodePassword.encodePassword(args, false, null, null), 0);
    args = new String[] { "-?" };
    assertEquals(EncodePassword.encodePassword(args, false, null, null), 0);
    args = new String[] { "/?" };
    assertEquals(EncodePassword.encodePassword(args, false, null, null), 0);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPCompareTestCase.java
@@ -1197,6 +1197,12 @@
    args = new String[] { "-H" };
    assertEquals(LDAPCompare.mainCompare(args, false, null, null), 0);
    args = new String[] { "-?" };
    assertEquals(LDAPCompare.mainCompare(args, false, null, null), 0);
    args = new String[] { "/?" };
    assertEquals(LDAPCompare.mainCompare(args, false, null, null), 0);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPDeleteTestCase.java
@@ -961,6 +961,12 @@
    args = new String[] { "-H" };
    assertEquals(LDAPDelete.mainDelete(args, false, null, null), 0);
    args = new String[] { "-?" };
    assertEquals(LDAPDelete.mainDelete(args, false, null, null), 0);
    args = new String[] { "/?" };
    assertEquals(LDAPDelete.mainDelete(args, false, null, null), 0);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPModifyTestCase.java
@@ -1935,6 +1935,12 @@
    args = new String[] { "-H" };
    assertEquals(LDAPModify.mainModify(args, false, null, null), 0);
    args = new String[] { "-?" };
    assertEquals(LDAPModify.mainModify(args, false, null, null), 0);
    args = new String[] { "/?" };
    assertEquals(LDAPModify.mainModify(args, false, null, null), 0);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPPasswordModifyTestCase.java
@@ -1108,6 +1108,14 @@
    args = new String[] { "-H" };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
    args = new String[] { "-?" };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
    args = new String[] { "/?" };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchTestCase.java
@@ -1652,6 +1652,12 @@
    args = new String[] { "-H" };
    assertEquals(LDAPSearch.mainSearch(args, false, null, null), 0);
    args = new String[] { "-?" };
    assertEquals(LDAPSearch.mainSearch(args, false, null, null), 0);
    args = new String[] { "/?" };
    assertEquals(LDAPSearch.mainSearch(args, false, null, null), 0);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDIFDiffTestCase.java
@@ -85,11 +85,16 @@
  @Test()
  public void testUsage()
  {
    String[] args =
    {
      "--help"
    };
    String[] args = { "--help" };
    assertEquals(LDIFDiff.mainDiff(args, true), 0);
    args = new String[] { "-H" };
    assertEquals(LDIFDiff.mainDiff(args, true), 0);
    args = new String[] { "-?" };
    assertEquals(LDIFDiff.mainDiff(args, true), 0);
    args = new String[] { "/?" };
    assertEquals(LDIFDiff.mainDiff(args, true), 0);
  }
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ListBackendsTestCase.java
@@ -387,11 +387,16 @@
  @Test()
  public void testHelp()
  {
    String[] args =
    {
      "--help"
    };
    String[] args = { "--help" };
    assertEquals(ListBackends.listBackends(args, false, null, null), 0);
    args = new String[] { "-H" };
    assertEquals(ListBackends.listBackends(args, false, null, null), 0);
    args = new String[] { "-?" };
    assertEquals(ListBackends.listBackends(args, false, null, null), 0);
    args = new String[] { "/?" };
    assertEquals(ListBackends.listBackends(args, false, null, null), 0);
  }
}